8000 Support indirect definition references by dmontagu · Pull Request #1130 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support indirect definition references #1130

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
merged 2 commits into from
Dec 21, 2023
Merged
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: 5 additions & 2 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3576,12 +3576,13 @@ def definitions_schema(schema: CoreSchema, definitions: list[CoreSchema]) -> Def
class DefinitionReferenceSchema(TypedDict, total=False):
type: Required[Literal['definition-ref']]
schema_ref: Required[str]
ref: str
metadata: Any
serialization: SerSchema


def definition_reference_schema(
schema_ref: str, metadata: Any = None, serialization: SerSchema | None = None
schema_ref: str, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> DefinitionReferenceSchema:
"""
Returns a schema that points to a schema stored in "definitions", this is useful for nested recursive
Expand All @@ -3606,7 +3607,9 @@ def definition_reference_schema(
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(type='definition-ref', schema_ref=schema_ref, metadata=metadata, serialization=serialization)
return _dict_not_none(
type='definition-ref', schema_ref=schema_ref, ref=ref, metadata=metadata, serialization=serialization
)


MYPY = False
Expand Down
10 changes: 10 additions & 0 deletions tests/validators/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ def test_use_after():
)
)
assert v.validate_python(['1', '2']) == (1, 2)


def test_definition_chain():
v = SchemaValidator(
core_schema.definitions_schema(
core_schema.definition_reference_schema('foo'),
[core_schema.definition_reference_schema(ref='foo', schema_ref='bar'), core_schema.int_schema(ref='bar')],
),
)
assert v.validate_python('1') == 1
0