Skip to content

Commit 90ea779

Browse files
committed
Fix a regression with RefResolver-based resolution in newly created drafts
We need a bit more state management to serve `RefResolver` until it's fully removed :/ (The handler here is simply to avoid needing to hit some remote reference.) Refs: #1061 (comment)
1 parent 56d57e7 commit 90ea779

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Diff for: CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v4.18.1
2+
=======
3+
4+
* Fix a regression with ``jsonschema.RefResolver`` based resolution when used in combination with a custom validation dialect (via ``jsonschema.validators.create``).
5+
16
v4.18.0
27
=======
38

Diff for: jsonschema/tests/test_validators.py

+22
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,28 @@ def test_pointer_within_schema_with_different_id(self):
23732373
validator = validators.Draft7Validator(another, resolver=two)
23742374
self.assertFalse(validator.is_valid({"maxLength": "foo"}))
23752375

2376+
def test_newly_created_validator_with_ref_resolver(self):
2377+
"""
2378+
See https://github.com/python-jsonschema/jsonschema/issues/1061#issuecomment-1624266555.
2379+
""" # noqa: E501
2380+
2381+
def handle(uri):
2382+
self.assertEqual(uri, "http://example.com/foo")
2383+
return {"type": "integer"}
2384+
2385+
resolver = validators._RefResolver("", {}, handlers={"http": handle})
2386+
Validator = validators.create(
2387+
meta_schema={},
2388+
validators=validators.Draft4Validator.VALIDATORS,
2389+
)
2390+
schema = {"$id": "http://example.com/bar", "$ref": "foo"}
2391+
validator = Validator(schema, resolver=resolver)
2392+
self.assertEqual(
2393+
(validator.is_valid({}), validator.is_valid(37)),
2394+
(False, True),
2395+
)
2396+
2397+
23762398

23772399
def sorted_errors(errors):
23782400
def key(error):

Diff for: jsonschema/validators.py

+5
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ def __attrs_post_init__(self):
275275
resource = specification.create_resource(self.schema)
276276
self._resolver = registry.resolver_with_root(resource)
277277

278+
# REMOVEME: Legacy ref resolution state management.
279+
push_scope = getattr(self._ref_resolver, "push_scope", None)
280+
if push_scope is not None:
281+
push_scope(id_of(self.schema))
282+
278283
@classmethod
279284
def check_schema(cls, schema, format_checker=_UNSET):
280285
Validator = validator_for(cls.META_SCHEMA, default=cls)

0 commit comments

Comments
 (0)