From 29ad460fb37072200ce019ae4dce4d899350527f Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 25 Apr 2023 11:49:30 -0400 Subject: 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 --- jsonschema/tests/test_validators.py | 9 +++++++++ jsonschema/validators.py | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index a9b7f6a..00e0c40 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -2328,6 +2328,15 @@ class TestRefResolver(TestCase): 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): diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 72af8fc..78e5829 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -1021,9 +1021,10 @@ class _RefResolver: 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) -- cgit v1.2.1