OpenAPITools / OpenAPITools/openapi-generator
[BUG][PYTHON] Generator creates Enums with duplicate attributes
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 (example)?
- 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
Generator creates Enums with duplicate attributes
openapi-generator version
openapi-generator v7.8.0
OpenAPI declaration file content or url
{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/status": {
"get": {
"summary": "Get Status",
"operationId": "get_status_status_get",
"parameters": [
{
"name": "Status",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/StatusEnum"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"StatusEnum": {
"type": "string",
"enum": [
"ACTIVE",
"active"
],
"title": "StatusEnum"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"type": "array",
"title": "Location"
},
"msg": {
"type": "string",
"title": "Message"
},
"type": {
"type": "string",
"title": "Error Type"
}
},
"type": "object",
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError"
}
}
}
}
where the important part is:
"StatusEnum": {
"type": "string",
"enum": [
"ACTIVE",
"active"
],
"title": "StatusEnum"
}
Generation Details
openapi-generator generate -i openapi_example.json -g python --package-name example -o .
Steps to reproduce
Create a simple FastAPI:
import json
from enum import Enum
from fastapi import FastAPI, APIRouter
app = FastAPI()
router = APIRouter()
class StatusEnum(str, Enum):
ACTIVE = "ACTIVE"
ENUM_ACTIVE = "active"
@router.get("/status")
async def get_status(Status: StatusEnum):
return {"status": Status.value}
def write_openap_spec(app):
openapi_schema = app.openapi()
with open("openapi_example.json", "w") as f:
json.dump(openapi_schema, f, indent=2)
if __name__ == "__main__":
app.include_router(router)
write_openap_spec(app)
and generate the PythonClient with the generator, resulting in the following Enum in Python:
# coding: utf-8
"""
FastAPI
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
The version of the OpenAPI document: 0.1.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
""" # noqa: E501
from __future__ import annotations
import json
from enum import Enum
from typing_extensions import Self
class StatusEnum(str, Enum):
"""
StatusEnum
"""
"""
allowed enum values
"""
ACTIVE = 'ACTIVE'
ACTIVE = 'active'
@classmethod
def from_json(cls, json_str: str) -> Self:
"""Create an instance of StatusEnum from a JSON string"""
return cls(json.loads(json_str))
where we retrieve the duplicate attribute ACTIVE in the StatusEnum.
Related issues/PRs
None
Suggest a fix
Add
@Setter protected boolean allEnumValuesUpperCase = true;
in AbstractPythonCodegen.java and the regarding logic to handle the case where enums shouldn't be capitalized.
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 with AbstractPythonCodegen.java and reproduce the issue using the supplied OpenAPI declaration and openapi-generator generate -i openapi_example.json -g python --package-name example -o . command. Trace how the ACTIVE and active enum values become Python attributes. Done means the generated StatusEnum no longer contains duplicate attribute names while preserving both enum values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100