OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.44k stars 6.48k forks source link

[BUG] [python-fastapi] default API parameter UNKNOWN_PARAMETER_NAME when $ref to path parameter #19672

Open andremueller-cosateq opened 6 days ago

andremueller-cosateq commented 6 days ago

Bug Report Checklist

Description

When an API path including a path argument /books/{bookId} references a path within an other file using $ref and the path parameter itself {bookId} references a parameters definition the base class generation creates invalid Python code.

openapi-generator version

v7.8.0

OpenAPI declaration file content or url

common.yml

swagger: "2.0"
info:
  title: common entities
  version: 0.0.1

paths:
  /books/{bookId}:
    get:
      operationId: getBook
      parameters:
        - $ref: "#/parameters/BookIdPathParam"
      responses:
        "200":
          description: Ok

parameters:
    BookIdPathParam:
      name: bookId
      in: path
      required: true
      type: string

swagger.yml

swagger: "2.0"
info:
  title: my api
  version: 0.0.1

paths:
  /books/{bookId}:
    $ref: "common.yml#/paths/~1books~1{bookId}"
Generation Details
SHELL := /bin/bash
PROJ_DIR := $(shell dirname "$(abspath $(lastword $(MAKEFILE_LIST)))")
ifeq ($(OS),Windows_NT)
    PROJ_DIR := $(shell cygpath -u "$(PROJ_DIR)")
endif
$(info PROJ_DIR $(PROJ_DIR))
USER := $(shell id -u):$(shell id -g)
DOCKER_RUN := MSYS_NO_PATHCONV=1 docker run --rm -v "/$(PROJ_DIR)://local" -u $(USER)
VERSION := v7.8.0

.PHONY: generate
generate:
    $(DOCKER_RUN) openapitools/openapi-generator-cli:$(VERSION) generate \
              -i /local/swagger.yml \
              -g python-fastapi \
              -o /local/api

.PHONY: validate
validate:
    $(DOCKER_RUN) openapitools/openapi-generator-cli:$(VERSION) validate \
              -i /local/swagger.yml

.PHONY: clean
clean:
    rm -rf "$(PROJ_DIR)/api"

Produced file openapi_server/apis/default_api_base.py. See the UNKNOWN_PARAMETER_NAME!

from typing import ClassVar, Dict, List, Tuple  # noqa: F401
class BaseDefaultApi:
    subclasses: ClassVar[Tuple] = ()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        BaseDefaultApi.subclasses = BaseDefaultApi.subclasses + (cls,)
    async def get_book(
        self,
        UNKNOWN_PARAMETER_NAME: ,
    ) -> None:
        ...

Now expected would have been

from typing import ClassVar, Dict, List, Tuple  # noqa: F401
class BaseDefaultApi:
    subclasses: ClassVar[Tuple] = ()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        BaseDefaultApi.subclasses = BaseDefaultApi.subclasses + (cls,)
    async def get_book(
        self,
        book_id: str: ,
    ) -> None:
        ...
Steps to reproduce
andremueller-cosateq commented 6 days ago

Added Makefile and yaml files for simple working on this issue. Uses docker. openapi-demo.zip