Skip to content

Commit 2bfe610

Browse files
fix: Indicate an error when duplicate models are detected rather than generating bad code (#336)
1 parent 2833602 commit 2bfe610

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

openapi_python_client/parser/properties/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ def build_model_property(
302302
name=name,
303303
additional_properties=additional_properties,
304304
)
305+
if prop.reference.class_name in schemas.models:
306+
error = PropertyError(
307+
data=data, detail=f'Attempted to generate duplicate models with name "{prop.reference.class_name}"'
308+
)
309+
return error, schemas
310+
305311
schemas = attr.evolve(schemas, models={**schemas.models, prop.reference.class_name: prop})
306312
return prop, schemas
307313

tests/test_parser/test_properties/test_init.py

+25
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,31 @@ def test_build_model_property(additional_properties_schema, expected_additional_
11051105
)
11061106

11071107

1108+
def test_build_model_property_conflict():
1109+
from openapi_python_client.parser.properties import Schemas, build_model_property
1110+
1111+
data = oai.Schema.construct(
1112+
required=["req"],
1113+
properties={
1114+
"req": oai.Schema.construct(type="string"),
1115+
"opt": oai.Schema(type="string", format="date-time"),
1116+
},
1117+
nullable=False,
1118+
)
1119+
schemas = Schemas(models={"OtherModel": None})
1120+
1121+
err, new_schemas = build_model_property(
1122+
data=data,
1123+
name="OtherModel",
1124+
schemas=schemas,
1125+
required=True,
1126+
parent_name=None,
1127+
)
1128+
1129+
assert new_schemas == schemas
1130+
assert err == PropertyError(detail='Attempted to generate duplicate models with name "OtherModel"', data=data)
1131+
1132+
11081133
def test_build_model_property_bad_prop():
11091134
from openapi_python_client.parser.properties import Schemas, build_model_property
11101135

0 commit comments

Comments
 (0)