OpenAPITools / OpenAPITools/openapi-generator

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

Open
#9,182 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • [ X] Have you provided a full/minimal spec to reproduce the issue?
  • [ X] Have you validated the input using an OpenAPI validator (example)?
  • [ X] Have you tested with the latest master to confirm the issue still exists?
  • [ X] Have you searched for related issues/PRs?
  • [ X] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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:

  • from sales_openapi.model.error_response_dto_dto import ErrorResponseDtoDto
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

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 the OpenAPI declaration in the linked gist and reproduce the issue using the provided openapi-generator-cli:v5.1.0 Docker command with --model-name-suffix Dto. Compare generated openapi_client/model/foo_schema_dto.py and its lazy_import function with output generated without the suffix; done means imports, class names, documentation, and filenames contain the suffix only once and the generated code works.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java, python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.