OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Python-Fastapi] Enum parameters typed as string instead of enum
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
When generating a FastAPI endpoint that receives an enum parameter, the value is not properly validated before being passed to the implementation. Currently, the parameter is typed as a string instead of using an enum class, and the enum class is not generated.
openapi-generator version
v7.9.0
OpenAPI declaration file content or url
https://gist.github.com/reyescabello/6cd965708fd0b03e8eadb9f3c0c0a899#file-query_param_example-yml
Generation Details
Generated with jar:
java -jar openapi-generator-cli.jar generate -i openapi.yml -g python-fastapi -o openapi_server
Steps to reproduce
The generated code is:
@router.get(
"/example",
responses={
200: {"model": QuizPaged, "description": "OK"},
400: {"description": ""},
401: {"description": ""},
403: {"description": ""},
500: {"description": ""},
},
tags=["Example"],
summary="Example endpoint",
response_model_by_alias=True,
)
async def example_get(
status: str = Query(None, description="", alias="status"),
) -> QuizPaged:
return await BaseExampleApi.subclasses[0]().example_get(status)
The expected code is something like this:
class StatusEnum(str, Enum):
CANDIDATE = 'CANDIDATE'
DRAFT = 'DRAFT'
COMPLETED = 'COMPLETED'
REVIEW_REQUIRED = 'REVIEW_REQUIRED'
WITHDRAWN = 'WITHDRAWN'
@router.get(
"/example",
responses={
200: {"model": QuizPaged, "description": "OK"},
400: {"description": ""},
401: {"description": ""},
403: {"description": ""},
500: {"description": ""},
},
tags=["Example"],
summary="Example endpoint",
response_model_by_alias=True,
)
async def example_get(
status: StatusEnum = Query(None, description="", alias="status"),
) -> QuizPaged:
return await BaseExampleApi.subclasses[0]().example_get(status)
Related issues/PRs
I think this may be related to this other issue about query params typing:
https://github.com/OpenAPITools/openapi-generator/issues/20115
Suggest a fix
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 using the OpenAPI declaration at the linked gist and the shown Java generation command to reproduce the python-fastapi output. Compare the generated endpoint with the expected StatusEnum example; done means the enum class is generated and the endpoint parameter uses it so FastAPI validates enum values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, openapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100