Skip to content

Commit

Permalink
fix: ensure module names are unique for models
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdd committed Nov 19, 2024
1 parent 40d63f9 commit 500bdbc
Showing 1 changed file with 20 additions and 0 deletions.
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

0 comments on commit 500bdbc

Please sign in to comment.