Skip to content

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

Merged
tiangolo merged 32 commits intomasterfrom
query-models
Sep 17, 2024
Merged

✨ Add support for Pydantic models for parameters using Query, Cookie, Header#12199
tiangolo merged 32 commits intomasterfrom
query-models

Conversation

@tiangolo
Copy link
Copy Markdown
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
Comment thread docs/en/docs/tutorial/cookie-param-models.md Outdated
Copy link
Copy Markdown
Contributor

@tonnico tonnico 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 🤗

Comment thread docs_src/query_param_models/tutorial001.py
Comment thread docs_src/query_param_models/tutorial001.py
@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
@tiangolo tiangolo deleted the query-models branch September 17, 2024 18:54
@oogetyboogety
Copy link
Copy Markdown

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.

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
Copy Markdown

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
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Mar 25, 2025
…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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jul 11, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Oct 28, 2025
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jan 7, 2026
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jan 7, 2026
…g Query, Cookie and Header

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.
JSCU-CNI added a commit to JSCU-CNI/fastapi that referenced this pull request Jan 7, 2026
…g Query, Cookie and Header

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.
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