Skip to content

[BUG][PYTHON-FASTAPI] Required query parameters generated with Query(None, ...) instead of Query(..., ...) #22494

@darkobardic

Description

@darkobardic

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

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}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions