Skip to content

Commit

Permalink
rebased
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Nov 26, 2023
1 parent 7b068de commit 4c3a4eb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions openapi2conv/issue847_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ paths:
err = v3.Validate(context.Background())
require.NoError(t, err)

schemaRequired := v3.Paths["/ping"].Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Required
schemaRequired := v3.Paths.Value("/ping").Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Required
require.Equal(t, schemaRequired, []string{"file"})

fieldRequired := v3.Paths["/ping"].Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Properties["file"].Value.Required
fieldRequired := v3.Paths.Value("/ping").Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Properties["file"].Value.Required
require.Nil(t, fieldRequired)
}
5 changes: 5 additions & 0 deletions openapi3/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (pair *callbacksKV) Next() *callbacksKV {
return (*callbacksKV)(ompair.Next())
}

// NewCallbacksWithCapacity builds a callbacks object of the given capacity.
func NewCallbacksWithCapacity(cap int) *Callbacks {
return &Callbacks{om: orderedmap.New[string, *CallbackRef](cap)}
}

var _ jsonpointer.JSONPointable = (*Callbacks)(nil)

// JSONLookup implements https://pkg.go.dev/github.com/go-openapi/jsonpointer#JSONPointable
Expand Down
21 changes: 14 additions & 7 deletions openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,6 @@ func (doc *T) addCallbackToSpec(c *CallbackRef, refNameResolver RefNameResolver,
return false
}
name := refNameResolver(c.Ref)

if doc.Components == nil {
doc.Components = &Components{}
}
if doc.Components.Callbacks == nil {
doc.Components.Callbacks = &Callbacks{}
}
c.Ref = "#/components/callbacks/" + name
doc.Components.Callbacks.Set(name, &CallbackRef{Value: c.Value})
return true
Expand Down Expand Up @@ -354,6 +347,13 @@ func (doc *T) derefPaths(paths *orderedmap.OrderedMap[string, *PathItem], refNam
if op.RequestBody != nil && op.RequestBody.Value != nil {
doc.derefRequestBody(*op.RequestBody.Value, refNameResolver, pathIsExternal || isExternal)
}

if doc.Components == nil {
doc.Components = &Components{}
}
if doc.Components.Callbacks == nil {
doc.Components.Callbacks = NewCallbacksWithCapacity(op.Callbacks.Len())
}
for pair := op.Callbacks.Iter(); pair != nil; pair = pair.Next() {
cb := pair.Value
isExternal := doc.addCallbackToSpec(cb, refNameResolver, pathIsExternal)
Expand All @@ -362,6 +362,7 @@ func (doc *T) derefPaths(paths *orderedmap.OrderedMap[string, *PathItem], refNam
doc.derefPaths(cbValue, refNameResolver, pathIsExternal || isExternal)
}
}

doc.derefResponses(op.Responses, refNameResolver, pathIsExternal)
for _, param := range op.Parameters {
isExternal := doc.addParameterToSpec(param, refNameResolver, pathIsExternal)
Expand Down Expand Up @@ -425,6 +426,12 @@ func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(ref stri
doc.derefExamples(components.Examples, refNameResolver, false)
doc.derefLinks(components.Links, refNameResolver, false)

if doc.Components == nil {
doc.Components = &Components{}
}
if doc.Components.Callbacks == nil {
doc.Components.Callbacks = NewCallbacksWithCapacity(components.Callbacks.Len())
}
for pair := components.Callbacks.Iter(); pair != nil; pair = pair.Next() {
cb := pair.Value
isExternal := doc.addCallbackToSpec(cb, refNameResolver, false)
Expand Down
6 changes: 3 additions & 3 deletions openapi3/issue819_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ paths:
err = doc.Validate(sl.Context)
require.NoError(t, err)

require.NotNil(t, doc.Paths["/v1/operation"].Get.Responses.Get(201))
require.Nil(t, doc.Paths["/v1/operation"].Get.Responses.Get(404))
require.Nil(t, doc.Paths["/v1/operation"].Get.Responses.Get(999))
require.NotNil(t, doc.Paths.Value("/v1/operation").Get.Responses.Get(201))
require.Nil(t, doc.Paths.Value("/v1/operation").Get.Responses.Get(404))
require.Nil(t, doc.Paths.Value("/v1/operation").Get.Responses.Get(999))
}
4 changes: 2 additions & 2 deletions openapi3/refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ components:
require.NoError(t, err)
v, kind, err = ptr.Get(doc)
require.NoError(t, err)
require.IsType(t, Paths{}, v)
require.Equal(t, reflect.TypeOf(Paths{}).Kind(), kind)
require.IsType(t, &Paths{}, v)
require.Equal(t, reflect.TypeOf(&Paths{}).Kind(), kind)

ptr, err = jsonpointer.New("/paths/~1pet")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ func TestDecodeParameter(t *testing.T) {
Title: "MyAPI",
Version: "0.1",
}
doc := &openapi3.T{OpenAPI: "3.0.0", Info: info, Paths: openapi3.Paths{}}
doc := &openapi3.T{OpenAPI: "3.0.0", Info: info, Paths: openapi3.NewPaths()}
op := &openapi3.Operation{
OperationID: "test",
Parameters: []*openapi3.ParameterRef{{Value: tc.param}},
Expand Down

0 comments on commit 4c3a4eb

Please sign in to comment.