3

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:

  1. 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

  2. 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."

  3. I have tried following Chris's answer but the inner object (b: AnotherBase) is for some reason in the body section (which does not exist)

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.