Skip to content

Use Request instead of Pydantic model in POST, but still document the pydanti model #11473

Closed Answered by YuriiMotov
achempak-polymer asked this question in Questions
Discussion options

You must be logged in to vote

You can declare every path operation twice, but disable including in openapi schema for one of them. Only first operation will be executed

from fastapi import FastAPI, Request
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str


# This operation will NOT be included in openapi schema, but it will be executed
@app.post("/", include_in_schema=False)
async def route_real(request: Request):
    print("real")
    body = (await request.body()).decode("utf-8")
    parsed = Item.model_validate_json(body)
    print(parsed.model_dump())

# This operation will be included in openapi schema, but it will NOT be executed
@app.post("/")
def route_fake(item: Item):
   …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by achempak-polymer
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
4 participants