OpenAPITools / OpenAPITools/openapi-generator

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

Open
#19,672 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

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
  • Call make generate

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 common.yml and swagger.yml, then run make generate using the reported v7.8.0 setup. Inspect openapi_server/apis/default_api_base.py and trace the python-fastapi generator's handling of a referenced path and parameter. Done means the generated get_book signature uses book_id with the expected string type instead of UNKNOWN_PARAMETER_NAME.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.