Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reusable enumerations in parameters/models #1250

Closed
jfiala opened this issue Apr 6, 2016 · 5 comments
Closed

Allow reusable enumerations in parameters/models #1250

jfiala opened this issue Apr 6, 2016 · 5 comments
Labels
Milestone

Comments

@jfiala
Copy link
Contributor

jfiala commented Apr 6, 2016

Version: 2.4.0

For enumerations:

public enum MyEnum {
    Value1, Value2;
}
private MyEnum myvalue;

the API-docs are currently rendered as a enum to the parameter/attribute:

"myvalue":{"type":"string","enum":["Value1","Value2"]}

When generating client code using Swagger-codegen (Java), the Enumeration is generated as inner class:

public class MyObject   {

public enum MyEnum {
    VALUE1("Value1"),
    VALUE2("Value2");

    private String value;

    MyEnum (String value) {
      this.value = value;
    }

    @Override
    @JsonValue
    public String toString() {
      return value;
    }
  }

}

If you have multiple variables of type MyEnum in different classes, this leads to the fact, that the enumeration is contained as inner classes in every class where it is used (and who don't know each other/have no common parent or reference).

It would be much better to have a reference to the enumeration, so the client code can generate a separate class for the Enumeration (same as done in the API model classes).

According to the OpenAPI-spec, this should be possible using a reference to a Set/List of type String and the enumeration of the possible values:
OAI/OpenAPI-Specification#300

I think this would be the more clean way of rendering the enumerations.

@dilipkrish
Copy link
Member

This is really a swagger-code-gen issue I believe. AFAIK this is not supported in swagger 2.0

@dilipkrish dilipkrish added this to the 2.5.0 milestone Apr 7, 2016
@jfiala
Copy link
Contributor Author

jfiala commented Apr 9, 2016

This should be possible with Swagger 2.0 using a reference to a List of type string with the enum values listed, pls see the example at
OAI/OpenAPI-Specification#300 (comment)

Codegen only picks up the Enums as defined, if the Enum is referenced it should generate the Enum as a separate enum class.

@kylerjensen
Copy link

kylerjensen commented Dec 20, 2017

This is a duplicate of #1738, but neither of these issues are an issue with swagger-codegen. Springfox should be writing top-level enums to the definitions section of the generated spec and referencing them using $ref where they are used, not writing them as inline enums.

@dilipkrish
Copy link
Member

dilipkrish commented Dec 21, 2017

@Kjens93 While I understand, this is the reason this is not a simple change.

@fzyzcjy
Copy link

fzyzcjy commented Mar 7, 2021

Hi is there any updates (after 3 years?) This is very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants