OpenAPITools / OpenAPITools/openapi-generator

[BUG][python-fastapi] &#39 in generated api when adding a default to optional query string parameter

Open
#15,685 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

Adding a default to an optional query parameter of type int leads to invalid arguments for Query call.

Generated code:

# coding: utf-8

from typing import Dict, List  # noqa: F401

from fastapi import (  # noqa: F401
    APIRouter,
    Body,
    Cookie,
    Depends,
    Form,
    Header,
    Path,
    Query,
    Response,
    Security,
    status,
)

from openapi_server.models.extra_models import TokenModel  # noqa: F401


router = APIRouter()


@router.get(
    "/pet/findByStatus",
    responses={
        200: {"description": "successful operation"},
    },
    tags=["default"],
    response_model_by_alias=True,
)
async def pet_find_by_status_get(
    status: str = Query('default_string', description="Status values that need to be considered for filter"),
) -> None:
    """Multiple status values can be provided with comma separated strings"""
    ...
openapi-generator version

Version 6.6.0

OpenAPI declaration file content or url
openapi: 3.0.2
servers:
  - url: /v3
info:
  description: ""
  version: 1.0.17
  title: Swagger Petstore - OpenAPI 3.0
paths:
  /pet/findByStatus:
    get:
      description: Multiple status values can be provided with comma separated strings
      parameters:
        - in: query
          name: status
          description: Status values that need to be considered for filter
          schema:
            type: string
            default: "default_string"
      responses:
        '200':
          description: successful operation
Steps to reproduce
java -jar generators/openapi-generator-cli-.jar generate -i min_spec.yaml -g python-fastapi -o pet_store 

Look in default_api.py line 33:

Related issues/PRs

None found

Suggest a fix

Change {{default}} in endpoint_arguments_definition.mustache, to {{&default}}.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with endpoint_arguments_definition.mustache, then reproduce the issue using the provided min_spec.yaml and generation command. Inspect default_api.py around line 33 and verify that the generated Query default is valid Python rather than containing an HTML entity.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.