5

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);
    }
}
Vít Kotačka
  • 1,472
  • 1
  • 15
  • 40
nayakam
  • 4,149
  • 7
  • 42
  • 62

1 Answers1

8

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.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • enumTestArray does not work. I missed the space when format the question. Could not get array of enum property. Thanks for the suggestion related to map property. – nayakam Oct 09 '17 at 13:54
  • enumTestArray is the correct way to define an array of enums. How exactly does it not work? Which tool(s) are you using, what result do you get and what is the expected result? – Helen Oct 09 '17 at 14:31
  • I have updated the question look like an issue with java code generation. Typescript file looks good. – nayakam Oct 10 '17 at 04:01