swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Generate enums with integer values assigned

Open kenjitayama opened this issue 10 years ago • 28 comments

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?

kenjitayama avatar Apr 23 '16 14:04 kenjitayama

@kenjitayama For Java, it will be taken care of by https://github.com/swagger-api/swagger-codegen/pull/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 https://github.com/swagger-api/swagger-codegen/pull/2508 as well. Currently, we're adding better enum support for JS in that PR.

wing328 avatar Apr 23 '16 14:04 wing328

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

kenjitayama avatar Apr 23 '16 14:04 kenjitayama

@kenjitayama https://github.com/swagger-api/swagger-codegen/pull/2508 has been merged into master so it should cover at least Java. Please pull the latest master and give it a try.

wing328 avatar May 03 '16 13:05 wing328

@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)

kenjitayama avatar May 04 '16 06:05 kenjitayama

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).

wing328 avatar May 04 '16 07:05 wing328

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);

?

kenjitayama avatar May 04 '16 08:05 kenjitayama

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)?

wing328 avatar May 04 '16 08:05 wing328

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.

kenjitayama avatar May 04 '16 08:05 kenjitayama

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

wing328 avatar May 04 '16 08:05 wing328

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?

kenjitayama avatar May 04 '16 09:05 kenjitayama

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

wing328 avatar May 04 '16 09:05 wing328

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

wing328 avatar Aug 09 '16 08:08 wing328

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.

kenjitayama avatar Aug 09 '16 22:08 kenjitayama

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

ePaul avatar Aug 15 '16 19:08 ePaul

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.

martinmcwhorter avatar Dec 09 '16 11:12 martinmcwhorter

@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?

paranoid945 avatar Mar 23 '17 07:03 paranoid945

@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.

ePaul avatar Mar 23 '17 09:03 ePaul

@ePaul I opened #5177 for this.

paranoid945 avatar Mar 23 '17 10:03 paranoid945

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;
}

}

Skyfish1 avatar Jul 05 '17 08:07 Skyfish1

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?

craffael avatar Aug 31 '17 12:08 craffael