Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect from recursion in openapi3.InternaliseRefs #578

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (doc *T) addCallbackToSpec(c *CallbackRef, refNameResolver RefNameResolver)
}

func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver) {
if s == nil {
if s == nil || doc.isVisitedSchema(s) {
return
}

Expand Down Expand Up @@ -229,6 +229,9 @@ func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver) {
func (doc *T) derefHeaders(hs Headers, refNameResolver RefNameResolver) {
for _, h := range hs {
doc.addHeaderToSpec(h, refNameResolver)
if doc.isVisitedHeader(h.Value) {
continue
}
doc.derefParameter(h.Value.Parameter, refNameResolver)
}
}
Expand Down Expand Up @@ -324,6 +327,8 @@ func (doc *T) derefPaths(paths map[string]*PathItem, refNameResolver RefNameReso
//
// doc.InternalizeRefs(context.Background(), nil)
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(ref string) string) {
doc.resetVisited()

if refNameResolver == nil {
refNameResolver = DefaultRefNameResolver
}
Expand Down
2 changes: 2 additions & 0 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type T struct {
Servers Servers `json:"servers,omitempty" yaml:"servers,omitempty"`
Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

visited visitedComponent
}

// MarshalJSON returns the JSON encoding of T.
Expand Down
4 changes: 4 additions & 0 deletions openapi3/testdata/recursiveRef/components/Cat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: object
properties:
cat:
$ref: ../openapi.yml#/components/schemas/Cat
2 changes: 2 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ components:
$ref: ./components/Foo/Foo2.yml
Bar:
$ref: ./components/Bar.yml
Cat:
$ref: ./components/Cat.yml
8 changes: 8 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml.internalized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
}
},
"type": "object"
},
"Cat": {
"properties": {
"cat": {
"$ref": "#/components/schemas/Cat"
}
},
"type": "object"
}
}
},
Expand Down
41 changes: 41 additions & 0 deletions openapi3/visited.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package openapi3

func newVisited() visitedComponent {
return visitedComponent{
header: make(map[*Header]struct{}),
schema: make(map[*Schema]struct{}),
}
}

type visitedComponent struct {
header map[*Header]struct{}
schema map[*Schema]struct{}
}

// resetVisited clears visitedComponent map
// should be called before recursion over doc *T
func (doc *T) resetVisited() {
doc.visited = newVisited()
}

// isVisitedHeader returns `true` if the *Header pointer was already visited
// otherwise it returns `false`
func (doc *T) isVisitedHeader(h *Header) bool {
if _, ok := doc.visited.header[h]; ok {
return true
}

doc.visited.header[h] = struct{}{}
return false
}

// isVisitedHeader returns `true` if the *Schema pointer was already visited
// otherwise it returns `false`
func (doc *T) isVisitedSchema(s *Schema) bool {
if _, ok := doc.visited.schema[s]; ok {
return true
}

doc.visited.schema[s] = struct{}{}
return false
}