首页 > 解决方案 > enunciate Enum with custom value or property

问题描述

I am now encountering the problem when generating Enum with custom properties.

In Project A, i build a API-webservice. I use enunciate-maven-plugin (version 2.12.1) to generate swagger documentation from Java code. When enunciate-maven-plugin runs, it will output the swagger.json

Then in the project B which consumes the Project A API-webservice, I use swagger-codegen-maven-plugin (version 2.3.1) to generate Java code from the above swagger.json.

maven configuration in Project B:

<plugin>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-codegen-maven-plugin</artifactId>
      <version>2.4.8</version>
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <inputSpec>swagger-project-a.json</inputSpec>
            <language>java</language>
            <generateApis>false</generateApis>
            <generateApiTests>false</generateApiTests>
            <generateApiDocumentation>false</generateApiDocumentation>
            <generateSupportingFiles>false</generateSupportingFiles>
            <generateModelDocumentation>false</generateModelDocumentation>
            <output>${project.basedir}</output>
            <apiPackage>extern.api.swagger</apiPackage>
            <modelNamePrefix></modelNamePrefix>
            <modelPackage>extern.api.projecta</modelPackage>
          </configuration>
        </execution>
      </executions>
    </plugin>

Everything runs well, except the Enum. For example, I have enum

public enum ColorEnum {
   RED("1"),
   GREEN("2"),
   BLUE("3")

   private final String value;

   ColorEnum(value) {
       this.value = value;
   }

   public String getValue(){ return value;}

}

The generated swagger.json will be :

"json_ColorEnum" : {
  "type" : "string",
  "title" : "ColorEnum",
      "enum" : [
        "RED",
        "GREEN",
        "BLUE"
      ]
}

The enum values are not generated. I already tried the @JsonProperty like this: REF: https://github.com/stoicflame/enunciate/issues/653

public enum ColorEnum {
   @JsonProperty("1")
   RED("1"),
   @JsonProperty("2")
   GREEN("2"),
   @JsonProperty("3")
   BLUE("3")

   private final String value;

   ColorEnum(value) {
       this.value = value;
   }

   public String getValue(){ return value;}

}

And the output is not as expected:

"json_ColorEnum" : {
  "type" : "string",
  "title" : "ColorEnum",
      "enum" : [
        "1",
        "2",
        "3"
      ]
}

How can i generate Enum documentation with both label and value ?

Any help is appreciated. Thank you.

标签: javaenumsswaggerenunciateswagger-codegen-maven-plugin

解决方案


推荐阅读