[Feature request] Possibility to use dependency class name for Body instead of generated name #10023
-
First Check
Commit to Help
Example Codefrom dataclasses import dataclass
from typing import Annotated
from fastapi import Depends, FastAPI, File, Form, UploadFile
from pydantic import BaseModel, TypeAdapter
api = FastAPI()
@dataclass
class Test:
a1: Annotated[str, Form()]
a2: Annotated[str, Form()]
a3: Annotated[UploadFile, File()]
TestDep = Annotated[Test, Depends()]
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class ItemOut(BaseModel):
item_name: str
@api.get("/items")
async def read_items(t: TestDep) -> ItemOut:
item = TypeAdapter(ItemOut).validate_python(fake_items_db[0])
return ItemOut.model_validate(item)DescriptionWhen we use non-JSON body parameters, e.g. If we use a user-defined class ( Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.101.0 Pydantic Version2.1.1 Python VersionPython 3.11.4 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I think this is not possible. The This is why FastAPI creates different request body schemas for every endpoint and uses the endpoint function name and http-method to construct the name of this schema. You can modify the way those name are being created using counter = {"request_body": 0}
def generate_unique_id(route: APIRoute) -> str:
counter["request_body"] += 1
return str(counter["request_body"])
app = FastAPI(generate_unique_id_function=generate_unique_id)By default this one is used: Lines 179 to 184 in b2d7420 |
Beta Was this translation helpful? Give feedback.

I think this is not possible.
The
Body_read_items_items_getthat you see in openapi is the schema of whole request body of the endpoint.Following the way you described, if we use this type in several endpoints, the request body schema should be named the same way (
TestDep?).But what if second endpoint includes other
Formparameters along with this dependency? All these parameters should be included into the request body schema.This is why FastAPI creates different request body schemas for every endpoint and uses the endpoint function name and http-method to construct the name of this schema.
You can modify the way those name are being created using
generate_unique_id_functionparameter:…