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.61k stars 6.52k forks source link

[BUG][PYTHON generator leaves out import statments when a model name suffix is given #9182

Open ben5448 opened 3 years ago

ben5448 commented 3 years ago

Bug Report Checklist

Description

The openapi-generator is creating import statements and documentation that have duplicate "modelNameSuffix" references in the class name and in the file name. As an example:

openapi-generator version

5.1.0 This is a regression from 4.3.1.

OpenAPI declaration file content or url

https://gist.github.com/ben5448/40aa828072214fd2e490708985debfb8

Command line used for generation

Using the 5.1 generator docker image from: "docker pull openapitools/openapi-generator-cli"

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v5.1.0 generate -i /local/test.yaml -g python -o /local --model-name-suffix Dto

Steps to reproduce

Run the docker command above on the yaml file provided in gist.github.com.

Examine the generated file openapi_client/model/foo_schema_dto.py. The lazy_import function will be:

def lazy_import():
    FooBSchemaDto
    FooSchemaAllOfDto

Using the same command line except remove the model-name-suffix will generate a correct lazy_import() function like this:

Note: The filename ends in _dto_dto and the imported class name ends in DtoDto.

Note: Running the openapi-generator-cli command without a model-name-suffix will generate a working set of files. However, the maven wrapper around the openapi generator defaults to a modelNameSuffix of "Dto" when no suffix is specified. The interaction between the maven wrapper and openapi generator always generates errant code.

Related issues/PRs
Suggest a fix/enhancement
auto-labeler[bot] commented 3 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

ben5448 commented 3 years ago

Accidently triggered the save without a proper example. Without the model-name-suffix as part of the command line, the generated lazy_import() function will be:

def lazy_import():
    from openapi_client.model.foo_b_schema import FooBSchema
    from openapi_client.model.foo_schema_all_of import FooSchemaAllOf
    globals()['FooBSchema'] = FooBSchema
    globals()['FooSchemaAllOf'] = FooSchemaAllOf

The maven wrapper around the openapi generator defaults to a modelNameSuffix of "Dto" when no suffix is specified. The interaction between the maven wrapper and openapi generator always generates errant code.