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.11k stars 6.39k forks source link

[BUG] Path issues when generating python client via docker #15312

Open MaggieWalker opened 1 year ago

MaggieWalker commented 1 year ago

Bug Report Checklist

Description

I am generating a python client with the attached specs, and getting an incomplete/broken output.

openapi-generator version

I am using openapi-generator-cli:latest-release https://hub.docker.com/layers/openapitools/openapi-generator-cli/latest-release/images/sha256-fc570971c1ca62fac829aba7d7d735efd580293adecdd2ccae21094904bc29e6?context=explore

OpenAPI declaration file content or url

openapispecs.yml

---
openapi: 3.0.2
info:
  title: my-service
  description: Service description
  version: 1.0.0
paths:
  "/":
    get:
      tags:
      - infra
      summary: Hello World
      description: |-
        Hello World with an optional GET param 'name'.

        Args:
            name: Str passed as URL parameter
            identity_uuid: uuid
        Returns:
            Response: PlainTextResponse containing "hello 'name'" text
      operationId: hello_world__get
      parameters:
      - required: false
        schema:
          title: Name
          type: string
          default: world
        name: name
        in: query
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/HTTPValidationError"
  "/hello/":
    get:
      tags:
      - infra
      summary: Health
      description: Check the health of the application.
      operationId: health_hello__get
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/HelloResponse"
components:
  schemas:
    HTTPValidationError:
      title: HTTPValidationError
      type: object
      properties:
        detail:
          title: Detail
          type: array
          items:
            "$ref": "#/components/schemas/ValidationError"
    HelloResponse:
      title: HelloResponse
      type: object
      properties:
        status:
          title: Status
          type: string
          default: ok
      description: Simple model with OK status.
    ValidationError:
      title: ValidationError
      required:
      - loc
      - msg
      - type
      type: object
      properties:
        loc:
          title: Location
          type: array
          items:
            anyOf:
            - type: string
            - type: integer
        msg:
          title: Message
          type: string
        type:
          title: Error Type
          type: string
Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest-release generate \
    -i /local/openapispecs.yml \
    -g python \
    -o /local/out/python
Output Example

I would expect the second line to have a complete import statement and for PathValues.SOLIDUS to have a value.

python/openapi_client/apis/path_to_api.py

import typing_extensions

from openapi_client.paths import PathValues
from openapi_client.apis.paths. import 
from openapi_client.apis.paths.hello_ import Hello

PathToApi = typing_extensions.TypedDict(
    'PathToApi',
    {
        PathValues.SOLIDUS: ,
        PathValues.HELLO_: Hello,
    }
)

path_to_api = PathToApi(
    {
        PathValues.SOLIDUS: ,
        PathValues.HELLO_: Hello,
    }
)
Steps to reproduce

When I run the same specs with the following command (using python-nextgen) I do not see this issue, and I am able to use the client.

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest-release generate \
    -i /local/openapispecs.yml \
    -g python-nextgen \
    -o /local/out/python-nextgen
Related issues/PRs
Suggest a fix
spacether commented 1 year ago

@MaggieWalker I just cut a release with a fix in the new python generator repo here:

There's a verification of it in the petstore sample here

from petstore_api.apis.paths.solidus import Solidus

PathToApi = typing_extensions.TypedDict(
    'PathToApi',
    {
    "/": typing.Type[Solidus],
    ....
    }
)

and Solidus exists:

from petstore_api.paths.solidus.get.operation import ApiForGet

class Solidus(
    ApiForGet,
):
    pass

and the imported api class exists too

MaggieWalker commented 1 year ago

Thank you @spacether ! I was able to test this locally and it is working for me as well. Thank you for the patch!

spacether commented 1 year ago

You're welcome 🙂 Thank you for the bug report.