Skip to content

Commit

Permalink
Also suggest a correct pointer for #/.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Mar 27, 2023
1 parent baef09b commit 0c0c910
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v0.26.2
-------

* Also suggest a proper JSON Pointer for users who accidentally use ``#/`` and intend to refer to the entire resource.

v0.26.1
-------

Expand Down
9 changes: 8 additions & 1 deletion referencing/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ class PointerToNowhere(Unresolvable):
resource: Resource[Any]

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


@frozen
Expand Down
17 changes: 17 additions & 0 deletions referencing/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,23 @@ def test_lookup_non_existent_pointer_to_array_index(self):
resource=resource,
)

def test_lookup_pointer_to_empty_string(self):
resolver = Registry().resolver_with_root(Resource.opaque({"": {}}))
assert resolver.lookup("#/").contents == {}

def test_lookup_non_existent_pointer_to_empty_string(self):
resource = Resource.opaque({"foo": {}})
resolver = Registry().resolver_with_root(resource)
with pytest.raises(
exceptions.Unresolvable,
match="^'/' does not exist within {'foo': {}}.*'#'",
) as e:
resolver.lookup("#/")
assert e.value == exceptions.PointerToNowhere(
ref="/",
resource=resource,
)

def test_lookup_non_existent_anchor(self):
root = ID_AND_CHILDREN.create_resource({"anchors": {}})
resolver = Registry().with_resource("urn:example", root).resolver()
Expand Down

0 comments on commit 0c0c910

Please sign in to comment.