如何将数组或枚举中的枚举值定义为映射 属性 中的键?

How to define enum values in array or enum as key in map property?

我正在使用 swagger-codegen-maven-plugin (2.2.1) 从 YML 配置生成 java 和打字稿代码 class 文件。我有两个问题。

如何在 YML 中定义枚举数组 属性?

如何在 YML 中定义 map 属性 enum 为 key 和 boolean 为 value?

让我知道是否可行或有任何解决方法?目前,我在 java 和 typecrtipt 中定义了枚举 class 并将其作为字符串传递。谢谢。

DataInfo:
       type: object
       properties:
          enumTest:        -- works fine
            type: string
            enum:
              - one
              - two   
           enumTestArray:   --failing to generate code
              type: array
              items:
                 type: string
                 enum:
                  - one
                   -two  
              testMap:   -- works fines generate Map<String, Boolean> and { [key: string]: boolean; };
                type: object         
                additionalProperties:
                    type: boolean 

swagger enum doc

Map Property

更新:

与第一个问题相关:定义枚举数组属性。 swagger-codegen-maven-plugin 生成无效的 java class 文件如下:看起来像生成 <、> 和 " 字符的问题。

@XmlType(name="List&lt;EnumTestArrayEnum&gt;")
@XmlEnum
public enum List&lt;EnumTestArrayEnum&gt; {

    ONE(List&lt;String&gt;.valueOf("&quot;one&quot;")), TWO(List&lt;String&gt;.valueOf("&quot;two&quot;"));


    private List&lt;String&gt; value;

    List&lt;EnumTestArrayEnum&gt; (List&lt;String&gt; v) {
        value = v;
    }

    public String value() {
        return value;
    }

    public static List&lt;EnumTestArrayEnum&gt; fromValue(String v) {
        return valueOf(v);
    }
}

How to define array of enum property in YML?

您的 enumTestArray 示例几乎是正确的——您只需要在“-”和 "two" 之间添加一个 space 即可使 YAML 有效:

           enumTestArray:
              type: array
              items:
                 type: string
                 enum:
                  - one
                  - two  # <----

How to define map property enum as key and boolean as value in YML?

在OpenAPI/Swagger中,映射键是任意字符串,它是。您可以在 description.

中口头记录密钥格式

或者,由于键是已知的(限于某些已知的枚举),您可以将所有可能的键定义为可选属性。不优雅,但它可能适合你。

              testMap:
                type: object
                properties:
                  one:
                    type: boolean
                  two:
                    type: boolean
                  ...

还提议添加 support for patternProperties,这将允许将键名称限制为正则表达式。