Type of the Query param not shown in /docs #10781
-
First Check
Commit to Help
Example Codefrom typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: Annotated[str | None, Query()] = None):
query_items = {"q": q}
return query_itemsDescriptionHi, since v.0.103 I have been noticing that query params that uses the Annotated typing, does not display the type of the param in the /docs. On /redoc works flawlessly. I also have tried the example shown on the documentation and the type of the query paramer is not shown on /docs. Good news are that if you declare the param like I tried to detect if the error is on FastAPI, Pydantic or even Swagger UI but I can't see were the type of a parameter is passed, in my opinion some knowledge about this will enrich the discussion! Thanks a lot! Operating SystemWindows Operating System DetailsNo response FastAPI Version0.105.0 Pydantic Version2.42 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
So, what you see in As far as I can tell, you get the same OpenAPI spec with or without annotations: from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items-1/")
async def read_items_1(q: str | None = Query(None)):
query_items = {"q": q}
return query_items
@app.get("/items-2/")
async def read_items_2(q: Annotated[str | None, Query()] = None):
query_items = {"q": q}
return query_items
print(app.openapi()['paths']['/items-1/']['get']['parameters'])
#> [{'name': 'q', 'in': 'query', 'required': False, 'schema': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'title': 'Q'}}]
print(app.openapi()['paths']['/items-2/']['get']['parameters'])
#> [{'name': 'q', 'in': 'query', 'required': False, 'schema': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'title': 'Q'}}](Generated with fastapi v0.103.2) So I don't think this has to do with the use of annotations. It looks like the default Swagger-UI version was updated in fastapi 0.99.0 and in 0.104.1. (You want to look at the default value of If you notice a difference in behavior in |
Beta Was this translation helpful? Give feedback.
So, what you see in
/docsor/redocis going to be based on how those pages choose to render the OpenAPI schema.As far as I can tell, you get the same OpenAPI spec with or without annotations: