Intro:
In FastAPI, when the input of a POST request is a Pydantic model (like in this example) The automatic docs generated by Fastapi contains a schema of that object ($ref) (as shown here).
Question:
Although the input of a GET request cannot be a Pydantic model (because Pydantic objects need to be sent inside the body section of the request, and get requests does not have a body - link,
Q1. Is it possible to use a Pydantic model for the auto generated docs? Edit - Yes: the answer is in Chris's response here
Q2. Is it possible to do that with nested Pydantic objects (to flatten the fields)? Something like this:
class AnotherBase(BaseModel):
c: str
d: str
class MyBase(BaseModel):
a: str = Query()
b: AnotherBase = Query()
@router.get("/myget")
def my_func(arg: MyBase = Depends()) -> MyBase:
return MyBase(a="1", b="2")
What I have tried:
To use a pydantic model as the input of a get request - the result was that it is not possible because get requests does not have a body
I have tried following this thread but still, using Depends, some of the input parameters need to be sent inside the body - so I receive this error from Fastapi "TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body."
I have tried following Cris's answer1 but the inner object (b: AnotherBase) is for some reason in the body section (which does not exists)