Skip to content

Commit

Permalink
Handle empty pointers (root pointers) in Resource.pointer.
Browse files Browse the repository at this point in the history
Resource.lookup did this correctly for fragment-represented pointers,
but this function needs to as well.

Closes: #148
  • Loading branch information
Julian committed Apr 30, 2024
1 parent a524132 commit 1357a73
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

v0.35.1
-------

* Make ``Resource.pointer`` also properly handle empty pointers (which refer to the root document).
This fix likely only affects you if you were using that function directly, as ``Resource.lookup`` already handles empty fragments.

v0.35.0
-------

Expand Down
3 changes: 3 additions & 0 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def pointer(self, pointer: str, resolver: Resolver[D]) -> Resolved[D]:
if the pointer points to a location not present in the document
"""
if not pointer:
return self

contents = self.contents
segments: list[int | str] = []
for segment in unquote(pointer[1:]).split("/"):
Expand Down
6 changes: 6 additions & 0 deletions referencing/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ def test_pointer_to_array(self):
resolver = Registry().resolver()
assert resource.pointer("/foo/bar/0", resolver=resolver).contents == 3

def test_root_pointer(self):
contents = {"foo": "baz"}
resource = Resource.opaque(contents=contents)
resolver = Registry().resolver()
assert resource.pointer("", resolver=resolver).contents == contents

def test_opaque(self):
contents = {"foo": "bar"}
assert Resource.opaque(contents) == Resource(
Expand Down

0 comments on commit 1357a73

Please sign in to comment.