Inconsistency in openapi.json
Generation for Annotated[list[str] | None]
in FastAPI
#10654
-
First Check
Commit to Help
Example Code# Failing
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_items
# Working
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_items DescriptionI've encountered an issue with FastAPI's Swagger UI not displaying the "Add item" button for a async def read_items(q: Annotated[list[str] | None, Query()] = None): In this case, the Swagger UI does not show the expected "Add item" button for the parameter To understand the underlying issue, I compared the generated --- /tmp/with_none.json 2023-11-15 21:44:21
+++ /tmp/without_none.json 2023-11-15 21:44:34
@@ -15,10 +15,17 @@
"in": "query",
"required": false,
"schema": {
- "type": "array",
- "items": {
- "type": "string"
- },
+ "anyOf": [
+ {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ },
+ {
+ "type": "null"
+ }
+ ],
"title": "Q"
}
} In the version with Operating SystemLinux Operating System DetailsDebian GNU/Linux 12 (bookworm) FastAPI Version0.104.1 Pydantic Version2.4.2 Python VersionPython 3.12.0 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
I have noticed the same thing. |
Beta Was this translation helpful? Give feedback.
-
@tiangolo 🙏🙏 |
Beta Was this translation helpful? Give feedback.
-
+1 I also just ran into this bug |
Beta Was this translation helpful? Give feedback.
-
From here #10836 (reply in thread) Use: from typing import Annotated
from fastapi import FastAPI, Query
from pydantic.json_schema import SkipJsonSchema
app = FastAPI()
@app.get('/items')
async def read_items(q: Annotated[list[str] | SkipJsonSchema[None], Query()] = None):
query_items = {"q": q}
return query_items |
Beta Was this translation helpful? Give feedback.
-
@Kludex just tagging you cause you marked the previous comment as the answer, do I need to raise an issue? Or is that the actual real solution? If yes, can the docs be updated? |
Beta Was this translation helpful? Give feedback.
From here #10836 (reply in thread)
Use: