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
10 changes: 10 additions & 0 deletions openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def _string_based_property(
)


class NameClashException(Exception):
pass


def build_model_property(
*, data: oai.Schema, name: str, schemas: Schemas, required: bool, parent_name: Optional[str]
) -> Tuple[Union[ModelProperty, PropertyError], Schemas]:
Expand Down Expand Up @@ -302,6 +306,9 @@ def build_model_property(
name=name,
additional_properties=additional_properties,
)
if prop.reference.class_name in schemas.models:
raise NameClashException(f'Attempted to generate duplicate models with name "{prop.reference.class_name}"')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return a PropertyError here instead so our higher level error handling code can manage it rather than printing a stack trace.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍. Thanks for the review!


schemas = attr.evolve(schemas, models={**schemas.models, prop.reference.class_name: prop})
return prop, schemas

Expand Down Expand Up @@ -374,6 +381,9 @@ def build_enum_property(
values=values,
value_type=value_type,
)
if prop.reference.class_name in schemas.enums:
raise NameClashException(f'Attempted to generate duplicate enums with name "{prop.reference.class_name}"')

schemas = attr.evolve(schemas, enums={**schemas.enums, prop.reference.class_name: prop})
return prop, schemas

Expand Down