Skip to content

Commit 41e6e4a

Browse files
RonnyPfannschmidtCursor AIclaude
committed
Fix KEEP_REF handler and add permissive_object_on_recursion
Fix keep_ref_on_recursion to actually preserve $ref dicts instead of replacing them with None. When the handler returns KEEP_REF, _build_recursion_chain now returns the sentinel early and _unroll_recursive skips substitution, leaving original $ref nodes intact. Add permissive_object_on_recursion handler that replaces recursive $ref with {"type": "object", "additionalProperties": true, "x-recursive-ref": "<original ref>"} for consumers that need valid schema nodes at recursion boundaries. Co-authored-by: Cursor AI <[email protected]> Co-authored-by: Anthropic Claude Opus 4.6 <[email protected]>
1 parent f8247b4 commit 41e6e4a

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎prance/util/resolver.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ def keep_ref_on_recursion(limit, parsed_url, recursions=()):
127127
return KEEP_REF
128128

129129

130+
def permissive_object_on_recursion(limit, parsed_url, recursions=()):
131+
"""Replace recursive ``$ref`` with a permissive object schema.
132+
133+
The replacement allows any attributes and tags the original
134+
reference via the ``x-recursive-ref`` extension key.
135+
"""
136+
return {
137+
"type": "object",
138+
"additionalProperties": True,
139+
"x-recursive-ref": parsed_url.geturl(),
140+
}
141+
142+
130143
# -- Ref-key: a hashable identifier for a unique $ref target -----------
131144
# Tuple of (url_resource_string, tuple_of_obj_path_parts).
132145

@@ -415,6 +428,8 @@ def _unroll_recursive(self, rk, raw, recursive_sites, resolved):
415428

416429
# Build the recursion chain for this specific dependency
417430
chain = self._build_recursion_chain(rk, dep_key, dep_sites, resolved)
431+
if chain is KEEP_REF:
432+
continue
418433
resolved[dep_key] = chain
419434

420435
# Now substitute the chain into raw
@@ -444,7 +459,7 @@ def _build_recursion_chain(self, parent_key, dep_key, dep_sites, resolved):
444459

445460
handler_value = self.__reclimit_handler(self.__reclimit, dep_url, recursions)
446461
if isinstance(handler_value, _KeepRef):
447-
handler_value = None
462+
return KEEP_REF
448463

449464
# We need the raw template for the dep_key target. If dep_key
450465
# is the same as parent_key (self-reference), we need to re-fetch

‎tests/test_cli.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ def test_validate_recursive_with_allow_recursion(runner):
131131
result = runner.invoke(
132132
cli.validate, ["--allow-recursion", "tests/specs/self_recursive.yaml"]
133133
)
134+
assert result.exit_code == 0
134135
assert "Allowing recursive references" in result.output
135-
assert "ResolutionError" not in result.output
136+
assert "Validates OK" in result.output
136137

137138

138139
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")

‎tests/test_util_resolver.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,42 @@ def test_resolve_package_ref(mock_get):
743743
res.specs, ("paths", "/pets/{petId}", "get", "responses", "203", "schema")
744744
)
745745
assert "required" in val
746+
747+
748+
def test_keep_ref_on_recursion():
749+
specs = get_specs("tests/specs/self_recursive.yaml")
750+
res = resolver.RefResolver(
751+
specs,
752+
fs.abspath("tests/specs/self_recursive.yaml"),
753+
recursion_limit_handler=resolver.keep_ref_on_recursion,
754+
)
755+
res.resolve_references()
756+
757+
from prance.util.path import path_get
758+
759+
file_def = path_get(res.specs, ("definitions", "File"))
760+
# reclimit=1: File is resolved once, the recursive parent inside
761+
# that resolved copy keeps its $ref
762+
inner_parent = path_get(file_def, ("properties", "parent", "properties", "parent"))
763+
assert "$ref" in inner_parent
764+
assert inner_parent["$ref"] == "#/definitions/File"
765+
766+
767+
def test_permissive_object_on_recursion():
768+
specs = get_specs("tests/specs/self_recursive.yaml")
769+
res = resolver.RefResolver(
770+
specs,
771+
fs.abspath("tests/specs/self_recursive.yaml"),
772+
recursion_limit_handler=resolver.permissive_object_on_recursion,
773+
)
774+
res.resolve_references()
775+
776+
from prance.util.path import path_get
777+
778+
file_def = path_get(res.specs, ("definitions", "File"))
779+
# reclimit=1: File is resolved once, the recursive parent inside
780+
# that resolved copy gets the permissive object
781+
inner_parent = path_get(file_def, ("properties", "parent", "properties", "parent"))
782+
assert inner_parent["type"] == "object"
783+
assert inner_parent["additionalProperties"] is True
784+
assert "x-recursive-ref" in inner_parent

0 commit comments

Comments
 (0)