Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions referencing/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,13 @@ def resolve(self, resolver: _Resolver[Schema]) -> _Resolved[Schema]:
continue
if isinstance(anchor, DynamicAnchor):
last = anchor.resource
try:
anchor = resolver._registry.anchor("", self.name).value
except (exceptions.NoSuchAnchor, exceptions.NoSuchResource):
pass
else:
if isinstance(anchor, DynamicAnchor):
last = anchor.resource
return _Resolved(
contents=last.contents,
resolver=resolver.in_subresource(last),
Expand Down
38 changes: 38 additions & 0 deletions referencing/tests/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ def test_multiple_lookup_dynamic_ref_to_nondynamic_ref():
assert fourth.contents == two.contents


def test_dynamic_ref_with_anonymous_root_schema():
template = referencing.jsonschema.DRAFT202012.create_resource(
{
"$id": "https://example.com/PaginatedTemplate",
"$defs": {
"itemType": {"$dynamicAnchor": "itemType", "not": {}},
},
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"items": {"$dynamicRef": "#itemType"},
},
},
},
)
anonymous = referencing.jsonschema.DRAFT202012.create_resource(
{
"$defs": {
"itemType": {"$dynamicAnchor": "itemType", "type": "string"},
},
"$ref": "https://example.com/PaginatedTemplate",
},
)
registry = Registry().with_resource(
"https://example.com/PaginatedTemplate",
template,
)
resolver = registry.resolver_with_root(anonymous)
resolved = resolver.lookup("https://example.com/PaginatedTemplate")
item_type = resolved.resolver.lookup("#itemType")
assert item_type.contents == {
"$dynamicAnchor": "itemType",
"type": "string",
}


def test_lookup_trivial_recursive_ref():
one = referencing.jsonschema.DRAFT201909.create_resource(
{"$recursiveAnchor": True},
Expand Down