Skip to content

Commit f898ba6

Browse files
committed
Track base URI changes while resolving pointers.
1 parent 52d9bcf commit f898ba6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

referencing/_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def pointer(self, pointer: str, resolver: Resolver[D]) -> Resolved[D]:
165165
contents = contents[segment] # type: ignore[reportUnknownArgumentType] # noqa: E501
166166
except LookupError:
167167
raise exceptions.PointerToNowhere(ref=pointer, resource=self)
168+
169+
# FIXME: this is slightly wrong, we need to know that we are
170+
# entering a subresource specifically, not just any mapping
171+
if not isinstance(contents, Sequence):
172+
subresource = self._specification.create_resource(contents) # type: ignore[reportUnknownArgumentType] # noqa: E501
173+
resolver = resolver.in_subresource(subresource)
168174
return Resolved(contents=contents, resolver=resolver) # type: ignore[reportUnknownArgumentType] # noqa: E501
169175

170176

referencing/tests/test_core.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,31 @@ def test_in_subresource(self):
503503
second = sub.lookup("grandchild")
504504
assert second.contents == {"ID": "grandchild"}
505505

506+
def test_in_pointer_subresource(self):
507+
root = ID_AND_CHILDREN.create_resource(
508+
{
509+
"ID": "http://example.com/",
510+
"children": [
511+
{
512+
"ID": "child/",
513+
"children": [{"ID": "grandchild"}],
514+
},
515+
],
516+
},
517+
)
518+
registry = Registry().with_resource(root.id(), root)
519+
520+
resolver = registry.resolver()
521+
first = resolver.lookup("http://example.com/")
522+
assert first.contents == root.contents
523+
524+
with pytest.raises(exceptions.Unresolvable):
525+
first.resolver.lookup("grandchild")
526+
527+
second = first.resolver.lookup("#/children/0")
528+
third = second.resolver.lookup("grandchild")
529+
assert third.contents == {"ID": "grandchild"}
530+
506531

507532
class TestSpecification:
508533
def test_create_resource(self):

0 commit comments

Comments
 (0)