Cannot use simple models derived from str (with Pydantic RootModel) as query params #11101
-
First Check
Commit to Help
Example Code# fail.py
# uvicorn fail:app --reload
from fastapi import FastAPI
from pydantic import RootModel
SimpleID = RootModel[str]
app = FastAPI()
@app.get("/test1")
async def t1(param1: SimpleID): # BUG: expected to be query param, but it is body
return {"message": f"Hello {param1}"}
# @app.get("/test2")
# async def t2(param1: Annotated[SimpleID, Query()]): # this workaround does NOT work
# return {"message": f"Hello {param1}"}
@app.get("/test3")
async def t3(param1: SimpleID, param2: str): # param2 is query param as expected, param1 is NOT
return {"message": f"Hello {param1} {param2}"} DescriptionSimple types should be usable as query params. Pydantic provides Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.108.0 Pydantic Version2.5.3 Python VersionPython 3.11.4 Additional ContextI am not sure this is possible, but I would like for FastAPI to understand RootModel derived types, if the root type is one of the types that can be used as query param, then it should work as such. |
Beta Was this translation helpful? Give feedback.
#11134