Bug Report Checklist
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.
Bug Report Checklist
Description
When using the
python-fastapigenerator (with Pydantic V2), query parametes defined as integer in the OpenAPI spec are generated withstrict=Truein theirFielddefinition.Fot the OpenAPI definition given below, the generator produces this FastAPI endpoint
Note that
strict=Trueis added to the Field of the query parametertop_n.By default, the query parameters in HTTP requests are always strings. Pydantic normally handles the conversion (
"3"->3). But withstrict=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
Field(ge=..., le=...)withoutstrict=True.