-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: handle circular JSON Pointer $ref in dereference_refs #3896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -116,6 +116,42 @@ def test_falls_back_for_circular_refs(self): | |||||||||||||||||||||
| assert result.get("type") == "object" | ||||||||||||||||||||||
| assert "$defs" in result # $defs preserved for circular refs | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_falls_back_for_circular_json_pointer_refs(self): | ||||||||||||||||||||||
| """Test that circular JSON Pointer $ref (non-$defs) does not crash. | ||||||||||||||||||||||
| .NET/System.Text.Json emits $ref with JSON Pointer paths like | ||||||||||||||||||||||
| #/properties/nodes/items instead of $defs-based references. | ||||||||||||||||||||||
| Circular pointers must not cause a RecursionError. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| schema = { | ||||||||||||||||||||||
| "type": "object", | ||||||||||||||||||||||
| "properties": { | ||||||||||||||||||||||
| "nodes": { | ||||||||||||||||||||||
| "type": "array", | ||||||||||||||||||||||
| "items": { | ||||||||||||||||||||||
| "type": "object", | ||||||||||||||||||||||
| "properties": { | ||||||||||||||||||||||
| "value": {"type": "string"}, | ||||||||||||||||||||||
| "children": { | ||||||||||||||||||||||
| "type": "array", | ||||||||||||||||||||||
| "items": {"$ref": "#/properties/nodes/items"}, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| result = dereference_refs(schema) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Should return the schema without crashing; circular refs stay unresolved | ||||||||||||||||||||||
| assert result["type"] == "object" | ||||||||||||||||||||||
| assert "properties" in result | ||||||||||||||||||||||
| # The circular $ref should be preserved (not inlined) | ||||||||||||||||||||||
| children_items = result["properties"]["nodes"]["items"]["properties"][ | ||||||||||||||||||||||
| "children" | ||||||||||||||||||||||
| ]["items"] | ||||||||||||||||||||||
| assert "$ref" in children_items | ||||||||||||||||||||||
|
Comment on lines
+149
to
+153
|
||||||||||||||||||||||
| # The circular $ref should be preserved (not inlined) | |
| children_items = result["properties"]["nodes"]["items"]["properties"][ | |
| "children" | |
| ]["items"] | |
| assert "$ref" in children_items | |
| # The circular $ref should be preserved exactly (not inlined or rewritten) | |
| children_items = result["properties"]["nodes"]["items"]["properties"][ | |
| "children" | |
| ]["items"] | |
| assert children_items["$ref"] == "#/properties/nodes/items" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception-block comment says the fallback “resolv[es] only root-level $ref”, but
resolve_root_ref()is a no-op when there is no root$ref(as in the JSON Pointer cycle case). Consider rewording this comment to reflect the actual behavior: fall back toresolve_root_ref()(which may leave the schema unchanged) rather than implying root refs will always be resolved.