diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5bae3bee..98925166 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +v4.18.2 +======= + +* Fix an additional regression with the deprecated ``jsonschema.RefResolver`` and pointer resolution. + v4.18.1 ======= diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 5a4f87f3..ca03efd4 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -2394,6 +2394,25 @@ def handle(uri): (False, True), ) + def test_refresolver_with_pointer_in_schema_with_no_id(self): + """ + See https://github.com/python-jsonschema/jsonschema/issues/1124#issuecomment-1632574249. + """ # noqa: E501 + + schema = { + "properties": {"x": {"$ref": "#/definitions/x"}}, + "definitions": {"x": {"type": "integer"}}, + } + + validator = validators.Draft202012Validator( + schema, + resolver=validators._RefResolver("", schema), + ) + self.assertEqual( + (validator.is_valid({"x": "y"}), validator.is_valid({"x": 37})), + (False, True), + ) + def sorted_errors(errors): diff --git a/jsonschema/validators.py b/jsonschema/validators.py index b8a9c141..60c19821 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -278,7 +278,9 @@ def __attrs_post_init__(self): # REMOVEME: Legacy ref resolution state management. push_scope = getattr(self._ref_resolver, "push_scope", None) if push_scope is not None: - push_scope(id_of(self.schema)) + id = id_of(self.schema) + if id is not None: + push_scope(id) @classmethod def check_schema(cls, schema, format_checker=_UNSET):