openapi-generator: [BUG] Schema $ref with same name is not producing models properly

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

My schema definition are spread across multiple files and they are cross reference(using $ref). In these files there is one schema with same name. I have 5 such instances

When i run code generator, Only 2 models are produced. Rest are ignored. Some of the $ref are pointing to wrong model.

Here is example given below

openapi-generator version

4.0.0-beta

OpenAPI declaration file content or url

https://gist.github.com/vinodchitrali/c5cd3be1c8d6fb0d2fb6bcf6a530ad94

Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar  generate -i ./api/openapi.yaml   -g python -o /tmp/python/ --api-package api --model-package model --additional-properties projectName=python
Steps to reproduce
  1. Copy the Yaml files from https://gist.github.com/vinodchitrali/c5cd3be1c8d6fb0d2fb6bcf6a530ad94
  2. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ./api/openapi.yaml -g python -o /tmp/python/ --api-package api --model-package model --additional-properties projectName=python
  3. Check files generated in /tmp/python/openapi_client/model/
Related issues/PRs
Suggest a fix

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 22 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@jmini , I know and you are doing a great job!. But the specific fix for this issue related to $ref was only released on version 2.0.12.
I could see that the latest released version of your branch is based on version 2.0.11, though…

I see some issue in the following code.

https://github.com/swagger-api/swagger-parser/blob/master/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java#L87

                //We add a number at the end of the definition name
                int i = 2;
                for (String name : schemas.keySet()) {
                    if (name.equals(possiblyConflictingDefinitionName)) {
                        tryName = possiblyConflictingDefinitionName + "_" + i;
                        existingModel = schemas.get(tryName);
                        i++;
                    }

In above code, possiblyConflictingDefinitionName remains same through out the loop. If possiblyConflictingDefinitionName = "model" then the outcome of the loop is always tryName = "model_2". I am hitting same issue.

So above code should be

               //We add a number at the end of the definition name
                int i = 2;
                tryName = possiblyConflictingDefinitionName;
                for (String name : schemas.keySet()) {
                    if (name.equals(tryName)) {
                        tryName = possiblyConflictingDefinitionName+ "_" + i;
                        existingModel = schemas.get(tryName);
                        i++;
                    }

Can some one help me to validate this code ? Also help to build org.openapitools.swagger.parser(2.0.11-OpenAPITools.org-1)

@jmini , I see that u have done recent pom update for 2.0.11. Can check above code and help to build swagger parser for OpenAPI tools