Skip to content

[BUG][Python] strict=True in Pydantic Field breaks HTTP query parameter type conversion for integers #21905

Description

@claudiadpp

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the python-fastapi generator (with Pydantic V2), query parametes defined as integer in the OpenAPI spec are generated with strict=True in their Field definition.

Fot the OpenAPI definition given below, the generator produces this FastAPI endpoint

async def get_items(
    limit: Optional[Annotated[int, Field(le=50, strict=True, ge=1)]] = Query(None, description="", alias="limit", ge=1, le=50),
) -> GetItems200Response:

Note that strict=True is added to the Field of the query parameter top_n.

By default, the query parameters in HTTP requests are always strings. Pydantic normally handles the conversion ("3" -> 3). But with strict=True, type coercion is disabled. This makes valid requests fail.

For the petition

curl "http://localhost:8080/items?limit=3"

The response is:

{
  "detail": [
    {
      "type": "int_type",
      "loc": ["query", "limit"],
      "msg": "Input should be a valid integer",
      "input": "3"
    }
  ]
}
openapi-generator version

openapi-generator v7.15.0 (latest at the moment)
generator: python-fastapi (pydantic v2)
pydantic version: 2.11.7 (latest at the moment)
fastapi version: 0.116.1 (latest at the moment)

OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs

This is related to this and this issue.

Suggest a fix
  • Query parameters with type: integer should generate Field(ge=..., le=...) without strict=True.
  • Pydantic’s default behavior (automatic coercion of strings to ints/floats/bools) is the correct behavior for query parameters.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions