swagger-api / swagger-api/swagger-codegen

[Python] Map enum validation looks at keys, not values

Open
#9,138 1 comment 0 reactions 0 assignees View on GitHub

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
  1. 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.
  2. Create a model object with valid values in the map.
  3. Serialize and then deserialize the object.
Related issues/PRs
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.