Skip to content

Commit 0c0c910

Browse files
committed
Also suggest a correct pointer for #/.
Refs: python-jsonschema/jsonschema#1070
1 parent baef09b commit 0c0c910

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/changes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
v0.26.2
6+
-------
7+
8+
* Also suggest a proper JSON Pointer for users who accidentally use ``#/`` and intend to refer to the entire resource.
9+
510
v0.26.1
611
-------
712

referencing/exceptions.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ class PointerToNowhere(Unresolvable):
9191
resource: Resource[Any]
9292

9393
def __str__(self):
94-
return f"{self.ref!r} does not exist within {self.resource.contents!r}"
94+
msg = f"{self.ref!r} does not exist within {self.resource.contents!r}"
95+
if self.ref == "/":
96+
msg += (
97+
". The pointer '/' is a valid JSON Pointer but it points to "
98+
"an empty string property ''. If you intended to point "
99+
"to the entire resource, you should use '#'."
100+
)
101+
return msg
95102

96103

97104
@frozen

referencing/tests/test_core.py

+17
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,23 @@ def test_lookup_non_existent_pointer_to_array_index(self):
723723
resource=resource,
724724
)
725725

726+
def test_lookup_pointer_to_empty_string(self):
727+
resolver = Registry().resolver_with_root(Resource.opaque({"": {}}))
728+
assert resolver.lookup("#/").contents == {}
729+
730+
def test_lookup_non_existent_pointer_to_empty_string(self):
731+
resource = Resource.opaque({"foo": {}})
732+
resolver = Registry().resolver_with_root(resource)
733+
with pytest.raises(
734+
exceptions.Unresolvable,
735+
match="^'/' does not exist within {'foo': {}}.*'#'",
736+
) as e:
737+
resolver.lookup("#/")
738+
assert e.value == exceptions.PointerToNowhere(
739+
ref="/",
740+
resource=resource,
741+
)
742+
726743
def test_lookup_non_existent_anchor(self):
727744
root = ID_AND_CHILDREN.create_resource({"anchors": {}})
728745
resolver = Registry().with_resource("urn:example", root).resolver()

0 commit comments

Comments
 (0)