-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root(
q: int | None = Query(None),
):
return qDescriptionOpen Operating SystemLinux Operating System DetailsNo response FastAPI Version0.105.0 Pydantic Version2.5.3 Python Version3.11.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
|
Use Annotated like from fastapi import FastAPI, Query
from typing import Annotated
app = FastAPI()
@app.get("/")
def root(q: Annotated[int, Query()] = None):
return q |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo
I don't think FastAPI(or maybe OpenAPI) has a way to accept null in queries. |
Beta Was this translation helpful? Give feedback.
-
|
So I came across this issue from the docs here: which say that: from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: Annotated[list[str] | None, Query()] = None):
query_items = {"q": q}
return query_itemsshould produce an array in Swagger UI/OpenAPI, which did not happen. remove the optional "None" type hint: from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: Annotated[list[str], Query()] = None):
query_items = {"q": q}
return query_itemsor downgrade pydantic:
However the current docs situation is quite bad for this, even if this is not a FastAPI issue originally. Using Redoc UI at |
Beta Was this translation helpful? Give feedback.



Thanks, I got it. And it helps me to find several issues about it and one hack solution.
Using
SkipJsonSchemaremoves anyOf and null from openapi.json and swagger shows parameter correctly"parameters":[{"name":"q","in":"query","required":false,"schema":{"type":"integer","title":"Q"}}]#9709 (reply in thread)
pydantic/pydantic#6653 (comment)
It seems like swagger doesn't support anyOf for parameters