From 8364920acb272e2df5c6e261ca67622f3d6d1874 Mon Sep 17 00:00:00 2001 From: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> Date: Fri, 2 Feb 2024 13:19:03 -0600 Subject: [PATCH] Reverting problematic fix from 2.6 release, fixing schema building bug (#8718) --- pydantic/_internal/_generate_schema.py | 2 -- tests/test_discriminated_union.py | 1 + tests/test_json_schema.py | 28 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pydantic/_internal/_generate_schema.py b/pydantic/_internal/_generate_schema.py index fef419ac82..f6e80244dd 100644 --- a/pydantic/_internal/_generate_schema.py +++ b/pydantic/_internal/_generate_schema.py @@ -438,8 +438,6 @@ def clean_schema(self, schema: CoreSchema) -> CoreSchema: if collect_invalid_schemas(schema): raise self.CollectedInvalid() schema = validate_core_schema(schema) - if 'definitions' in schema: - schema['definitions'] = list(reversed(schema['definitions'])) return schema def collect_definitions(self, schema: CoreSchema) -> CoreSchema: diff --git a/tests/test_discriminated_union.py b/tests/test_discriminated_union.py index fb12e2d661..0e3a0eeadb 100644 --- a/tests/test_discriminated_union.py +++ b/tests/test_discriminated_union.py @@ -1700,6 +1700,7 @@ class DiscriminatedModel(BaseModel): assert exc_info.code == 'callable-discriminator-no-tag' +@pytest.mark.xfail(reason='Issue not yet fixed, see: https://github.com/pydantic/pydantic/issues/8271.') def test_presence_of_discriminator_when_generating_type_adaptor_json_schema_definitions() -> None: class ItemType(str, Enum): ITEM1 = 'item1' diff --git a/tests/test_json_schema.py b/tests/test_json_schema.py index 7a6488ad18..9a7671e52e 100644 --- a/tests/test_json_schema.py +++ b/tests/test_json_schema.py @@ -5879,3 +5879,31 @@ class Model(BaseModel): x: BaseModel assert 'description' not in Model.model_json_schema()['$defs']['BaseModel'] + + +def test_recursive_json_schema_build() -> None: + """ + Schema build for this case is a bit complicated due to the recursive nature of the models. + This was reported as broken in https://github.com/pydantic/pydantic/issues/8689, which was + originally caused by the change made in https://github.com/pydantic/pydantic/pull/8583, which has + since been reverted. + """ + + class AllowedValues(str, Enum): + VAL1 = 'Val1' + VAL2 = 'Val2' + + class ModelA(BaseModel): + modelA_1: AllowedValues = Field(..., max_length=60) + + class ModelB(ModelA): + modelB_1: typing.List[ModelA] + + class ModelC(BaseModel): + modelC_1: ModelB + + class Model(BaseModel): + b: ModelB + c: ModelC + + assert Model.model_json_schema()