You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
openapi-generator generate -i openapi.yml -g python-fastapi -o server
This generates something like this
@router.post("/pets",responses={201: {"description": "Created"}, },tags=["default"],summary="Add a new pet",response_model_by_alias=True,)asyncdefpets_post(
pet: Annotated[Pet, Field(description="Optional description in *Markdown*")] =Body(None, description="Optional description in *Markdown*"),
) ->None:
note the pet field value: Body(None, description="Optional description in *Markdown*"), the Body has None as a default argument, so FastAPI marks that as optional.
The expected field value should be something like just Body(description="Optional description in *Markdown*") so pydantic marks that as required
Note: the Path param uses something like Path(..., description="description"), pydantic used to require ... as an argument to mark the field as a required, but this is not required in pydantic's newer versions, just omit that first default argument. See: https://stackoverflow.com/a/74884189
Related issues/PRs
Something similar has been done previously with the Path param #17532, but this fix is not exactly the same (the parameter should not always be required as the path param, but rely on requestBody.required property to set the default value to None or not)
When settings the first argument as None, we must check if the requestBody.required property is set to true, and in that case, omit the first argument entirely.
The text was updated successfully, but these errors were encountered:
I'm open to contribute to fix this, but I may need help to know how can I access the requestBody.required property inside the .mustache file. In which template variable should be that available?
Should that required attribute be accessible in the mustache file and maybe we can do something like {{^required}}None{{/required}}?
jorgehermo9
changed the title
[BUG] [PYTHON-FASTAPI] requestBody.required = true generates and optional body
[BUG] [PYTHON-FASTAPI] required request body generates an optional body
May 7, 2025
Uh oh!
There was an error while loading. Please reload this page.
Bug Report Checklist
Description
When defining a
requestBody
as required via therequestBody.required: true
property in openapi (https://swagger.io/docs/specification/v3_0/describing-request-body/describing-request-body/#requestbody-content-and-media-types), I expect the generated server to generate a required body. However, the generated server sets the default value of that parameter toNone
, so FastAPI marks that body as optional.Pydantic reference: https://fastapi.tiangolo.com/tutorial/body/#create-your-data-model

openapi-generator version
7.12.0
I don't think this is a regression, as looking through the git history, didn't seem to have ever worked
OpenAPI declaration file content or url
Generation Details
This generates something like this
note the
pet
field value:Body(None, description="Optional description in *Markdown*")
, theBody
hasNone
as a default argument, so FastAPI marks that as optional.The expected field value should be something like just
Body(description="Optional description in *Markdown*")
so pydantic marks that as requiredNote: the
Path
param uses something likePath(..., description="description")
, pydantic used to require...
as an argument to mark the field as a required, but this is not required in pydantic's newer versions, just omit that first default argument. See: https://stackoverflow.com/a/74884189Related issues/PRs
Something similar has been done previously with the Path param #17532, but this fix is not exactly the same (the parameter should not always be required as the path param, but rely on
requestBody.required
property to set the default value toNone
or not)Suggest a fix
The problem is in this file:
https://github.com/OpenAPITools/openapi-generator/blob/f2813716fb815dae0e5bc6b2219452b3303f4934/modules/openapi-generator/src/main/resources/python-fastapi/endpoint_argument_definition.mustache
And in the definition of the
Body
parameterWhen settings the first argument as
None
, we must check if therequestBody.required
property is set to true, and in that case, omit the first argument entirely.The text was updated successfully, but these errors were encountered: