Skip to content

fix: Fail when duplicate names are generated #336

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 9 commits into from
Feb 10, 2021
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
6 changes: 6 additions & 0 deletions openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions tests/test_parser/test_properties/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down