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

Generate enums with integer values assigned #2690

Open
kenjitayama opened this issue Apr 23, 2016 · 28 comments
Open

Generate enums with integer values assigned #2690

kenjitayama opened this issue Apr 23, 2016 · 28 comments

Comments

@kenjitayama
Copy link

I want to generate enum with integer values assigned.

Swift:

enum Weather: Int {
    case Sunny = 1
    case Cloudy = 2
    case Rainy = 3
}

Java:

public enum Weather {
    Sunny(1),
    Cloudy(2),
    Rainy(3);

    private final int type;

    private Weather(final int price) {
        this.type = type;
    }

    public type getType() {
        return type;
    }
}

But I couldn't figure out how to do this.

In the Swagger 2.0 specs, it references JSON Schema specs for enum.
It says any type can be used, but it seems like type needs to be string for generating enums. Like this:

definitions:
  weather:
    type: object
    required:
      - type
    properties:
      type:
        type: string
        enum:
          - Sunny
          - Cloudy
          - Rainy

If I change type to integer, no enum is generated, and it will be a plain integer property.

Firstly, I couldn't find how to assign integers to enum items using Swagger specs.

Are there any plans or workarounds to support this kind of enum?

@wing328
Copy link
Contributor

wing328 commented Apr 23, 2016

@kenjitayama For Java, it will be taken care of by #2508, which adds better support for enum (number, string, special characters) in PHP, C# and Java.

For Swift, we want to do it as part of #2508 as well. Currently, we're adding better enum support for JS in that PR.

@kenjitayama
Copy link
Author

Thanks for the quick response! I will be watching the issue 👍

@wing328
Copy link
Contributor

wing328 commented May 3, 2016

@kenjitayama #2508 has been merged into master so it should cover at least Java. Please pull the latest master and give it a try.

@kenjitayama
Copy link
Author

@wing328
Thanks for the notice!
Looking forward for Swift support too :)

I wasn't sure of the supported yaml syntax and I tried the following.
Only weather3 generated an enum.

definitions:
  weather1:
    type: object
    required:
      - type
    properties:
      type:
        type: integer
        enum:
          - Sunny
          - Cloudy
          - Rainy
  weather2:
    type: object
    required:
      - type
    properties:
      type:
        type: integer
        enum:
          - 1
          - 2
          - 3
  weather3:
    type: object
    required:
      - type
    properties:
      type:
        type: integer
        format: int32
        enum:
          - 1
          - 2
          - 3
  weather4:
    type: object
    required:
      - type
    properties:
      type:
        type: integer
        format: int32
        enum:
          - Sunny
          - Cloudy
          - Rainy

weather3 produced Java code :

public enum TypeEnum {
    NUMBER_1(1),
    NUMBER_2(2),
    NUMBER_3(3);

    private Integer value;

    TypeEnum(Integer value) {
      this.value = value;
    }

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

Is there any syntax for having custom names for each enum items?

weather3 caused exception for Swift (I guess not supported yet) :

Exception in thread "main" java.lang.RuntimeException: Could not process model 'weather3'
    at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:244)
    at io.swagger.codegen.cmd.Generate.run(Generate.java:223)
    at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at io.swagger.codegen.languages.SwiftCodegen.fromProperty(SwiftCodegen.java:331)

@wing328
Copy link
Contributor

wing328 commented May 4, 2016

For weather1 and weather4, I don't think it's valid as the enum is string but the type is integer.

For enum naming convention, is there any reference? The most common one I saw is adding the "Enum" suffix (e.g. StatusEnum).

@kenjitayama
Copy link
Author

For weather1 and weather4, I don't think it's valid as the enum is string but the type is integer.

I agree. Tried them in case I could find out something.

For enum naming convention, is there any reference? The most common one I saw is adding the "Enum" suffix (e.g. StatusEnum).

Looks like I couldn't express my question correctly.
Is there a way to get

public enum TypeEnum {
    Sunny(1),
    Cloudy(2),
    Rainy(3);

Instead of

public enum TypeEnum {
    NUMBER_1(1),
    NUMBER_2(2),
    NUMBER_3(3);

?

@wing328
Copy link
Contributor

wing328 commented May 4, 2016

Don't think we support that at the moment.

Should be mapping be done in the server side, which means the client will send the string "Sunny" and the server will map it to 1 (in database, etc)?

@kenjitayama
Copy link
Author

Don't think we support that at the moment.

This was what I was looking for…, but thanks for sharing.

Should be mapping be done in the server side, which means the client will send the string "Sunny" and the server will map it to 1 (in database, etc)?

In my case, the client needs to send and retrieve 1 knowing that it means "Sunny".
Maybe this kind of server API design is considered not good, but I need to stick with it.
I guess the best that I can do is to describe them with description:, and write enums manually.

@wing328
Copy link
Contributor

wing328 commented May 4, 2016

@kenjitayama technically we can support it via vendor extension with code changes in how to handle the enum mapping.

@kenjitayama
Copy link
Author

I see that the Swagger specs itself doesn't support this use case.
I think the vendor extension could be something like this:

properties:
  type:
    type: integer
    format: int32
    enum:
      - 1
      - 2
      - 3
    x-enumNames:
      - Sunny
      - Cloudy
      - Rainy 

I saw swagger-codegen supporting a vendor extension before (x-swagger-router-controller) , so I recognize that it's position is not closed for supporting vendor extensions.
It will be great if I can see this happen in the future.
Maybe this is something that can go into Open API 3.0 specs?

@wing328
Copy link
Contributor

wing328 commented May 4, 2016

Feel free to suggest here: https://github.com/OAI/OpenAPI-Specification/issues

@wing328
Copy link
Contributor

wing328 commented Aug 9, 2016

2.2.1 has been released with some improvements in enum. Please give it a try.

@wing328 wing328 modified the milestones: Future, v2.2.2 Aug 9, 2016
@kenjitayama
Copy link
Author

I'm currently not using swagger(nor any API description language), but I'm listening to updates :) I'd love to try if this feature gets supported.

@ePaul
Copy link
Contributor

ePaul commented Aug 15, 2016

I think for better support (i.e. define name + value together) we need to wait for OpenAPI 3.0. OAI/OpenAPI-Specification#681 seems to have some ideas on how this could be defined. If we have that, implementing it in Codegen seems quite straightforward.

@martinmcwhorter
Copy link
Contributor

I have noticed that Enums are not properly generated in the Angular2 TypeScript Client. I will look at the code of that specific generator to see if it is a bug there.

@paranoid945
Copy link

@kenjitayama @wing328

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

Did you guys noticed that the json value generated is type string instead of type integer, which is wrong by swagger spec?

@ePaul
Copy link
Contributor

ePaul commented Mar 23, 2017

@paranoid945 could you please open a new issue about this? I fear it gets lost in this discussion, which is about supporting names for enum values.

@paranoid945
Copy link

@ePaul
I opened #5177 for this.

@Skyfish1
Copy link

Skyfish1 commented Jul 5, 2017

Is there also a way to have Enums with additional values

I have something like this in Java:

public enum MimeType {
PDF("application/pdf"),
PNG("image/png"),
XML("text/xml"),
HTML("text/html"),
PKPASS("application/pkpass");

protected final String mediaTyp;

private MimeType(@NotNull String mediaType) {
    if(mediaType == null) {
        throw new IllegalArgumentException("Argument \'mediaType\' must not be \'null\'!");
    } else {
        this.mediaType = mediaType;
    }
}

public String getMediaType() {
    return this.mediaType;
}

}

@craffael
Copy link
Contributor

I've implemented the x-enumNames approach suggested by @kenjitayama that passes the value of the x-enumNames list to the template: x-enumNames

Considering that even Version 3 of the Swagger OpenAPI Spec does not support something similar in the near future, I think it may be an option to add this "vendor extension" to swagger-codegen?

@xiangyucao
Copy link

Help! I think my problem is related to this. Here is my enum:

public enum JoinType
{
    Private = 0,
    ApprovalNeeded = 1,
    Auto = 2  
}

Any I use c.DescribeAllEnumsAsStrings();
So my interface looks like:
"JoinType":{"enum":["Private","ApprovalNeeded","Auto"],"type":"string"}.

But the generated code start with enum 1, instead of 0? Why? C# default start with 0 instead of 1. Is there any way I can specify the start number?? Thanks!

@vaishali-ntnx
Copy link

Is there any plan to support this for Golang?

@FearlessHyena
Copy link

It's been a while since this was proposed. Any updates or plans for supporting this?

@nasirvi
Copy link

nasirvi commented Feb 11, 2020

We are also facing the same issue for C#. @xiangyucao, did you find any solution for this?

@xiangyucao
Copy link

xiangyucao commented Feb 11, 2020 via email

@fzyzcjy
Copy link
Contributor

fzyzcjy commented Jun 25, 2020

Hi, what about dart/flutter support of enums? Thanks!

@ArvindLakra
Copy link

Hello,

Was it implemented in any update? I am also running into same issue. Want to define custom value for specific enum names.

Any plan to support this ?

Thanks,
Arvind Lakra

@jgarciadelanoceda
Copy link

jgarciadelanoceda commented Jul 7, 2022

Same issue in C#.
Reviewed the OpenApi generator of VS and if you set the extension x-enumNames the generator of VS writes the Enum with the correct values.

Good feature to have in Swagger-CodeGen too

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

No branches or pull requests