OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Supply only payload schema for FastAPI endpoint without actual validation by Pydantic

  • Thread starter Thread starter Альберт Александров
  • Start date Start date
А

Альберт Александров

Guest
I need to supply only payload schema for API endpoint without any Pydantic validation. I'm using:

  • FastAPI==0.110.1
  • Pydantic v2

Code:
from typing import Literal, Annotated
    
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field, RootModel

app = FastAPI()


def get_openapi_extra(model) -> dict:
    return {
        "requestBody": {
            "content": {"application/json": {"schema": model.schema(ref_template="#/components/schemas/{model}")}}
        }
    }


class Cat(BaseModel):
    pet_type: Literal['cat']
    meows: int


class Dog(BaseModel):
    pet_type: Literal['dog']
    barks: float


class Lizard(BaseModel):
    pet_type: Literal['reptile', 'lizard']
    scales: bool


class Model(RootModel):
    root: Annotated[Cat | Dog | Lizard, Field(..., discriminator='pet_type')]


@app.get("/", openapi_extra=get_openapi_extra(Model))
def index():
    return "ok"

When I was using Pydantic v1 and FastAPI==0.98.0 it worked without problems. But when I started to upgrade to Pydantic v2 and FastAPI 0.110.1 I started facing the next problem:

enter image description here

It seems that FastAPI does not know about Lizard, Dog and Cat schemas...

How to fix it?
<p>I need to supply only payload schema for API endpoint without any <code>Pydantic</code> validation. I'm using:</p>
<ul>
<li><code>FastAPI==0.110.1</code></li>
<li><code>Pydantic v2</code></li>
</ul>
<pre class="lang-py prettyprint-override"><code>from typing import Literal, Annotated

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field, RootModel

app = FastAPI()


def get_openapi_extra(model) -> dict:
return {
"requestBody": {
"content": {"application/json": {"schema": model.schema(ref_template="#/components/schemas/{model}")}}
}
}


class Cat(BaseModel):
pet_type: Literal['cat']
meows: int


class Dog(BaseModel):
pet_type: Literal['dog']
barks: float


class Lizard(BaseModel):
pet_type: Literal['reptile', 'lizard']
scales: bool


class Model(RootModel):
root: Annotated[Cat | Dog | Lizard, Field(..., discriminator='pet_type')]


@app.get("/", openapi_extra=get_openapi_extra(Model))
def index():
return "ok"
</code></pre>
<p>When I was using <code>Pydantic v1</code> and <code>FastAPI==0.98.0</code> it worked without problems. But when I started to upgrade to <code>Pydantic v2</code> and <code>FastAPI 0.110.1</code> I started facing the next problem:</p>
<p><a href="https://i.sstatic.net/fnZT506t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fnZT506t.png" alt="enter image description here" /></a></p>
<p>It seems that FastAPI does not know about Lizard, Dog and Cat schemas...</p>
<p>How to fix it?</p>
 

Latest posts

L
Replies
0
Views
1
Lavínia Beghini
L
Top