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
I used the GitHub search to find a similar question and didn't find it.
I searched the FastAPI documentation, with the integrated search.
I already searched in Google "How to X in FastAPI" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to FastAPI but to Pydantic.
I already checked if it is not related to FastAPI but to Swagger UI.
I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromfastapiimportFastAPI, Responseapp=FastAPI()
classModelA(BaseModel):
name: strclassModelB(BaseModel):
nameA: ModelAclassModelC(BaseModel):
nameB: ModelB@app.post("/", status_code=201,responses={208: {"description": "Already exists","model": ModelC}}, )defpost_root(response: Response) ->ModelC:
ifTrue: # for example if already existsresponse.status_code=208returnModelC(nameB=ModelB(nameA=ModelA(name="name")))
else:
# createreturnModelC(nameB=ModelB(nameA=ModelA(name="name")))
uvicorn.run(app, host="localhost", port=8000)
Description
probelm
When a model is nested with 2 levels or more and there are multiple responses faulty OpenApi schema is generated:
a redundant ModelC-Input and ModelC-Output are generated instead of just one ModelC
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
probelm
When a model is nested with 2 levels or more and there are multiple responses faulty OpenApi schema is generated:
a redundant
ModelC-Input
andModelC-Output
are generated instead of just oneModelC
other test cases
if model is nested 2 level but with single response the result is correct :
the resulting model is correct (ie only
ModelC
created):if model is nested only 1 level with 2 responses the result is correct:
resulting model is correct:
Operating System
Linux
Operating System Details
any OS
FastAPI Version
0.111.0
Pydantic Version
2.7.4
Python Version
3.12.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions