Skip to content

Commit 9b476b4

Browse files
committed
Strip empty fragments when directly looking up a resource.
This isn't necessarily 100% 'nice feeling', but early versions of JSON Schema used URIs with empty fragments as their $ids, and even though later versions don't, and future versions may even make this an error, a downstream library (including jsonschema) is likely to copy these $ids exactly as-is and expect the resource to be retrievable, despite the last commit making some of this behavior JSON Schema-specific. We may revisit this all later if/when it turns out to matter. Refs: python-jsonschema/jsonschema#1064
1 parent 90665d6 commit 9b476b4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

referencing/_core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ def contents(self, uri: URI) -> D:
362362
"""
363363
Retrieve the contents identified by the given URI.
364364
"""
365-
return self._resources[uri].contents
365+
# Empty fragment URIs are equivalent to URIs without the fragment.
366+
# TODO: Is this true for non JSON Schema resources? Probably not.
367+
return self._resources[uri.rstrip("#")].contents
366368

367369
def crawl(self) -> Registry[D]:
368370
"""

referencing/tests/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ def test_contents(self):
155155
registry = Registry().with_resource(uri, resource)
156156
assert registry.contents(uri) == {"foo": "bar"}
157157

158+
def test_contents_strips_empty_fragments(self):
159+
uri = "http://example.com/"
160+
resource = ID_AND_CHILDREN.create_resource({"ID": uri + "#"})
161+
registry = resource @ Registry()
162+
assert registry.contents(uri) == {"ID": uri + "#"}
163+
158164
def test_crawled_anchor(self):
159165
resource = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
160166
registry = Registry().with_resource("urn:example", resource)

0 commit comments

Comments
 (0)