diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index 2ea75a569..992d726df 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -302,6 +302,12 @@ def build_model_property( name=name, additional_properties=additional_properties, ) + if prop.reference.class_name in schemas.models: + error = PropertyError( + data=data, detail=f'Attempted to generate duplicate models with name "{prop.reference.class_name}"' + ) + return error, schemas + schemas = attr.evolve(schemas, models={**schemas.models, prop.reference.class_name: prop}) return prop, schemas diff --git a/tests/test_parser/test_properties/test_init.py b/tests/test_parser/test_properties/test_init.py index a7ea05881..ee07d3973 100644 --- a/tests/test_parser/test_properties/test_init.py +++ b/tests/test_parser/test_properties/test_init.py @@ -1105,6 +1105,31 @@ def test_build_model_property(additional_properties_schema, expected_additional_ ) +def test_build_model_property_conflict(): + from openapi_python_client.parser.properties import Schemas, build_model_property + + data = oai.Schema.construct( + required=["req"], + properties={ + "req": oai.Schema.construct(type="string"), + "opt": oai.Schema(type="string", format="date-time"), + }, + nullable=False, + ) + schemas = Schemas(models={"OtherModel": None}) + + err, new_schemas = build_model_property( + data=data, + name="OtherModel", + schemas=schemas, + required=True, + parent_name=None, + ) + + assert new_schemas == schemas + assert err == PropertyError(detail='Attempted to generate duplicate models with name "OtherModel"', data=data) + + def test_build_model_property_bad_prop(): from openapi_python_client.parser.properties import Schemas, build_model_property