Skip to content

Commit

Permalink
Resolve JSON pointer refs locally
Browse files Browse the repository at this point in the history
When `instance.base_uri` is relaive and `ref` is a JSON pointer
fragment, the fragment is removed from `ref_uri` which causes it to be
empty. The ref is meant to be resolved locally, but it was being passed
to `resolve_ref` instead. I thought this case was covered by the
`ref_uri.to_s == @base_uri.to_s` check, but it's currently possible
(though incorrect?) for `@base_uri` to be relative.

Checking `ref_uri.to_s.empty?` should be ok since it means `ref` was a
plain JSON pointer fragment (eg, `#/path/to/schema`).

Example here: #131

Closes: #131
  • Loading branch information
davishmcclurg committed Jun 14, 2023
1 parent 63224cf commit dacec83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/json_schemer/schema/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def validate_ref(instance, ref, &block)
ref_uri.fragment = nil
end

ref_object = if ids.key?(ref_uri) || ref_uri.to_s == @base_uri.to_s
ref_object = if ids.key?(ref_uri) || ref_uri.to_s.empty? || ref_uri.to_s == @base_uri.to_s
self
else
child(resolve_ref(ref_uri), base_uri: ref_uri)
Expand Down
23 changes: 23 additions & 0 deletions test/ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,27 @@ def test_it_handles_base_uri_change_folder_in_subschema
assert(schema.valid?({ 'list' => [1] }))
refute(schema.valid?({ 'list' => ['a'] }))
end

def test_it_handles_relative_base_uri_json_pointer_ref
refs = {
'relative' => {
'definitions' => {
'foo' => {
'type' => 'integer'
}
},
'properties' => {
'bar' => {
'$ref' => '#/definitions/foo'
}
}
}
}
schema = JSONSchemer.schema(
{ '$ref' => 'relative' },
:ref_resolver => proc { |uri| refs[uri.to_s] }
)
assert(schema.valid?({ 'bar' => 1 }))
refute(schema.valid?({ 'bar' => '1' }))
end
end

0 comments on commit dacec83

Please sign in to comment.