Duplicated enumerations in swagger definitions
See original GitHub issueVersion 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:
- Created 6 years ago
- Reactions:16
- Comments:33 (16 by maintainers)
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
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