OpenAPITools / OpenAPITools/openapi-generator
[BUG] [PYTHON] Schema name normalization collision generates invalid model imports
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When using snake case names for OpenAPI components such as operation IDs and schema names, the Python generator seems to normalize segments that contain a single character by combining them (e.g. test_s_t becomes test_st).
This can also lead to name collisions, which can break generation for otherwise valid schemas. While there seems to be a fallback for colliding operation IDs, when schema names collide in this way, only one of the schemas is generated and the API code attempts to import the missing model.
openapi-generator version
v7.25.0
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Schema Name Collision Reproducer
version: 1.0.0
paths:
/first:
get:
operationId: get_first_model
responses:
'200':
description: First model
content:
application/json:
schema:
$ref: '#/components/schemas/test_st'
/second:
get:
operationId: get_second_model
responses:
'200':
description: Second model
content:
application/json:
schema:
$ref: '#/components/schemas/test_s_t'
components:
schemas:
test_st:
type: object
properties:
first:
type: string
test_s_t:
type: object
properties:
second:
type: integer
Generation Details
From the languages I tested, this issue only seems to occur with Python.
Steps to reproduce
Generate a Python client using the provided OpenAPI declaration.
Only one TestSt model is generated, while default_api.py contains an invalid import for TestST:
from openapi_client.models.test_st import TestST
from openapi_client.models.test_st import TestSt
There is no TestST model generated, so the resulting client cannot be imported.
The issue can also be observed in the generated response type for the second operation, which references TestST even though only TestSt exists.
Related issues/PRs
Suggest a fix
I am not familiar enough with the generator code to suggest an appropriate fix, but applying the same fallback solution used for operation IDs to schema models would at least fix the specific issue described here.
This case is obviously unlikely to occur naturally, but a simple workaround is to rename the affected components so that their normalized model names do not collide.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied OpenAPI declaration through the Python generator and inspecting the generated models and default_api.py. Trace the normalization and collision handling for test_st and test_s_t; done means both schemas have distinct generated models and the API imports and response types reference existing models.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100