[Bug] Using Annotated[Optional[str], Query(alias="q")] = None prevents type from being shown for parameter in Swagger UI
#11179
Answered
by
JavierSanchezCastro
caffeinatedMike
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom typing import Annotated, Optional
import uvicorn
from fastapi import APIRouter, FastAPI, Query
api = APIRouter(prefix="/api")
@api.get("/a")
async def a(
x: Annotated[str, Query()] = None,
y: Annotated[int, Query()] = None,
z: Annotated[bool, Query()] = False
):
"""Properly shows parameter types in Swagger UI, but MyPy complains."""
@api.get("/b")
async def b(
x: Annotated[Optional[str], Query()] = None,
y: Annotated[Optional[int], Query()] = None,
z: Annotated[Optional[bool], Query()] = False
):
"""Satisfies MyPy, but fails to show parameter types in Swagger UI."""
app = FastAPI(docs_url="/")
app.include_router(api)
if __name__ == "__main__":
uvicorn.run("__main__:app", host="0.0.0.0", port=8000)Description
Operating SystemWindows Operating System DetailsNo response FastAPI Version0.109.2 Pydantic Version2.6.1 Python VersionPython 3.9.13 Additional ContextNew way with Annotated, without
|
Beta Was this translation helpful? Give feedback.
Answered by
JavierSanchezCastro
Feb 21, 2024
Replies: 1 comment 2 replies
-
|
Try this #10836 (reply in thread) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
caffeinatedMike
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Try this #10836 (reply in thread)