Pydantic Wrapped List Query Parameters in Body Instead of Query
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import List, Optional
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel
app = FastAPI()
class Querymodel(BaseModel):
problematic: Optional[List[str]] = Query([])
okay: Optional[str] = Query(None)
@app.get("/")
async def root(select: List[str] = Query(None), queries: Querymodel = Depends()):
return {"select": select, "queries": queries}
Description
Re-production:
-
Open the browser and navigate to the swagger or redoc page (/docs or /redoc)
-
See that the get root endpoint offers the query parameters: select and okay
-
See that the request has a body that contains the default value of the problematic Query object
-
Execute a
GET
request to:/?select=string&select=string&okay=hello&problematic=string&problematic=string
-
See that the problematic field is returned empty, but all others are correctly populated
Expected Behavior:
- All 3 Query parameters are shown and are usable.
Operating System
Linux
Operating System Details
Ubuntu 20.04.3 LTS
FastAPI Version
0.72.0
Python Version
3.8.12
Additional Context
The intent was to wrap a significant number of query parameters (some of which are lists) inside of a pydantic model in order to reduce the size of the method signature. Unwrapping the Query objects into the signature results in the expected behavior, of all query parameters being present.
I’ve looked at https://github.com/tiangolo/fastapi/issues/321 as it seems to be the closest to my question, but am unable to identify further parallels than use of the explicit Query object declaration.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:6
Top GitHub Comments
I also have this as an issue. I hesitate to call it bug, because i also cannot really find this in the documentation. I found it through StackOverflow and for me it really solves a problem.
From my understanding, the issue is somewhere in the
fastapi.dependencies.utils.get_dependant
function, where somehow the details are lost when adding the sub_dependant. Anyhow, i don’t know FastApi well enough to commit a pull request for this.As a workaround, one can simply wrap the
Query()
call in aField()
call (give it as default parameter toField
). This way, theQuery
survives and is added as expected also with details like the description or examples.So, this may work for you:
I also drafted a simple metaclass to automate this, hoping to make it easier to remove later, in case this is solved in FastApi.
I faced the same issue and that comment: https://github.com/tiangolo/fastapi/issues/2869#issuecomment-786729484 helped me. Dataclasses solved this problem. But don’t inherit dataclasses from Pydantic models, as said in docs. If you override class attributes, somehow in init order of inheritance is lost, so your instance get attributes like they are in parent class (not overrided).