swagger-api / swagger-api/swagger-parser

Schema $ref with same name is not producing models properly

Open
#1,089 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

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.


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 = tryName + "_" + i;
                    existingModel = schemas.get(tryName);
                    i++;
                }

Can some one help me to validate this code

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 in modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java around line 87 and inspect the duplicate-definition naming loop. Reproduce the case with schemas spread across multiple files and same-named schemas, then run the code generator to verify that every model is produced and each $ref points to the correct model.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
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.