简体   繁体   English

如何使用Swagger Codegen在String中定义枚举值

[英]How to define enum values in String using Swagger Codegen

I am using SwaggerCodegen(2.3.1) to generate the client code of my APIs. 我正在使用SwaggerCodegen(2.3.1)生成我的API的客户端代码。 I've used enum as part of YAML definition file, after which Swagger generates the java file. 我使用enum作为YAML定义文件的一部分,之后Swagger生成java文件。

random:
    enum:
    - A
    - B
    - C+
    - C-
    type: string

This yml gets converted to 这个yml被转换为

  public enum Random{
    A("A"),
   B("B"),
   C_("C+"),
   C_("C-"); // compile time error..

private String value;
....}

Is there a way I can give a name to my enum values like below ? 有没有办法可以给我的枚举值命名如下?

public enum Random{
A("A"),
B("B"),
CP("C+"),  
CM("C-"); 
}

Special Characters is a restriction to swagger codegen and may be they're still not supported so to achieve it I have to add a new block in the yml file 特殊字符是对swagger codegen的限制,可能它们仍然不受支持所以要实现它我必须在yml文件中添加一个新块

random:
enum:
- A
- B
- C+
- C-
x-enum-names:
- A
- B
- CP
- CM
type: string

and along with this I've to create a class that inherits JavaClientCodegen and override below method which will replace C+ to CP 和我一起创建一个继承JavaClientCodegen并覆盖下面方法的类,它将C +替换为CP

class SwaggerCodegen : JavaClientCodegen() {
 override fun updateCodegenPropertyEnum(codegenProperty: CodegenProperty?) {
      super.updateCodegenPropertyEnum(codegenProperty)
      if (codegenProperty!!.vendorExtensions.containsKey( "x-enum-names" )) {
         val alterNames = codegenProperty.vendorExtensions["x-enum-names"] as List<String>
         val enums = codegenProperty.allowableValues.get("enumVars") as List<MutableMap<String, String>>
         if (alterNames.size != enums.size) {
            return
         }
         alterNames.forEachIndexed { index, element ->
            enums[index].put("name", element)
         }
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM