question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Is it possible to have multiple body schemas?

See original GitHub issue

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:

@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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ritikjain51commented, Feb 22, 2022

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

1reaction
inikolaevcommented, Dec 13, 2019

As I understand it depends on pydantic and I found the following ticket there: https://github.com/samuelcolvin/pydantic/issues/619

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple representations of the body schema for the same ...
The present study reveals that there are multiple representations of the body schema for the same body part. The body schema has been...
Read more >
How to reuse schemas when required properties change ...
Make two schemas. One schema would be your base schema. The other would reference the base schema and use an allOf to "add"...
Read more >
Swagger; specify two responses with same code based on ...
In OAS3 you can use oneOf to define multiple possible request bodies or response bodies for the same operation:
Read more >
OpenAPi 3.0.3 - can I use two POST methods for the same path
OpenAPI only allows one POST/GET/etc. operation per path, but you can define multiple possible request bodies by using oneOf:
Read more >
Understanding JSON Schema
There are many examples throughout this book, and they all follow the same format. At the beginning of each example is a short...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found