Skip to content

fix: Generate conflicting class names into separate modules - #1523

Open
lyn1dev wants to merge 3 commits into
openapi-generators:mainfrom
lyn1dev:fix/conflicting-module-names
Open

lyn1dev wants to merge 3 commits into
openapi-generators:mainfrom
lyn1dev:fix/conflicting-module-names

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026 •

Copy link
Copy Markdown

What was wrong

Module names are snake_case versions of class names, so schemas named VLAN and Vlan were both generated into
models/vlan.py. _build_models writes that file once per class, so the second model overwrote the first one, while
models/__init__.py still imported both classes from models.vlan:

from .vlan import VLAN, Vlan

Importing the generated client then failed with ImportError: cannot import name 'VLAN' from '...models.vlan'.

Fix

ensure_unique_module_name now runs whenever a model or enum class is created: if another generated class already
uses the resulting module name, the new class gets a numeric suffix (vlan_1, vlan_2, ...) and a warning is emitted,
as suggested in the issue:

Renamed a conflicting module

The module name `vlan` is already used by another schema, so `Vlan` will be generated in `vlan_1.py` instead of overwriting it.

The rename happens before the class is added to Schemas, so every import generated for references, endpoints and
models/__init__.py uses the final module name. Two classes with the exact same name keep the existing behaviour,
since those are reported as duplicate model/enum errors.

The class_overrides section of the README documents the new suffixing behaviour.

Tests

  • Added tests/test_parser/test_properties/test_schemas.py::TestEnsureUniqueModuleName for the helper: no conflict,
    class name already present, conflict renamed, and picking the next free suffix when _1 is already taken.
  • Added an end-to-end test that generates a client from a spec with VLAN, Vlan and a model referencing both, then
    imports the three generated models and round-trips data through them
    (end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py).

The end-to-end test fails without the fix and passes with it:

# without the fix
E   ImportError: cannot import name 'VLAN' from 'testapi_client.models.vlan'
1 error in 2.02s

# with the fix
1 passed in 5.50s

Full suite, lint, format and type checks were run on Windows. Two test__run_post_hooks_* tests fail there because
python3 resolves to the Microsoft Store alias on that platform; the same two fail on an unmodified checkout:

$ pytest tests end_to_end_tests
2 failed, 470 passed, 4 skipped in 92.15s

$ pytest tests end_to_end_tests/test_end_to_end.py end_to_end_tests/functional_tests --basetemp=tests/tmp --cov openapi_python_client --cov-report=term-missing
2 failed, 474 passed in 183.16s
Name                                                                             Stmts   Miss  Cover   Missing
openapi_python_client\parser\properties\schemas.py                                 101      0   100%
TOTAL                                                                             2876      0   100%

$ ruff check .
All checks passed!

$ ruff format . --check
143 files already formatted

$ mypy openapi_python_client fuzz
Success: no issues found in 68 source files

Fixes #1163

AI was used to write this code.

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.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conflicting module names for models silently generate invalid code

1 participant