[QUESTION] Is it possible to have multiple body schemas?
See original GitHub issueDescription
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:
@router.post("/{foobar}")
def post_foobar(foobar: Foobar, bar: Union[Model1, Model2], response: Response):
The following makes fastapi understand bar as a query:
@router.post("/{foobar}")
def post_foobar(foobar: Foobar, bar: Any, response: Response):
The following makes fastapi understand bar as a query either:
@router.post("/{foobar}")
def post_foobar(foobar: Foobar, bar, response: Response):
Is there a way to fix this behavior? Or simply force fastapi to understand “bar” as body and NOT a query parameter?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (3 by maintainers)
Top GitHub Comments
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
As I understand it depends on pydantic and I found the following ticket there: https://github.com/samuelcolvin/pydantic/issues/619