swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.08k stars 6.03k forks source link

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

Open bhavanki opened 5 years ago

bhavanki commented 5 years ago
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)))
            )
michaelarnauts commented 5 months ago

This seems to be still the case in the latest 2.4.41 version.