swagger-codegen
swagger-codegen copied to clipboard
[Python] Map enum validation looks at keys, not values
Description
The setter method in the Python model.mustache template has special handling for list and map properties which are also tagged as enumerated. The list validation checks the list members as expected, but the map validation checks the map keys, when it should check the values. Therefore, given a map property with string keys and enumerated values, the check fails because the map keys are not in the enumeration. This causes deserialization to fail.
Swagger-codegen version
2.3.1
Swagger declaration file content or url
Using Swagger annotations on a Java model class, with enum type MyEnum:
@ApiModelProperty
private final Map<String, MyEnum> mapOfMyEnum;
Command line used for generation
Generated using Maven plugin 2.3.1.
Steps to reproduce
- Generate a model class with a property that is a map with string keys and enum values. Note that the allowed_values variable in the setter method for the property lists what's permitted for the enum type, not for keys.
- Create a model object with valid values in the map.
- Serialize and then deserialize the object.
Related issues/PRs
- https://github.com/swagger-api/swagger-codegen/issues/3712
- https://github.com/swagger-api/swagger-codegen/pull/3713 - PR that introduced code in question
Suggest a fix/enhancement
Switch "keys" to "values" in Python model.mustache.
if not set({{{name}}}.values()).issubset(set(allowed_values)):
raise ValueError(
"Invalid values in `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
.format(", ".join(map(str, set({{{name}}}.values()) - set(allowed_values))), # noqa: E501
", ".join(map(str, allowed_values)))
)
This seems to be still the case in the latest 2.4.41 version.