Skip to content

Bug: multiple parameters with duplicate query works inconsistently #4242

@cstarkers

Description

@cstarkers

Description

When using Parameter(query=... to rename the parameter like this, it is possible to have the same query parameter map to multiple different Python parameters by setting query to an identical value.

Sometimes this works, and the provided HTTP query parameter gets copied into multiple Python parameters of different types. Sometimes it does not work, and a validation error is returned. Behaviour seems to depend on what types it is being coerced into.

I would expect that using the same query value to assign a HTTP query param to multiple Python params is either always allowed, or never allowed (and if it is not allowed, the current error message could be improved).

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get
from litestar.params import Parameter
from typing import Annotated
from datetime import datetime

@get("/broken")
async def broken(
    some_date: Annotated[datetime, Parameter(query="someDate", required=True)],
    some_list: Annotated[
        list[str],
        Parameter(query="someDate", required=True),
    ],
) -> str:
    return f"{some_date=}, {some_list=}"

@get("/works")
async def works(
    some_date: Annotated[datetime, Parameter(query="someDate", required=True)],
    some_string: Annotated[
        str,
        Parameter(query="someDate", required=True),
    ],
) -> str:

    return f"{some_date=}, {some_string=}"

app = Litestar([broken, works])

Steps to reproduce

  1. Run the MCVE
  2. curl 'localhost:8000/works?someDate=2025-01-01T00:00:00Z' - this works.
  3. curl 'localhost:8000/broken?someDate=2025-01-01T00:00:00Z' - this does not work, and returns a validation error.

Screenshots

No response

Logs

# curl 'localhost:8000/broken?someDate=2025-01-01T00:00:00Z'
{"status_code":400,"detail":"Validation failed for GET /broken?someDate=2025-01-01T00:00:00Z","extra":[{"message":"Expected `datetime`, got `array`","key":"some_date","source":"query"}]}%   

# curl 'localhost:8000/works?someDate=2025-01-01T00:00:00Z' 
some_date=datetime.datetime(2025, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), some_string='2025-01-01T00:00:00Z'%

Litestar Version

2.16.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions