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
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<EnumTestArrayEnum>")
@XmlEnum
public enum List<EnumTestArrayEnum> {
ONE(List<String>.valueOf(""one"")), TWO(List<String>.valueOf(""two""));
private List<String> value;
List<EnumTestArrayEnum> (List<String> v) {
value = v;
}
public String value() {
return value;
}
public static List<EnumTestArrayEnum> fromValue(String v) {
return valueOf(v);
}
}
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.