Skip to content

Commit

Permalink
Ignore non-str $ids for *deprecated* RefResolver resolution.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Julian committed Apr 25, 2023
1 parent 3b35731 commit 29ad460
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 9 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,15 @@ def test_helpful_error_message_on_failed_pop_scope(self):
resolver.pop_scope()
self.assertIn("Failed to pop the scope", str(exc.exception))

def test_pointer_within_schema_with_different_id(self):
"""
See #1085.
"""
schema = validators.Draft7Validator.META_SCHEMA
resolver = validators._RefResolver("", schema)
validator = validators.Draft7Validator(schema, resolver=resolver)
self.assertFalse(validator.is_valid({"maxLength": "foo"}))


def sorted_errors(errors):
def key(error):
Expand Down
7 changes: 4 additions & 3 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,9 +1021,10 @@ def _find_in_subschemas(self, url):
return None
uri, fragment = urldefrag(url)
for subschema in subschemas:
target_uri = self._urljoin_cache(
self.resolution_scope, subschema["$id"],
)
id = subschema["$id"]
if not isinstance(id, str):
continue
target_uri = self._urljoin_cache(self.resolution_scope, id)
if target_uri.rstrip("/") == uri.rstrip("/"):
if fragment:
subschema = self.resolve_fragment(subschema, fragment)
Expand Down

0 comments on commit 29ad460

Please sign in to comment.