8000 ✨ Add support for Pydantic models for parameters using `Query`, `Cookie`, `Header` by tiangolo · Pull Request #12199 · fastapi/fastapi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

✨ Add support for Pydantic models for parameters using Query, Cookie, Header #12199

Merged
merged 32 commits into from
Sep 17, 2024

Conversation

tiangolo
Copy link
Member

✨ Add support for Pydantic models for parameters using Query, Cookie, Header

@tiangolo tiangolo added the feature New feature or request label Sep 13, 2024
Co-authored-by: Nico Tonnhofer <github@wurstnase.de>
Copy link
Contributor
@Wurstnase Wurstnase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking forward for this PR. This will makes some parts much more simple.

Thanks in advance 🤗

docs_src/query_param_models/tutorial001.py Show resolved Hide resolved
docs_src/query_param_models/tutorial001.py Show resolved Hide resolved
@tiangolo tiangolo marked this pull request as ready for review September 17, 2024 18:53
@tiangolo tiangolo merged commit 55035f4 into master Sep 17, 2024
50 checks passed
@tiangolo tiangolo deleted the query-models branch September 17, 2024 18:54
@oogetyboogety
Copy link
oogetyboogety commented Sep 26, 2024

@tiangolo is there a way we can work together on extending support for
a) https://fastapi.tiangolo.com/tutorial/header-params/#automatic-conversion for CommonHeaders, docs show the underscores still remain underscores:

class CommonHeaders(BaseModel):
    model_config = {"extra": "forbid"}

    host: str
    save_data: bool
    if_modified_since: str | None = None
    traceparent: str | None = None
    x_tag: list[str] = []

image01
I realized you were saying that the usability of this functionality in Depends works, but coincidentally works.
I would still like for automatic conversion to happen on these Pydantic parameters models though, even if included via Depends.
We have noticed some odd behavior (input parameter becomes a json object) when trying to use Depends and Header model together. It works as expected by only including "repeated" header parameter in Depends. Perhaps we could add tests for these repeated parameters when they are also in Pydantic model.

8000

JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Oct 17, 2024
…der and cookie

From fastapi#12199 onwards, Pydantic models are supported for query, cookie and
header parameters. When one parameter is present, the model is flattened
in the OpenAPI spec, but when multiple are defined, they aren't.

This is confusing, and results in a confusing OpenAPI spec. Since these
arguments are used in flattened form anyway, it makes more sense to
flatten all of them.
@ollie-bell
Copy link
ollie-bell commented Oct 24, 2024

Is it possible to mix "vanilla" query parameters with the new model parameters introduced in this PR?

e.g. the example from the docs no longer works with an additional other_param: str = "foo" (and forbidding extra parameters in the model doesn't fix it).

GET http://127.0.0.1:8000/items/
{"detail":[{"type":"missing","loc":["query","filter_query"],"msg":"Field required","input":null}]}
from typing import Annotated, Literal

from fastapi import FastAPI, Query
from pydantic import BaseModel, Field

app = FastAPI()


class FilterParams(BaseModel):
    model_config = {"extra": "forbid"}

    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: Annotated[FilterParams, Query()], other_param: str = "foo"):
    return filter_query

s-rigaud pushed a commit to s-rigaud/fastapi that referenced this pull request Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0