Skip to content

Commit 29ad460

Browse files
committed
Ignore non-str $ids for *deprecated* RefResolver resolution.
The new referencing behavior is more correct (which is why it exists), but also means that even though `RefResolver` was/is untouched, it can be called now with more subschemas than previously, and in some cases that tickles this code to blow up (see the closed issue for a specific example). We simply now ignore non-strs, as doing so is no more wrong than this code used to be. Closes: #1085
1 parent 3b35731 commit 29ad460

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

jsonschema/tests/test_validators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,15 @@ def test_helpful_error_message_on_failed_pop_scope(self):
23282328
resolver.pop_scope()
23292329
self.assertIn("Failed to pop the scope", str(exc.exception))
23302330

2331+
def test_pointer_within_schema_with_different_id(self):
2332+
"""
2333+
See #1085.
2334+
"""
2335+
schema = validators.Draft7Validator.META_SCHEMA
2336+
resolver = validators._RefResolver("", schema)
2337+
validator = validators.Draft7Validator(schema, resolver=resolver)
2338+
self.assertFalse(validator.is_valid({"maxLength": "foo"}))
2339+
23312340

23322341
def sorted_errors(errors):
23332342
def key(error):

jsonschema/validators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,10 @@ def _find_in_subschemas(self, url):
10211021
return None
10221022
uri, fragment = urldefrag(url)
10231023
for subschema in subschemas:
1024-
target_uri = self._urljoin_cache(
1025-
self.resolution_scope, subschema["$id"],
1026-
)
1024+
id = subschema["$id"]
1025+
if not isinstance(id, str):
1026+
continue
1027+
target_uri = self._urljoin_cache(self.resolution_scope, id)
10271028
if target_uri.rstrip("/") == uri.rstrip("/"):
10281029
if fragment:
10291030
subschema = self.resolve_fragment(subschema, fragment)

0 commit comments

Comments
 (0)