swagger-api / swagger-api/swagger-codegen
[Python] Map enum validation looks at keys, not values
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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)))
)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Python model.mustache template and the setter validation for map properties tagged as enumerated. Reproduce the described map<string, MyEnum> serialization and deserialization case, then verify that valid enum values no longer cause deserialization to fail.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100