Skip to content

Commit

Permalink
Merge pull request #297 from tdakkota/fix/ref-to-ref-handling
Browse files Browse the repository at this point in the history
fix(jsonschema): ref-to-ref handling, prevent false-positive infinite recursion detection
  • Loading branch information
ernado authored Apr 15, 2022
2 parents d9e5604 + c04582d commit 5bc1fb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jsonschema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func (p *Parser) resolve(ref string, ctx resolveCtx) (*Schema, error) {
return nil, errors.New("infinite recursion")
}
ctx[ref] = struct{}{}
defer func() {
// Drop the resolved ref to prevent false-positive infinite recursion detection.
delete(ctx, ref)
}()

raw, err := p.resolver.ResolveReference(ref)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions jsonschema/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,41 @@ func TestSchemaInfiniteRecursion(t *testing.T) {
}
}

func TestSchemaRefToRef(t *testing.T) {
// This regression test checks ref-to-ref handling.
//
// Such schema caused a false-positive infinite recursion error before.
components := components{
"first": {
Ref: "#/components/schemas/second",
},
"second": {
Ref: "#/components/schemas/third",
},
"third": {
Ref: "#/components/schemas/actual",
},
"actual": {
Type: "integer",
},
"referer": {
Type: "object",
Properties: RawProperties{
{"Ref1", &RawSchema{Ref: "#/components/schemas/first"}},
{"Ref2", &RawSchema{Ref: "#/components/schemas/first"}},
{"Ref3", &RawSchema{Ref: "#/components/schemas/second"}},
},
},
}
parser := NewParser(Settings{
Resolver: components,
})
_, err := parser.Parse(&RawSchema{
Ref: "#/components/schemas/referer",
})
require.NoError(t, err)
}

func TestSchemaSideEffects(t *testing.T) {
expectSide := []*Schema{
{
Expand Down

0 comments on commit 5bc1fb4

Please sign in to comment.