Skip to content

Commit aac36a1

Browse files
committed
Only resolve ids in known keywords
It looks like we were too permissive in: #71 This addresses issues from: - json-schema-org/JSON-Schema-Test-Suite#471 - json-schema-org/JSON-Schema-Test-Suite#484 The first issue is more important, I think. We were resolving `$id` in keywords like `enum` and `const`, which is unexpected since those values are literal. The second issue is a little trickier. It's possible people are using non-standard keys to store reference schemas, which will cause issues since this only looks in `definitions` now.
1 parent 66de9e3 commit aac36a1

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

lib/json_schemer/schema/base.rb

+14-6
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,21 @@ def resolve_ids(schema, ids = {}, parent_uri = nil, pointer = '')
665665
elsif schema.is_a?(Hash)
666666
uri = join_uri(parent_uri, schema[id_keyword])
667667
schema.each do |key, value|
668-
if key == id_keyword && uri != parent_uri
669-
ids[uri.to_s] = {
670-
schema: schema,
671-
pointer: pointer
672-
}
668+
case key
669+
when id_keyword
670+
unless uri == parent_uri
671+
ids[uri.to_s] = {
672+
schema: schema,
673+
pointer: pointer
674+
}
675+
end
676+
when 'items', 'allOf', 'anyOf', 'oneOf', 'additionalItems', 'contains', 'additionalProperties', 'propertyNames', 'if', 'then', 'else', 'not'
677+
resolve_ids(value, ids, uri, "#{pointer}/#{key}")
678+
when 'properties', 'patternProperties', 'definitions', 'dependencies'
679+
value.each do |subkey, subvalue|
680+
resolve_ids(subvalue, ids, uri, "#{pointer}/#{key}/#{subkey}")
681+
end
673682
end
674-
resolve_ids(value, ids, uri, "#{pointer}/#{key}")
675683
end
676684
end
677685
ids

test/json_schemer_test.rb

+16-18
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def test_it_returns_correct_pointers_for_ref_id
520520

521521
def test_can_refer_to_subschemas_inside_hashes
522522
root = {
523-
'foo' => {
523+
'definitions' => {
524524
'bar' => {
525525
'$id' => '#bar',
526526
'type' => 'string'
@@ -536,8 +536,8 @@ def test_can_refer_to_subschemas_inside_hashes
536536
{
537537
'data' => 42,
538538
'data_pointer' => '',
539-
'schema' => root['foo']['bar'],
540-
'schema_pointer' => '/foo/bar',
539+
'schema' => root['definitions']['bar'],
540+
'schema_pointer' => '/definitions/bar',
541541
'root_schema' => root,
542542
'type' => 'string'
543543
},
@@ -547,8 +547,8 @@ def test_can_refer_to_subschemas_inside_hashes
547547

548548
def test_can_refer_to_subschemas_inside_arrays
549549
root = {
550-
'foo' => [{
551-
'bar' => {
550+
'allOf' => [{
551+
'if' => {
552552
'$id' => '#bar',
553553
'type' => 'string'
554554
}
@@ -567,8 +567,8 @@ def test_can_refer_to_subschemas_inside_arrays
567567
{
568568
'data' => 1,
569569
'data_pointer' => '/a/x',
570-
'schema' => root['foo'].first['bar'],
571-
'schema_pointer' => '/foo/0/bar',
570+
'schema' => root['allOf'].first['if'],
571+
'schema_pointer' => '/allOf/0/if',
572572
'root_schema' => root,
573573
'type' => 'string'
574574
},
@@ -579,7 +579,7 @@ def test_can_refer_to_subschemas_inside_arrays
579579
def test_can_refer_to_subschemas_in_hash_with_remote_pointer
580580
ref_schema = {
581581
'$id' => 'http://example.com/ref_schema.json',
582-
'foo' => {
582+
'definitions' => {
583583
'bar' => {
584584
'$id' => '#bar',
585585
'type' => 'string'
@@ -604,8 +604,8 @@ def test_can_refer_to_subschemas_in_hash_with_remote_pointer
604604
{
605605
'data' => 1,
606606
'data_pointer' => '/a/x',
607-
'schema' => ref_schema['foo']['bar'],
608-
'schema_pointer' => '/foo/bar',
607+
'schema' => ref_schema['definitions']['bar'],
608+
'schema_pointer' => '/definitions/bar',
609609
'root_schema' => ref_schema,
610610
'type' => 'string'
611611
},
@@ -616,18 +616,16 @@ def test_can_refer_to_subschemas_in_hash_with_remote_pointer
616616
def test_can_refer_to_multiple_subschemas_in_hash
617617
ref_schema = {
618618
'$id' => 'http://example.com/ref_schema.json',
619-
'types' => {
619+
'definitions' => {
620620
'uuid' => {
621621
'$id' => "#uuid",
622622
'type' => 'string',
623623
'pattern' => "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
624624
}
625625
},
626-
'foo' => {
627-
'bar' => {
628-
'$id' => '#bar',
629-
'allOf' => [{ "$ref" => "#uuid"}]
630-
}
626+
'not' => {
627+
'$id' => '#bar',
628+
'allOf' => [{ "$ref" => "#uuid"}]
631629
}
632630
}
633631
root = {
@@ -648,8 +646,8 @@ def test_can_refer_to_multiple_subschemas_in_hash
648646
{
649647
'data' => "1122-112",
650648
'data_pointer' => '/a/x',
651-
'schema' => ref_schema['types']['uuid'],
652-
'schema_pointer' => '/types/uuid',
649+
'schema' => ref_schema['definitions']['uuid'],
650+
'schema_pointer' => '/definitions/uuid',
653651
'root_schema' => ref_schema,
654652
'type' => 'pattern'
655653
},

0 commit comments

Comments
 (0)