Skip to content

Commit db2d117

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 ad4776d commit db2d117

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

lib/json_schemer/schema/base.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,21 @@ def resolve_ids(schema, ids = {}, parent_uri = nil, pointer = '')
645645
elsif schema.is_a?(Hash)
646646
uri = join_uri(parent_uri, schema[id_keyword])
647647
schema.each do |key, value|
648-
if key == id_keyword && uri != parent_uri
649-
ids[uri.to_s] = {
650-
schema: schema,
651-
pointer: pointer
652-
}
648+
case key
649+
when id_keyword
650+
unless uri == parent_uri
651+
ids[uri.to_s] = {
652+
schema: schema,
653+
pointer: pointer
654+
}
655+
end
656+
when 'items', 'allOf', 'anyOf', 'oneOf', 'additionalItems', 'contains', 'additionalProperties', 'propertyNames', 'if', 'then', 'else', 'not'
657+
resolve_ids(value, ids, uri, "#{pointer}/#{key}")
658+
when 'properties', 'patternProperties', 'definitions', 'dependencies'
659+
value.each do |subkey, subvalue|
660+
resolve_ids(subvalue, ids, uri, "#{pointer}/#{key}/#{subkey}")
661+
end
653662
end
654-
resolve_ids(value, ids, uri, "#{pointer}/#{key}")
655663
end
656664
end
657665
ids

test/json_schemer_test.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def test_it_returns_correct_pointers_for_ref_id
522522

523523
def test_can_refer_to_subschemas_inside_hashes
524524
root = {
525-
'foo' => {
525+
'definitions' => {
526526
'bar' => {
527527
'$id' => '#bar',
528528
'type' => 'string'
@@ -537,17 +537,17 @@ def test_can_refer_to_subschemas_inside_hashes
537537
assert errors.first == {
538538
'data' => 42,
539539
'data_pointer' => '',
540-
'schema' => root['foo']['bar'],
541-
'schema_pointer' => '/foo/bar',
540+
'schema' => root['definitions']['bar'],
541+
'schema_pointer' => '/definitions/bar',
542542
'root_schema' => root,
543543
'type' => 'string'
544544
}
545545
end
546546

547547
def test_can_refer_to_subschemas_inside_arrays
548548
root = {
549-
'foo' => [{
550-
'bar' => {
549+
'allOf' => [{
550+
'if' => {
551551
'$id' => '#bar',
552552
'type' => 'string'
553553
}
@@ -565,8 +565,8 @@ def test_can_refer_to_subschemas_inside_arrays
565565
assert errors.first == {
566566
'data' => 1,
567567
'data_pointer' => '/a/x',
568-
'schema' => root['foo'].first['bar'],
569-
'schema_pointer' => '/foo/0/bar',
568+
'schema' => root['allOf'].first['if'],
569+
'schema_pointer' => '/allOf/0/if',
570570
'root_schema' => root,
571571
'type' => 'string'
572572
}
@@ -575,7 +575,7 @@ def test_can_refer_to_subschemas_inside_arrays
575575
def test_can_refer_to_subschemas_in_hash_with_remote_pointer
576576
ref_schema = {
577577
'$id' => 'http://example.com/ref_schema.json',
578-
'foo' => {
578+
'definitions' => {
579579
'bar' => {
580580
'$id' => '#bar',
581581
'type' => 'string'
@@ -599,8 +599,8 @@ def test_can_refer_to_subschemas_in_hash_with_remote_pointer
599599
assert errors.first == {
600600
'data' => 1,
601601
'data_pointer' => '/a/x',
602-
'schema' => ref_schema['foo']['bar'],
603-
'schema_pointer' => '/foo/bar',
602+
'schema' => ref_schema['definitions']['bar'],
603+
'schema_pointer' => '/definitions/bar',
604604
'root_schema' => ref_schema,
605605
'type' => 'string'
606606
}
@@ -609,18 +609,16 @@ def test_can_refer_to_subschemas_in_hash_with_remote_pointer
609609
def test_can_refer_to_multiple_subschemas_in_hash
610610
ref_schema = {
611611
'$id' => 'http://example.com/ref_schema.json',
612-
'types' => {
612+
'definitions' => {
613613
'uuid' => {
614614
'$id' => "#uuid",
615615
'type' => 'string',
616616
'pattern' => "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
617617
}
618618
},
619-
'foo' => {
620-
'bar' => {
621-
'$id' => '#bar',
622-
'allOf' => [{ "$ref" => "#uuid"}]
623-
}
619+
'not' => {
620+
'$id' => '#bar',
621+
'allOf' => [{ "$ref" => "#uuid"}]
624622
}
625623
}
626624
root = {
@@ -640,8 +638,8 @@ def test_can_refer_to_multiple_subschemas_in_hash
640638
assert errors.first == {
641639
'data' => "1122-112",
642640
'data_pointer' => '/a/x',
643-
'schema' => ref_schema['types']['uuid'],
644-
'schema_pointer' => '/types/uuid',
641+
'schema' => ref_schema['definitions']['uuid'],
642+
'schema_pointer' => '/definitions/uuid',
645643
'root_schema' => ref_schema,
646644
'type' => 'pattern'
647645
}

0 commit comments

Comments
 (0)