question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicated enumerations in swagger definitions

See original GitHub issue

Version 2.6.1

Enums are currently being listed inline in the swagger output, causing enums to be generated as inner class. They should be referenced via "$ref" like classes are. Attached are two swagger json definitions, one provided by springfox and the other fixed by hand.

This is problematic for clients that attempt to use the model files, as they cannot be reused across api endpoints, as each generated model will contain a copy of the enum. This is especially problematic in apis that define enums with large sets of values, such as an enum set of the 249 ISO 3166 alpha 2 or 3 letter country codes.

Example enum: public enum Crest { ONE, TWO, THREE, }

Example class:

public class Obj {
    private Crest crest;

    public Crest getCrest() {
        return crest;
    }

    public void setCrest(Crest crest) {
        this.crest = crest;
    }
}

Incorrect swagger:

"definitions": {
  "Obj": {
    "type": "object",
    "properties": {
      "crest": {
        "type": "string",
        "enum": [
          "ONE",
          "TWO",
          "THREE"
        ]
      }
    }
  },
}

Resulting codegen:

// ... snip ...
public class Obj {
  /**
   * Gets or Sets crest
   */
  public enum CrestEnum {
    ONE("ONE"),
    
    TWO("TWO"),
    
    THREE("THREE");

    private String value;

    CrestEnum(String value) {
      this.value = value;
    }
  }
  // ... snip ...
}

Correct swagger:

"definitions": {
  "Obj": {
    "type": "object",
    "properties": {
      "crest": {
        "$ref": "#/definitions/Crest"
      }
    }
  },
  "Crest": {
    "type": "string",
    "enum": [
      "ONE",
      "TWO",
      "THREE"
    ]
  },
}

Resulting codegen:

// ... snip ...
public enum Crest {
  
  ONE("ONE"),
  
  TWO("TWO"),
  
  THREE("THREE");

  private String value;

  Crest(String value) {
    this.value = value;
  }
  // ... snip ...
}

// ... snip ...
public class Obj {
  @JsonProperty("crest")
  private Crest crest = null;

  // ... snip ...
  public Crest getCrest() {
    return crest;
  }

  public void setCrest(Crest crest) {
    this.crest = crest;
  }
  // ... snip ...
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:16
  • Comments:33 (16 by maintainers)

github_iconTop GitHub Comments

17reactions
jackkoppacommented, Dec 10, 2019

Similar use case for us: we now have a number of places where we need to cast generated TS enums, to a different instance of that same Java enum, because they’ve been generated using this class-specific naming. Would love to hear if this change is thought to be manageable

7reactions
dilipkrishcommented, Aug 16, 2020

This one is a toughie 😳 Its not easy to isolate enumerations easily the way things have been structured. I’ll take a look at how I can tease out this feature incrementally so that its possible

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove Duplicate Enum Entries in Swagger Documents with ...
In some constellations, NSwag might generate duplicated entries for enumeration values in the Swagger document. Here's how to fix that.
Read more >
Enums - Swagger
This lets you easily duplicate the content across a YAML file. Note: An anchor ( & ) must be defined before it is...
Read more >
NSwagStudio generating broken client code (duplicate enum ...
The generated code contains a set of enums that, based on the attributes decorating them, are apparently flags for Json serialization - I...
Read more >
Fix Swagger Validator errors in Power Platform connectors
Enum values should be unique and not duplicated. ... Your swagger has an operation which doesn't define a parameter that seems to be ......
Read more >
Types and Parameters - OpenAPI - Documentation | NestJS
Based on the CreateCatDto , the following model definition Swagger UI will be ... run into a problem with the generated code containing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found