Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using swagger-codegen-maven-plugin (2.2.1) to generate java and typescript code class files from YML configuration. I have two questions.

How to define array of enum property in YML?

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

Let me know is it possible or is there any workaround? Currently, I defined enum class in java and typescrtipt and pass it as string. Thanks.

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

Updated:

Related to first question: Define array of enum property. swagger-codegen-maven-plugin generate invalid java class file as follows: Look like and issue with generating <, > and " characters.

@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);
    }
}
like image 470
nayakam Avatar asked Oct 09 '17 04:10

nayakam


1 Answers

How to define array of enum property in YML?

Your enumTestArray example is almost correct – you just need a space between "-" and "two" to make the YAML valid:

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

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

In OpenAPI/Swagger, the map keys are arbitrary strings and it's not possible to limit the key names or format. You can document the key format verbally in the description.

Alternatively, since the keys are known (limited to some known enum), you can define all possible keys as optional properties. Not elegant, but it might work for you.

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

There's also proposal to add support for patternProperties, which would allow limiting the key names to a regular expression.

like image 56
Helen Avatar answered Oct 29 '22 03:10

Helen