fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

✨ Allow to have multiple Query parameter models

Open uriyyo opened this issue 1 year ago • 11 comments

Currently, it's allowed to have only one model as a query/headers/etc model parameter, in this PR I propose to remove this restriction. This PR should be backward-compatible.

For instance, example below will not work:

from typing import Annotated

from pydantic import BaseModel

from fastapi import FastAPI, Query

app = FastAPI()


class FilterParams(BaseModel):
    name: str
    age: int


class PageParams(BaseModel):
    limit: int
    offset: int


@app.get("/")
async def route(
    filters: Annotated[FilterParams, Query()],
    page: Annotated[PageParams, Query()],
):
    return {}

And same here:

from typing import Annotated

from pydantic import BaseModel

from fastapi import FastAPI, Query

app = FastAPI()


class FilterParams(BaseModel):
    name: str
    age: int


@app.get("/")
async def route(
    filters: Annotated[FilterParams, Query()],
    limit: Annotated[int, Query()],
    offset: Annotated[int, Query()],
):
    return {}

WIth this PR both examples above will work as expected. Also, it will generate the correct OpenAPI schema: image

Discussion - https://github.com/fastapi/fastapi/discussions/12212

uriyyo avatar Nov 16 '24 11:11 uriyyo

This looks similar to #12481

wu-clan avatar Nov 17 '24 05:11 wu-clan

I agree, It does solve the same issue. I didn't see it when I started working on this PR(

uriyyo avatar Nov 17 '24 09:11 uriyyo

Hope this gets approved and released soon !

jperuggia avatar Jan 28 '25 18:01 jperuggia

This looks awesome! Really looking forward to it! Do we know if/when it'll be approved and released?

Mulugruntz avatar Mar 05 '25 09:03 Mulugruntz

Do we know when this will be published? It would make things easier.

Vaserty avatar Mar 15 '25 12:03 Vaserty

Just merged with master and fixed tests, @Kludex any chance you can take a look on this PR? 🙏

uriyyo avatar Aug 05 '25 09:08 uriyyo

@Ale-Cas can you please take a look at this ?

bassemkaroui avatar Aug 12 '25 22:08 bassemkaroui

is this merging soon? would be nice to have pagination and query on models in FastAPI, which is blocked on merging this PR ....

bobbyo avatar Sep 15 '25 20:09 bobbyo

This pull request has a merge conflict that needs to be resolved.

github-actions[bot] avatar Oct 11 '25 16:10 github-actions[bot]

Just chiming in that my team are also interested in this feature. :pray:

bjmc avatar Nov 20 '25 16:11 bjmc

I've just noticed that the same limitation seems to affect Form(), could this fix be extended to cover form POST as well?

bjmc avatar Nov 21 '25 08:11 bjmc

This pull request has a merge conflict that needs to be resolved.

github-actions[bot] avatar Dec 12 '25 15:12 github-actions[bot]