Bug Report Checklist
Description
The python-fastapi generator incorrectly generates Query(None, ...) for required query parameters (marked with required: true), making them optional in FastAPI. This contradicts the OpenAPI specification and causes the API to accept requests without required parameters.
This is similar to Issue #16029 which fixed the same problem for path parameters, but the issue persists for query parameters.
In FastAPI:
- Query(None, description="", alias="") indicates an optional parameter
- Query(..., description="", alias="") indicates a required parameter
openapi-generator version
v7.17.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example API
version: 1.0.0
paths:
/test:
get:
parameters:
- name: paramRequired
in: query
required: true
schema:
type: integer
- name: paramOptional
in: query
required: false
schema:
type: integer
responses:
"200":
description: Success
Actual output:
async def test_get(
param_required: StrictInt = Query(None, description="", alias="paramRequired"),
param_optional: Optional[StrictInt] = Query(None, description="", alias="paramOptional"),
):
...
Expected output:
async def test_get(
param_required: StrictInt = Query(...,description="", alias="paramRequired"),
param_optional: Optional[StrictInt] = Query(None, description="", alias="paramOptional"),
):
...
Generation Details
Steps to reproduce
- Create the OpenAPI spec above with both required and optional query parameters
- Generate code using python-fastapi generator
- Observe the generated function signature
- Notice that both required and optional parameters have Query(None, ...)
Related issues/PRs
#16029 - Fixed the same issue for path parameters, but query parameters still have this bug
Suggest a fix
The generator's templates need to check the required field for query parameters, similar to how it was fixed for path parameters in #16029:
- If required: true → generate Query(...) or Query() without None
- If required: false or omitted → generate Query(None, ...)
The fix should be in the mustache templates for the python-fastapi generator.
{{^defaultValue}}
{{#isPathParam}}...{{/isPathParam}}
{{^isPathParam}}None{{/isPathParam}}
{{/defaultValue}}
to be replaced by
{{^defaultValue}}
{{#required}}...{{/required}}
{{^required}}None{{/required}}
{{/defaultValue}}
Bug Report Checklist
Description
The python-fastapi generator incorrectly generates Query(None, ...) for required query parameters (marked with required: true), making them optional in FastAPI. This contradicts the OpenAPI specification and causes the API to accept requests without required parameters.
This is similar to Issue #16029 which fixed the same problem for path parameters, but the issue persists for query parameters.
In FastAPI:
openapi-generator version
v7.17.0
OpenAPI declaration file content or url
Actual output:
Expected output:
Generation Details
Steps to reproduce
Related issues/PRs
#16029 - Fixed the same issue for path parameters, but query parameters still have this bug
Suggest a fix
The generator's templates need to check the required field for query parameters, similar to how it was fixed for path parameters in #16029:
The fix should be in the mustache templates for the python-fastapi generator.
to be replaced by