From ef663cf3f97c63df04ba3789a4334965ef6243c9 Mon Sep 17 00:00:00 2001 From: lyn1dev <78355114+lyn1dev@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:37:13 +0100 Subject: [PATCH 1/3] fix: Generate conflicting class names into separate modules Class names are turned into module names by snake_casing them, so schemas named `VLAN` and `Vlan` were both generated into `vlan.py`. The second module silently overwrote the first one, but `models/__init__.py` still imported both classes, so the generated client could not be imported at all. Give every model and enum a module name that no other generated class is using, appending a numeric suffix to conflicts, and emit a warning describing the rename. --- README.md | 5 ++ .../test_conflicting_module_names.py | 43 ++++++++++++++ .../parser/properties/enum_property.py | 3 +- .../properties/literal_enum_property.py | 3 +- .../parser/properties/model_property.py | 3 +- .../parser/properties/schemas.py | 48 ++++++++++++++++ .../test_properties/test_schemas.py | 57 ++++++++++++++++++- 7 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py diff --git a/README.md b/README.md index bdaf6bfce..ffea3183b 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,11 @@ class_overrides: The easiest way to find what needs to be overridden is probably to generate your client and go look at everything in the `models` folder. +Model and enum modules are named after their class names, so two schemas whose class names only differ by +capitalization or punctuation would end up in the same module (for example, `VLAN` and `Vlan` would both be generated +into `vlan.py`). The conflicting class is generated into a module with a numeric suffix instead (such as `vlan_1.py`) +and a warning is emitted. You can use `class_overrides` to choose a more meaningful module name in that case. + ### docstrings_on_attributes By default, when `openapi-python-client` generates a model class, it includes a list of attributes and their diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py b/end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py new file mode 100644 index 000000000..af1b9b537 --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py @@ -0,0 +1,43 @@ +from end_to_end_tests.functional_tests.helpers import ( + assert_model_decode_encode, + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( + """ +components: + schemas: + VLAN: + type: object + properties: + vlanId: {"type": "integer"} + required: ["vlanId"] + Vlan: + type: object + properties: + name: {"type": "string"} + required: ["name"] + Container: + type: object + properties: + upper: {"$ref": "#/components/schemas/VLAN"} + lower: {"$ref": "#/components/schemas/Vlan"} + required: ["upper", "lower"] +""" +) +@with_generated_code_imports( + ".models.VLAN", + ".models.Vlan", + ".models.Container", +) +class TestConflictingModuleNames: + def test_both_models_are_generated(self, VLAN, Vlan, Container): + assert_model_decode_encode(VLAN, {"vlanId": 1}, VLAN(vlan_id=1)) + assert_model_decode_encode(Vlan, {"name": "a"}, Vlan(name="a")) + assert_model_decode_encode( + Container, + {"upper": {"vlanId": 1}, "lower": {"name": "a"}}, + Container(upper=VLAN(vlan_id=1), lower=Vlan(name="a")), + ) diff --git a/openapi_python_client/parser/properties/enum_property.py b/openapi_python_client/parser/properties/enum_property.py index 573d5f0d8..2b0f2d255 100644 --- a/openapi_python_client/parser/properties/enum_property.py +++ b/openapi_python_client/parser/properties/enum_property.py @@ -13,7 +13,7 @@ from ..errors import PropertyError from .none import NoneProperty from .protocol import PropertyProtocol, Value, convert_example -from .schemas import Class, Schemas +from .schemas import Class, Schemas, ensure_unique_module_name from .union import UnionProperty ValueType = str | int @@ -119,6 +119,7 @@ def build( # noqa: PLR0911 if parent_name: class_name = f"{strings.pascal_case(parent_name)}{strings.pascal_case(class_name)}" class_info = Class.from_string(string=class_name, config=config) + class_info = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) var_names = data.model_extra.get("x-enum-varnames", []) if data.model_extra else [] values = EnumProperty.values_from_list(value_list, class_info, var_names) diff --git a/openapi_python_client/parser/properties/literal_enum_property.py b/openapi_python_client/parser/properties/literal_enum_property.py index 0388fe04e..5b71bf690 100644 --- a/openapi_python_client/parser/properties/literal_enum_property.py +++ b/openapi_python_client/parser/properties/literal_enum_property.py @@ -13,7 +13,7 @@ from ..errors import PropertyError from .none import NoneProperty from .protocol import PropertyProtocol, Value, convert_example -from .schemas import Class, Schemas +from .schemas import Class, Schemas, ensure_unique_module_name from .union import UnionProperty ValueType = str | int @@ -118,6 +118,7 @@ def build( # noqa: PLR0911 if parent_name: class_name = f"{strings.pascal_case(parent_name)}{strings.pascal_case(class_name)}" class_info = Class.from_string(string=class_name, config=config) + class_info = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) values: set[str | int] = set(value_list) if class_info.name in schemas.classes_by_name: diff --git a/openapi_python_client/parser/properties/model_property.py b/openapi_python_client/parser/properties/model_property.py index 3f6548e77..43807ba19 100644 --- a/openapi_python_client/parser/properties/model_property.py +++ b/openapi_python_client/parser/properties/model_property.py @@ -11,7 +11,7 @@ from ..errors import ParseError, PropertyError from .any import AnyProperty from .protocol import PropertyProtocol, Value, convert_example -from .schemas import Class, ReferencePath, Schemas, parse_reference_path +from .schemas import Class, ReferencePath, Schemas, ensure_unique_module_name, parse_reference_path @define @@ -75,6 +75,7 @@ def build( else: class_string = title class_info = Class.from_string(string=class_string, config=config) + class_info = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) model_roots = {*roots, class_info.name} required_properties: list[Property] | None = None optional_properties: list[Property] | None = None diff --git a/openapi_python_client/parser/properties/schemas.py b/openapi_python_client/parser/properties/schemas.py index 49e278437..ad71bfcf4 100644 --- a/openapi_python_client/parser/properties/schemas.py +++ b/openapi_python_client/parser/properties/schemas.py @@ -102,6 +102,54 @@ def add_dependencies(self, ref_path: ReferencePath, roots: set[ReferencePath | C self.dependencies[ref_path].update(roots) +def ensure_unique_module_name(*, class_info: Class, schemas: Schemas, config: Config) -> Class: + """Give a class a module name that no other schema in this document is using. + + Different class names can be turned into the same module name, since module names are snake_case versions of + class names (`VLAN` and `Vlan` both become `vlan`). Without this, both classes would be generated into the same + module, where the second one silently overwrites the first one and the `models` package fails to import. + + Args: + class_info: The class for which a module name is needed. + schemas: Existing Schemas which have already been processed (to check module name conflicts). + config: Config data for this run of the generator, used for renaming. + + Returns: + Either `class_info` unchanged, or a copy of it with a unique module name. + """ + if class_info.name in schemas.classes_by_name: + # Classes with the same name conflict in a more fundamental way, which the callers already report; + # renaming their module would not help and would make the error messages harder to understand. + return class_info + + used_module_names: set[str] = set() + for existing in schemas.classes_by_name.values(): + # Only models and enums have a module of their own; `getattr` is used instead of an `isinstance` check + # because importing those property classes here would be circular. + existing_class_info = getattr(existing, "class_info", None) + if isinstance(existing_class_info, Class): + used_module_names.add(existing_class_info.module_name) + + module_name = class_info.module_name + if module_name not in used_module_names: + return class_info + + suffix = 1 + while f"{module_name}_{suffix}" in used_module_names: + suffix += 1 + new_module_name = PythonIdentifier(f"{module_name}_{suffix}", config.field_prefix) + schemas.errors.append( + ParseError( + header="Renamed a conflicting module", + detail=( + f"The module name `{module_name}` is already used by another schema, so `{class_info.name}` " + f"will be generated in `{new_module_name}.py` instead of overwriting it." + ), + ) + ) + return evolve(class_info, module_name=new_module_name) + + def update_schemas_with_data( *, ref_path: ReferencePath, data: oai.Schema, schemas: Schemas, config: Config ) -> Schemas | PropertyError: diff --git a/tests/test_parser/test_properties/test_schemas.py b/tests/test_parser/test_properties/test_schemas.py index 8433404d2..75d6d534a 100644 --- a/tests/test_parser/test_properties/test_schemas.py +++ b/tests/test_parser/test_properties/test_schemas.py @@ -6,13 +6,15 @@ from openapi_python_client.parser.properties import Class, Parameters from openapi_python_client.parser.properties.schemas import ( ReferencePath, + Schemas, + ensure_unique_module_name, parameter_from_data, parameter_from_reference, ) from openapi_python_client.schema import Parameter, ParameterLocation, Reference, Schema from openapi_python_client.schema.ref import Ref from openapi_python_client.schema.untrusted_string import UntrustedString -from openapi_python_client.strings import ClassName +from openapi_python_client.strings import ClassName, PythonIdentifier MODULE_NAME = "openapi_python_client.parser.properties.schemas" @@ -106,3 +108,56 @@ def test_returns_reference_from_registry(self): param_by_ref = Reference.model_construct(ref=Ref("#/components/parameters/a_param")) param_or_error = parameter_from_reference(param=param_by_ref, parameters=params) assert param_or_error == existing_param + + +class TestEnsureUniqueModuleName: + def test_keeps_module_name_when_there_is_no_conflict(self, config): + class_info = Class.from_string(string="/components/schemas/PingResponse", config=config) + schemas = Schemas() + + result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) + + assert result is class_info + assert schemas.errors == [] + + def test_keeps_module_name_when_the_class_name_already_exists(self, config, model_property_factory): + existing = model_property_factory( + class_info=Class(name=ClassName(UntrustedString("Vlan"), ""), module_name=PythonIdentifier("vlan", "")) + ) + schemas = Schemas(classes_by_name={existing.class_info.name: existing}) + class_info = Class.from_string(string="/components/schemas/Vlan", config=config) + + result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) + + assert result is class_info + assert schemas.errors == [] + + def test_renames_a_conflicting_module_name(self, config, model_property_factory): + existing = model_property_factory( + class_info=Class(name=ClassName(UntrustedString("VLAN"), ""), module_name=PythonIdentifier("vlan", "")) + ) + schemas = Schemas(classes_by_name={existing.class_info.name: existing}) + class_info = Class.from_string(string="/components/schemas/Vlan", config=config) + + result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) + + assert result.name == "Vlan" + assert result.module_name == "vlan_1" + assert len(schemas.errors) == 1 + assert "will be generated in `vlan_1.py`" in schemas.errors[0].detail + + def test_keeps_looking_until_it_finds_an_unused_suffix(self, config, model_property_factory): + class_infos = ( + Class(name=ClassName(UntrustedString("VLAN"), ""), module_name=PythonIdentifier("vlan", "")), + Class(name=ClassName(UntrustedString("Vlan1"), ""), module_name=PythonIdentifier("vlan_1", "")), + ) + schemas = Schemas( + classes_by_name={ + class_info.name: model_property_factory(class_info=class_info) for class_info in class_infos + } + ) + class_info = Class.from_string(string="/components/schemas/Vlan", config=config) + + result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) + + assert result.module_name == "vlan_2" From 54aa62ab9e378f32d34b580c93cd46fb9ef32a10 Mon Sep 17 00:00:00 2001 From: lyn1dev <78355114+lyn1dev@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:56:18 +0100 Subject: [PATCH 2/3] refactor: Track used module names on `Schemas` Looking up the module name of every already-parsed class for each new class made parsing quadratic in the number of schemas. Keep the used module names in a set on `Schemas` instead. --- openapi_python_client/parser/properties/schemas.py | 12 ++++-------- tests/test_parser/test_properties/test_schemas.py | 14 ++++++++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/openapi_python_client/parser/properties/schemas.py b/openapi_python_client/parser/properties/schemas.py index ad71bfcf4..63eeb8213 100644 --- a/openapi_python_client/parser/properties/schemas.py +++ b/openapi_python_client/parser/properties/schemas.py @@ -90,6 +90,7 @@ class Schemas: classes_by_name: dict[ClassName, Property] = field(factory=dict) models_to_process: list[ModelProperty] = field(factory=list) errors: list[ParseError] = field(factory=list) + used_module_names: set[str] = field(factory=set) def add_dependencies(self, ref_path: ReferencePath, roots: set[ReferencePath | ClassName]) -> None: """Record new dependencies on the given ReferencePath @@ -122,22 +123,17 @@ class names (`VLAN` and `Vlan` both become `vlan`). Without this, both classes w # renaming their module would not help and would make the error messages harder to understand. return class_info - used_module_names: set[str] = set() - for existing in schemas.classes_by_name.values(): - # Only models and enums have a module of their own; `getattr` is used instead of an `isinstance` check - # because importing those property classes here would be circular. - existing_class_info = getattr(existing, "class_info", None) - if isinstance(existing_class_info, Class): - used_module_names.add(existing_class_info.module_name) - + used_module_names = schemas.used_module_names module_name = class_info.module_name if module_name not in used_module_names: + used_module_names.add(module_name) return class_info suffix = 1 while f"{module_name}_{suffix}" in used_module_names: suffix += 1 new_module_name = PythonIdentifier(f"{module_name}_{suffix}", config.field_prefix) + used_module_names.add(new_module_name) schemas.errors.append( ParseError( header="Renamed a conflicting module", diff --git a/tests/test_parser/test_properties/test_schemas.py b/tests/test_parser/test_properties/test_schemas.py index 75d6d534a..89c01decf 100644 --- a/tests/test_parser/test_properties/test_schemas.py +++ b/tests/test_parser/test_properties/test_schemas.py @@ -113,18 +113,21 @@ def test_returns_reference_from_registry(self): class TestEnsureUniqueModuleName: def test_keeps_module_name_when_there_is_no_conflict(self, config): class_info = Class.from_string(string="/components/schemas/PingResponse", config=config) - schemas = Schemas() + schemas = Schemas(used_module_names={"other_model"}) result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) assert result is class_info + assert schemas.used_module_names == {"other_model", "ping_response"} assert schemas.errors == [] def test_keeps_module_name_when_the_class_name_already_exists(self, config, model_property_factory): existing = model_property_factory( class_info=Class(name=ClassName(UntrustedString("Vlan"), ""), module_name=PythonIdentifier("vlan", "")) ) - schemas = Schemas(classes_by_name={existing.class_info.name: existing}) + schemas = Schemas( + classes_by_name={existing.class_info.name: existing}, used_module_names={existing.class_info.module_name} + ) class_info = Class.from_string(string="/components/schemas/Vlan", config=config) result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) @@ -136,7 +139,9 @@ def test_renames_a_conflicting_module_name(self, config, model_property_factory) existing = model_property_factory( class_info=Class(name=ClassName(UntrustedString("VLAN"), ""), module_name=PythonIdentifier("vlan", "")) ) - schemas = Schemas(classes_by_name={existing.class_info.name: existing}) + schemas = Schemas( + classes_by_name={existing.class_info.name: existing}, used_module_names={existing.class_info.module_name} + ) class_info = Class.from_string(string="/components/schemas/Vlan", config=config) result = ensure_unique_module_name(class_info=class_info, schemas=schemas, config=config) @@ -154,7 +159,8 @@ def test_keeps_looking_until_it_finds_an_unused_suffix(self, config, model_prope schemas = Schemas( classes_by_name={ class_info.name: model_property_factory(class_info=class_info) for class_info in class_infos - } + }, + used_module_names={class_info.module_name for class_info in class_infos}, ) class_info = Class.from_string(string="/components/schemas/Vlan", config=config) From 0402d9498e7f71f6cf6b6679e26ad517b5446550 Mon Sep 17 00:00:00 2001 From: lyn1dev <78355114+lyn1dev@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:58:59 +0100 Subject: [PATCH 3/3] docs: Document the new `Schemas.used_module_names` field --- openapi_python_client/parser/properties/schemas.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openapi_python_client/parser/properties/schemas.py b/openapi_python_client/parser/properties/schemas.py index 63eeb8213..d172c21bc 100644 --- a/openapi_python_client/parser/properties/schemas.py +++ b/openapi_python_client/parser/properties/schemas.py @@ -90,6 +90,7 @@ class Schemas: classes_by_name: dict[ClassName, Property] = field(factory=dict) models_to_process: list[ModelProperty] = field(factory=list) errors: list[ParseError] = field(factory=list) + # Module names of the models and enums generated so far, see `ensure_unique_module_name`. used_module_names: set[str] = field(factory=set) def add_dependencies(self, ref_path: ReferencePath, roots: set[ReferencePath | ClassName]) -> None: