-
-
Notifications
You must be signed in to change notification settings - Fork 427
Fix RecursionError in _merge_ref_with_schema for circular $ref #2983
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
Merged
+516
−5
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
26f1041
Fix RecursionError in _merge_ref_with_schema for circular $ref
koxudaxi 2ebee75
Merge remote-tracking branch 'origin/main' into fix/circular-ref-merg…
koxudaxi 22eaa82
Fix external ref cycle detection context and add e2e coverage
koxudaxi 457f2a1
Add schema markers to circular ref fixtures
koxudaxi 577e416
Generate alias model for circular ref with keywords
koxudaxi 6d8c3d1
Restrict ref+keywords alias generation to definitions
koxudaxi 000cf9c
Simplify ref fragment parsing in cycle detection
koxudaxi db8e024
Handle URL refs in cycle detection context
koxudaxi c9768fd
Use conservative fallback for circular ref detection
koxudaxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/data/expected/main/jsonschema/circular_ref_external_relative_keywords.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: root.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| root: Context | None = None | ||
|
|
||
|
|
||
| class Context(BaseModel): | ||
| child: Child | None = None | ||
|
|
||
|
|
||
| class Child(BaseModel): | ||
| parent: Context | None = None | ||
|
|
||
|
|
||
| Model.update_forward_refs() | ||
| Context.update_forward_refs() |
36 changes: 36 additions & 0 deletions
36
tests/data/expected/main/jsonschema/circular_ref_indirect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: circular_ref_indirect.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Kind(Enum): | ||
| x = 'x' | ||
| y = 'y' | ||
|
|
||
|
|
||
| class NodeC(BaseModel): | ||
| value: str | None = None | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| root: NodeA | None = None | ||
|
|
||
|
|
||
| class NodeA(BaseModel): | ||
| kind: Kind | None = None | ||
| c: NodeC | None = None | ||
| b: NodeB | None = None | ||
|
|
||
|
|
||
| class NodeB(BaseModel): | ||
| a: NodeA | None = None | ||
|
|
||
|
|
||
| Model.update_forward_refs() | ||
| NodeA.update_forward_refs() |
23 changes: 23 additions & 0 deletions
23
tests/data/expected/main/jsonschema/circular_ref_ref_with_schema_keywords.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: circular_ref_ref_with_schema_keywords.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| root: Node | None = None | ||
|
|
||
|
|
||
| class Node(BaseModel): | ||
| __root__: BaseNode | ||
|
|
||
|
|
||
| class BaseNode(BaseModel): | ||
| next: Node | None = None | ||
|
|
||
|
|
||
| Model.update_forward_refs() | ||
| Node.update_forward_refs() |
14 changes: 14 additions & 0 deletions
14
tests/data/expected/main/jsonschema/circular_ref_root_with_type.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: circular_ref_root_with_type.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Node(BaseModel): | ||
| child: Node | None = None | ||
|
|
||
|
|
||
| Node.update_forward_refs() |
19 changes: 19 additions & 0 deletions
19
tests/data/expected/main/jsonschema/circular_ref_with_schema_keywords.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: circular_ref_with_schema_keywords.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Context(BaseModel): | ||
| name: str | None = None | ||
| children: list[Context] | None = None | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| root: Context | None = None | ||
|
|
||
|
|
||
| Context.update_forward_refs() |
11 changes: 11 additions & 0 deletions
11
tests/data/expected/main/jsonschema/x_python_import_unused.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: x_python_import_unused.json | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Model(BaseModel): | ||
| name: str | None = None |
19 changes: 19 additions & 0 deletions
19
tests/data/jsonschema/circular_ref_external_relative_keywords/defs/context.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "$defs": { | ||
| "Context": { | ||
| "type": "object", | ||
| "properties": { | ||
| "child": { | ||
| "anyOf": [ | ||
| { | ||
| "type": "object", | ||
| "$ref": "nested/child.json#/$defs/Child" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/Context" | ||
| } |
22 changes: 22 additions & 0 deletions
22
tests/data/jsonschema/circular_ref_external_relative_keywords/defs/nested/child.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "$defs": { | ||
| "Child": { | ||
| "type": "object", | ||
| "x-bad-ref": { | ||
| "$ref": "#missing" | ||
| }, | ||
| "properties": { | ||
| "parent": { | ||
| "anyOf": [ | ||
| { | ||
| "type": "object", | ||
| "$ref": "../context.json#/$defs/Context" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/Child" | ||
| } |
9 changes: 9 additions & 0 deletions
9
tests/data/jsonschema/circular_ref_external_relative_keywords/root.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "object", | ||
| "properties": { | ||
| "root": { | ||
| "$ref": "defs/context.json#/$defs/Context" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "$defs": { | ||
| "NodeA": { | ||
| "type": "object", | ||
| "properties": { | ||
| "kind": { | ||
| "type": "string", | ||
| "enum": ["x", "y"] | ||
| }, | ||
| "c": { | ||
| "anyOf": [{"$ref": "#/$defs/NodeC"}] | ||
| }, | ||
| "b": { | ||
| "anyOf": [{"type": "object", "$ref": "#/$defs/NodeB"}] | ||
| } | ||
| } | ||
| }, | ||
| "NodeB": { | ||
| "type": "object", | ||
| "properties": { | ||
| "a": { | ||
| "anyOf": [{"type": "object", "$ref": "#/$defs/NodeA"}] | ||
| } | ||
| } | ||
| }, | ||
| "NodeC": { | ||
| "type": "object", | ||
| "properties": { | ||
| "value": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "type": "object", | ||
| "properties": { | ||
| "root": {"$ref": "#/$defs/NodeA"} | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
tests/data/jsonschema/circular_ref_ref_with_schema_keywords.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "$defs": { | ||
| "Node": { | ||
| "$ref": "#/$defs/BaseNode", | ||
| "type": "object" | ||
| }, | ||
| "BaseNode": { | ||
| "type": "object", | ||
| "properties": { | ||
| "next": { | ||
| "$ref": "#/$defs/Node" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "type": "object", | ||
| "properties": { | ||
| "root": { | ||
| "$ref": "#/$defs/Node" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "$defs": { | ||
| "Node": { | ||
| "type": "object", | ||
| "properties": { | ||
| "child": { | ||
| "anyOf": [{"type": "object", "$ref": "#/$defs/Node"}] | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/Node", | ||
| "type": "object" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.