This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to have multiple body schemas? #432
Comments
I'm also interested in support of union types in requests. In my use-case a model has an attribute class Bicycle(BaseModel):
# attributes specific for bicycle here
type: str = 'bicycle'
class Car(BaseModel):
# attributes specific for car here
type: str = 'car'
class MyRequest(BaseModel):
vehicle: Union[Bicycle, Car] In there example above I would like {
"vehicle": {
"type": "bicycle",
"break_type": "hydraulic"
}
} |
As I understand it depends on pydantic and I found the following ticket there: pydantic/pydantic#619 |
Thanks for the discussion here @inikolaev . @jonDel maybe it was a bug at some point and it was fixed in recent versions, because this seems to work: from fastapi import FastAPI
from typing import Union
from pydantic import BaseModel
class User(BaseModel):
name: str
class Item(BaseModel):
size: int
price: float
app = FastAPI()
@app.post("/multi/")
def process_things(body: Union[User, Item]):
return body Still, if you want to have single fields ( |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
It's works! but sample request body is empty in swagger How to set default request body? |
I have tried the approach. Dynamic schema detection is not happening here. The current approach is working with basic datatypes like int, float and all but user-defined structures or pydantic models not working. It is considering the object in basic datatype like dict or list. Thanks in advance |
I am facing the same issue. Is there already a github issue for this open? |
I am getting EDIT: Sorry for the created confusion, it seems the input data changed a little - the body was not passing validation. Everything is working as expected once I updated the schema. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Description
In a Response, according to the documentation, I can have a Union of multiple pydantic models as the response of an endpoint operation. That works fine. However, it seems that there is no way to use a similar approach when receiving input in a a body. My problem: I have an endpoint, and this endpoint could receive any of 4 different models as valid input in the body. I tried to define a Union, and it breaks the fastapi schema generation; I tried to use Any, from pydantic, and fastapi treats the input as a query operation instead of a body operation (the same happens if I don't use any model at all)
Consider Foobar, Model1 and Model2 as pydantic classes.
The following breaks schema generation:
The following makes fastapi understand bar as a query:
The following makes fastapi understand bar as a query either:
Is there a way to fix this behavior? Or simply force fastapi to understand "bar" as body and NOT a query parameter?
The text was updated successfully, but these errors were encountered: