New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression: FastAPI request fails when query param defined as pydantic model with alias being a python keyword #4467
Comments
@ospikovets Does that example work with previous versions of Pydantic? I tested Just out of curiosity, I haven't seen that usage in FastAPI docs, is that supported? |
@aminalaee just verified with both The example works well.
However, once I install
As to the support of such a behavior in FastAPI. It is not explicitly mentioned that a pydantic model can be used as dependency, however, there is a section with an example of usage for classes as dependencies in the context of query parameters. https://fastapi.tiangolo.com/tutorial/dependencies/classes-as-dependencies/ Additionally, as mentioned above, the example works for both Lastly, if I change the |
This is caused by #4011, I can confirm the behaviour change however I do not think it's really a bug - but rather an unexpected side effect of a reasonable change. Workarounds:
@tiangolo any thoughts? |
This is actually a workaround/hack, it is not officially supported in FastAPI, and the combination of Pydantic generating an automatic I intend to support something similar, as a future feature, but it would look different. More like: from fastapi import FastAPI
from fastapi import Query
from pydantic import BaseModel
app = FastAPI()
class Params(BaseModel):
from_year: int = Field(default=2022, alias="from")
@app.get("/")
def get_error(params: Params = Query()):
return {"from_year": params.from_year}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", reload=True) So that it would behave similar to using |
@tiangolo this looks like an excellent feature, that will solve this problem! Thank you! |
Great! I think we could move this to the FastAPI repo and close this issue here, as it would be more FastAPI-specific. |
👍 |
Initial Checks
Description
Starting from
pydantic
version1.10.0
, FastAPI request fails when query param defined aspydantic
model with alias being a python keyword. The request returns422
status with the response body:Similar behavior with exactly the same error message was also seeing for
pydantic
1.9.2
in case, query param alias contained a non valid python variable name, like:There is a related issue in FastAPI repository: tiangolo/fastapi#4842
However, with
pydantic >= 1.10.0
the issue also started to occur when query param alias is a valid python keyword.For the context:
fastapi==0.81.0
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: