Skip to content
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

Validation of pydantic models in query params #11243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mylius
Copy link

@mylius mylius commented Mar 4, 2024

If a pydantic model is supplied as params and auto cast via fastapi.Depends() the models config is ignored:

Take a look at this example:

app = FastAPI()

class NoExtraFields(BaseModel):
    model_config = ConfigDict(extra="forbid")
    a: int
    b: int

@app.get("/")
def foo(foo: NoExtraFields = Depends()):
    return foo

client = TestClient(app)

response = client.get("/", params={"a": 1, "b": 2, "c": 3})
assert response.status_code == 200, response.text

This assert will pass, since the config is ignored. This is a result of all the params field getting validated separately in solve_dependencies instead of together as is done with body_params.

While there are workarounds as discussed here, this is not a satisfactory state considering the otherwise great compatibility with pydantic.

This PR addresses this by doing a validation for the model as a whole for query params as well.

@mylius mylius changed the title Jgm/fastapi/validate pydantic model Validation of pydantic models in query params Mar 4, 2024
@tiangolo tiangolo added p4 feature New feature or request labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request p4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants