brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Dereference external subschema with nested reference #106

Closed kytrinyx closed 5 years ago

kytrinyx commented 5 years ago

Hi,

I ran into something that I think is a bug. The key thing is that foo points to bar (in a separate schema) which points to baz (in the second schema). After expanding the references, bar is expanded, but baz doesn't have the data I would expect it to, like so:

> schema1.inspect_schema
=> {:@expanded=>"true",
 :@type=>["\"object\""],
 :@properties=>
  {"foo"=>"http://json-schema.org/b.json#/definitions/bar [EXPANDED] [CLONE]",
   "foo2"=>"http://json-schema.org/b.json#/definitions/baz [EXPANDED] [CLONE]"}}

> schema2.inspect_schema
=> {:@expanded=>"true",
 :@definitions=>
  {"bar"=>{:@expanded=>"true", :@type=>["\"object\""], :@properties=>{"omg"=>"#/definitions/baz [COLLAPSED] [ORIGINAL]"}},
   "baz"=>{:@expanded=>"true", :@type=>["\"string\""], :@max_length=>"3"}},
 :@type=>["\"object\""]}

I added a loop to dereference subschemas, but this blew up, because the second reference was local to the second schema (so no URI in the reference). When the resolve_pointer method did its lookup, it would find a nil URI, and therefore attempt to resolve against the base schema @schema which in this case is the wrong one. By copying over the URI from the original reference, the lookup happens in the right place, but now I've got some infinite recursion going on.

kytrinyx commented 5 years ago

@brandur I think I've figured this one out, and have updated the original PR description with my findings.

brandur commented 5 years ago

Thanks for digging into this Katrina!

I haven't done enough recent work on this library to say for sure we haven't missed any other edges, but what you've done looks sane as far as I can tell. Thanks for adding the new test case — that makes evaluating what's happening here a lot easier!

brandur commented 5 years ago

Released as 0.20.2.

kytrinyx commented 5 years ago

Awesome, thank you @brandur !