OpenAPI schema: Missing description with Query parameters #8348
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI, APIRouter, Depends
import uvicorn
from typing import List
from fastapi import Query
from pydantic import BaseModel, Field
import os
class A(BaseModel):
foo: str = Field(
description="This is Foo",
)
class B(A):
bar: str = Field(
description="This is Bar with alias",
alias="bar2",
)
class C(B):
def __init__(self, xyz: str = Query(description="This is Xyz")):
self.xyz = xyz
app = FastAPI(docs_url=None, redoc_url="/redocs")
rtr_api = APIRouter(prefix="/api")
@rtr_api.get(
"/test1",
description="Query me",
summary="Querio-matic 1",
response_model=List[B],
)
async def test(bdata: B = Depends()):
return [bdata]
@rtr_api.get(
"/test2",
description="Query me 2",
summary="Querio-matic 2",
response_model=List[B],
)
async def test2(bdata: B):
return [bdata]
@rtr_api.get(
"/test3",
description="Query me 3",
summary="Querio-matic 3",
response_model=List[C],
)
async def test3(bdata: C = Depends()):
return [bdata]
app.include_router(rtr_api)
if __name__ == "__main__":
uvicorn.run(
app,
port=int(os.environ.get("PORT", "8000")),
host=os.environ.get("HOST", "localhost"),
debug=True,
)Description
To test
While this usage is not documented, I would be hoping to use Classes inheritance to define query parameters and reuse descriptions. This does not seem possible without adding it manually to the schema. Yet I was still unsure if it's an expected behavior since Thank you for your help Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.79.0 Python VersionPython 3.9.12 Additional ContextOpenAPI JSON file: You can see the difference between
[
{
"required": true,
"schema": {
"title": "Foo",
"type": "string"
},
"name": "foo",
"in": "query"
},
{
"required": true,
"schema": {
"title": "Bar2",
"type": "string"
},
"name": "bar2",
"in": "query"
}
]
[
{
"description": "This is Xyz",
"required": true,
"schema": {
"title": "Xyz",
"type": "string",
"description": "This is Xyz"
},
"name": "xyz",
"in": "query"
}
] |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
There's already registered relevant issue at #4700 |
Beta Was this translation helpful? Give feedback.
-
|
This appears to be not a fully supported function yet #11037 (comment)
With some attempted work rounds
|
Beta Was this translation helpful? Give feedback.
-
|
Since FastAPI 0.115.0 you can oficially use Pydantic models to define Query parameters. from typing import List
from fastapi import FastAPI, Query
from pydantic import BaseModel, Field
class A(BaseModel):
foo: str = Field(description="This is Foo")
class B(A):
bar: str = Field(
description="This is Bar with alias",
alias="bar2",
)
app = FastAPI(docs_url=None, redoc_url="/redocs")
@app.get("/test1", response_model=List[B])
async def test(bdata: B = Query()):
return [bdata]
@app.get("/test2", response_model=List[B])
async def test2(bdata: B):
return [bdata] |
Beta Was this translation helpful? Give feedback.

Since FastAPI 0.115.0 you can oficially use Pydantic models to define Query parameters.
And descriptions work fine: