Skip to content

Commit bf92aed

Browse files
committed
Fix $id in the presence of $ref on draft 6 and 7.
In these drafts, not only did $ref cause other validator keywords to be ignored, it prevents $id from setting the resolution scope too. In draft 3 and 4 this is now half-fixed in the sense that the correct URI is now the one that will be retrieved, but the logic for finding subschemas by ID is still broken on these drafts.
1 parent 167ac4b commit bf92aed

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

jsonschema/_legacy_validators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
from jsonschema.exceptions import ValidationError
33

44

5+
def id_of_ignore_ref(property="$id"):
6+
def id_of(schema):
7+
"""
8+
Ignore an ``$id`` sibling of ``$ref`` if it is present.
9+
10+
Otherwise, return the ID of the given schema.
11+
"""
12+
if schema is True or schema is False or "$ref" in schema:
13+
return ""
14+
return schema.get(property, "")
15+
return id_of
16+
17+
518
def ignore_ref_siblings(schema):
619
"""
720
Ignore siblings of ``$ref`` if it is present.

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def leap_second(test):
156156
or missing_format(jsonschema.draft3_format_checker)(test)
157157
or complex_email_validation(test)
158158
or skip(
159-
message=bug(371),
159+
message=bug(),
160160
subject="ref",
161161
valid=False,
162162
case_description=(
@@ -188,27 +188,27 @@ def leap_second(test):
188188
case_description="Recursive references between schemas",
189189
)(test)
190190
or skip(
191-
message=bug(371),
191+
message=bug(),
192192
subject="ref",
193193
case_description=(
194194
"Location-independent identifier with "
195195
"base URI change in subschema"
196196
),
197197
)(test)
198198
or skip(
199-
message=bug(371),
199+
message=bug(),
200200
subject="ref",
201201
case_description=(
202202
"$ref prevents a sibling id from changing the base uri"
203203
),
204204
)(test)
205205
or skip(
206-
message=bug(371),
206+
message=bug(),
207207
subject="id",
208208
description="match $ref to id",
209209
)(test)
210210
or skip(
211-
message=bug(371),
211+
message=bug(),
212212
subject="id",
213213
description="no match on enum or $ref to id",
214214
)(test)
@@ -248,13 +248,6 @@ def leap_second(test):
248248
subject="refRemote",
249249
case_description="base URI change - change folder in subschema",
250250
)(test)
251-
or skip(
252-
message=bug(371),
253-
subject="ref",
254-
case_description=(
255-
"$ref prevents a sibling $id from changing the base uri"
256-
),
257-
)(test)
258251
),
259252
)
260253

@@ -279,13 +272,6 @@ def leap_second(test):
279272
subject="refRemote",
280273
case_description="base URI change - change folder in subschema",
281274
)(test)
282-
or skip(
283-
message=bug(371),
284-
subject="ref",
285-
case_description=(
286-
"$ref prevents a sibling $id from changing the base uri"
287-
),
288-
)(test)
289275
or skip(
290276
message=bug(),
291277
subject="ref",

jsonschema/validators.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def extend(
445445
type_checker=_types.draft3_type_checker,
446446
format_checker=_format.draft3_format_checker,
447447
version="draft3",
448-
id_of=lambda schema: schema.get("id", ""),
448+
id_of=_legacy_validators.id_of_ignore_ref(property="id"),
449449
applicable_validators=_legacy_validators.ignore_ref_siblings,
450450
)
451451

@@ -482,7 +482,7 @@ def extend(
482482
type_checker=_types.draft4_type_checker,
483483
format_checker=_format.draft4_format_checker,
484484
version="draft4",
485-
id_of=lambda schema: schema.get("id", ""),
485+
id_of=_legacy_validators.id_of_ignore_ref(property="id"),
486486
applicable_validators=_legacy_validators.ignore_ref_siblings,
487487
)
488488

@@ -524,6 +524,7 @@ def extend(
524524
type_checker=_types.draft6_type_checker,
525525
format_checker=_format.draft6_format_checker,
526526
version="draft6",
527+
id_of=_legacy_validators.id_of_ignore_ref(),
527528
applicable_validators=_legacy_validators.ignore_ref_siblings,
528529
)
529530

@@ -566,6 +567,7 @@ def extend(
566567
type_checker=_types.draft7_type_checker,
567568
format_checker=_format.draft7_format_checker,
568569
version="draft7",
570+
id_of=_legacy_validators.id_of_ignore_ref(),
569571
applicable_validators=_legacy_validators.ignore_ref_siblings,
570572
)
571573

0 commit comments

Comments
 (0)