Skip to content
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

fix: ensure module names are unique for models #1165

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions openapi_python_client/parser/properties/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def parse_reference_path(ref_path_raw: str) -> Union[ReferencePath, ParseError]:
return cast(ReferencePath, parsed.fragment)


EXISTING_MODULE_NAMES: set[PythonIdentifier] = set()

def _unique_module_name(candidate_name: PythonIdentifier) -> PythonIdentifier:
if candidate_name not in EXISTING_MODULE_NAMES:
EXISTING_MODULE_NAMES.add(candidate_name)
return candidate_name

counter = 0
while True:
new_name = f"{candidate_name}{counter}"

if new_name not in EXISTING_MODULE_NAMES:
EXISTING_MODULE_NAMES.add(new_name)
return new_name

counter += 1


@define
class Class:
"""Represents Python class which will be generated from an OpenAPI schema"""
Expand All @@ -67,7 +85,9 @@ def from_string(*, string: str, config: Config) -> "Class":
module_name = override.module_name
else:
module_name = class_name

module_name = PythonIdentifier(module_name, config.field_prefix)
module_name = _unique_module_name(module_name)

return Class(name=class_name, module_name=module_name)

Expand Down