[BUG] In version 0.115.0 of FastAPI, the pydantic model that has declared an alias cannot correctly receive query parameters #12401
-
First Check
Commit to Help
Example Codeimport uvicorn
from typing import Literal
from fastapi import FastAPI, Query
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
app = FastAPI()
class FilterParams(BaseModel):
model_config = ConfigDict(alias_generator=to_camel)
limit: int = Field(100, gt=0, le=100)
offset: int = Field(0, ge=0)
order_by: Literal['created_at', 'updated_at'] = 'created_at'
tags: list[str] = []
@app.get('/items/')
async def read_items(filter_query: FilterParams = Query()):
return filter_query
if __name__ == '__main__':
uvicorn.run(app='app:app')DescriptionRunning the code in the example above, I encountered an incorrect result when accessing http://127.0.0.1:8000/items/?offset=1&orderBy=updated_at in the browser, orderBy did not receive successfully. The correct result should be as follows Operating SystemWindows Operating System DetailsNo response FastAPI Version0.115.0 Pydantic Version2.9.2 Python Version3.9.19 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
@insistence I think this is the same problem as with if value is not None:
params_to_process[field.alias] = value # change field.name to field.aliasCan you patch a local fastapi install with that change and give it a try? With that change, everything works in my case, both with or without |
Beta Was this translation helpful? Give feedback.
-
|
There is an issue for this. Let's track it there: #12402 |
Beta Was this translation helpful? Give feedback.
There is an issue for this. Let's track it there: #12402