How to handle multiple pydantic response in fast api #7255
-
|
Describe the bug When using Union on response_model, it doesn't return accurate result Create three modelsoutput when cond 2 gets true then when cond 3 gets true then Expected behavior cond 1 and 2 output are correct how can i get the output to have all the fields of dict3 i am new to fast api and can anyone help me with this issue, any resolution to this issue or any other way to get the Additional Info: |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
|
When you specify the response model in the decorator, eg: The line return child3(**dict3).dict()is what is causing the problem here. Do not convert it to a What you need to be doing: return child3(**dict3) |
Beta Was this translation helpful? Give feedback.
-
|
Hi @Rushyanth111
|
Beta Was this translation helpful? Give feedback.
-
|
Ok, I tested this once again, and it seems to be true, the output @gunjeetgulatigit is getting is valid: {
"id": "1",
"code": "213",
"user": null
}When I solved the problem by Introducing type-annotations into the pydantic Models. So I converted your models a little: from typing import Dict
class parent(BaseModel):
id: str
class child1(parent):
code: str
user: Dict[str, str] #Note here the Use of Dict[str, str]
class child2(parent):
error: Any #Should be str, really, Using Any is not advisable.
class child3(parent):
code: str
user_data: Dict[str, str] #Note here the Use of Dict[str, str]Changing to the appropriate types ensured the output is what you expect. I'm a little confused as to how that happened, since the keys are obviously different, if anyone else could elucidate on that, I'd be happy. |
Beta Was this translation helpful? Give feedback.
-
|
thanks @Rushyanth111 |
Beta Was this translation helpful? Give feedback.
-
|
Currently, it is a known missing feature within the Swagger UI (See: #1083 ) and the generation of the example cannot be automatically done by Swagger UI, however it is possible to provide a manual example, which would mean that you have to make an example to be displayed. Link to the Relevant Issue Solution from #1083 Link to Another Solution from #86 The Example Shows you that you need to provide a manual example like so: Create A Class that holds the Union of any of the three Classes: class parent(BaseModel):
id: str
class child1(parent):
code: str
user: Dict[str, str]
class child2(parent):
error: Any
class child3(parent):
code: str
user_data: Dict[str, str]
class UnionChild(BaseModel): # This will Hold the Union
Child: Union[child1, child2, child3] #The Union Required
class Config: #This is inside UnionChild, do not place it outside.
schema_extra = {
"example": { #Mandatory field, this holds your example. Define Your Field from here.
"id": "1",
"error": "Some Random String",
"user": "OSAS UVUWE",
"user_data": {"oh": "oh_no"},
}
}
app = FastAPI()
@app.get("/verify", response_model=UnionChild)
async def something(some: int):
pass |
Beta Was this translation helpful? Give feedback.
-
|
thanks @Rushyanth111 |
Beta Was this translation helpful? Give feedback.
-
|
Hi @Rushyanth111 Is there any way to show multiple response scheme at swagger UI through this manual example step .. ? |
Beta Was this translation helpful? Give feedback.
-
|
@gunjeetgulatigit Yes, the example is indicative of your needs, you should edit the schema, according to the Open API Documentations, as it is manually set. As a small side note, please close the issue if the help required is satisfactory. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @Rushyanth111 |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @Rushyanth111 ! 👏 🙇 Thanks for reporting back and closing the issue @gunjeetgulatigit 👍 |
Beta Was this translation helpful? Give feedback.
-
|
try this it works |
Beta Was this translation helpful? Give feedback.
Currently, it is a known missing feature within the Swagger UI (See: #1083 ) and the generation of the example cannot be automatically done by Swagger UI, however it is possible to provide a manual example, which would mean that you have to make an example to be displayed.
Link to the Relevant Issue Solution from #1083
Link to Another Solution from #86
The Example Shows you that you need to provide a manual example like so:
Create A Class that holds the Union of any of the three Classes: