Skip to content

fix(python): validate map values (not keys) for additionalProperties.enum#1405

Open
pvillard31 wants to merge 1 commit intoswagger-api:masterfrom
pvillard31:fix/python-map-enum-validates-values
Open

fix(python): validate map values (not keys) for additionalProperties.enum#1405
pvillard31 wants to merge 1 commit intoswagger-api:masterfrom
pvillard31:fix/python-map-enum-validates-values

Conversation

@pvillard31
Copy link
Copy Markdown

Description

The python and pythonFlaskConnexion generator templates produced setter validation that compared map keys to the enum, while OpenAPI 3.0 additionalProperties.enum constrains map values. Any realistic payload with arbitrary string keys (parameter names, identifiers, tags, etc.) failed deserialization with a spurious ValueError.

This PR:

  • Fixes the {{#isMapContainer}} branch inside {{#isEnum}}{{#isContainer}} in src/main/resources/handlebars/python/model.mustache (lines 131-138) and src/main/resources/handlebars/pythonFlaskConnexion/model.mustache (lines 101-108) so setters validate .values() rather than .keys() and the error message reads Invalid values in ... rather than Invalid keys in ....
  • Adds a regression test, PythonMapEnumValidationTest, that uses the existing 3_0_0/map_of_inner_enum.yaml fixture and GeneratorRunner.runGenerator to assert the generated Python model contains the corrected validation.

The fix is byte-minimal: six substitutions across the two templates.

Before (broken)

allowed_values = ["DEVELOPER", "TESTER", "OWNER"]
if not set(project_role.keys()).issubset(set(allowed_values)):
    raise ValueError(
        "Invalid keys in `project_role` [{0}], must be a subset of [{1}]"
        ...
    )

After (correct)

allowed_values = ["DEVELOPER", "TESTER", "OWNER"]
if not set(project_role.values()).issubset(set(allowed_values)):
    raise ValueError(
        "Invalid values in `project_role` [{0}], must be a subset of [{1}]"
        ...
    )

Fixes #1404

Type of Change

  • Bug fix
  • New feature
  • Refactor (non-breaking change)
  • Tests
  • Documentation
  • Chore (build or tooling)

Checklist

  • I have added/updated tests as needed
  • I have added/updated documentation where applicable
  • The PR title is descriptive
  • The code builds and passes tests locally (see note below about TestNG runner)
  • I have linked related issues

How to Test

  1. Check out the branch and build: ./mvnw clean test-compile — should succeed.
  2. Reproduce the original bug (before the fix) by generating Python with swagger-codegen-cli 3.0.77 from a spec containing a Map<String, Enum> property (for example, 3_0_0/map_of_inner_enum.yaml in this repo). Inspect employee_with_map_of_enum.py; prior to this fix, the setter for project_role validates .keys() against the enum.
  3. After applying this PR, regenerate from the same spec and verify the setter validates .values() and the error message reads Invalid values in 'project_role'.
  4. Automated: PythonMapEnumValidationTest.mapOfEnumSetterValidatesValuesNotKeys exercises the end-to-end generation and asserts on the generated file contents.

Note on test execution

The project uses TestNG for its tests (see the rest of src/test/java/.../python/, .../java/, etc.), and this PR follows that convention. The current pom.xml Surefire configuration has <testNGArtifactName>none:none</testNGArtifactName> at line 93, which excludes TestNG from default mvn test / mvn verify runs. As a result this and all other TestNG tests in the suite do not execute in the default CI pipeline today — this is a pre-existing, project-wide situation, not something introduced by this PR. The new test compiles cleanly and can be executed from an IDE (IntelliJ, Eclipse) or by temporarily overriding the Surefire config. Happy to align with whatever runner strategy maintainers prefer if you'd like the test refactored.

Screenshots / Additional Context

Discovered while integrating an OpenAPI 3.0 spec from Apache NiFi (ParameterGroupConfigurationEntity.parameterSensitivities) into nipyapi, which generates its Python client with swagger-codegen-cli 3.0.68. The NiFi spec correctly places the enum on additionalProperties (per OAS 3.0 semantics), but the generated Python client rejected every response because of the Invalid keys in \parameter_sensitivities`check. The same bug exists through at leastv1.0.77`.

See issue #1404 for a minimal reproducer spec and additional context.

…enum

The python and pythonFlaskConnexion model.mustache templates emitted setters
that validated Map<String, Enum> keys against the enum instead of validating
the values. Per OpenAPI 3.0, additionalProperties describes map values, so
an enum inside it constrains values. The current templates produce code that
rejects any realistic payload whose keys are arbitrary strings.

Fix the {{#isMapContainer}} branch in both templates to iterate .values()
instead of .keys() and update the error message from "Invalid keys in" to
"Invalid values in".

Add PythonMapEnumValidationTest that uses the existing
3_0_0/map_of_inner_enum.yaml fixture and the GeneratorRunner harness to
assert the generated setter validates values.

Fixes swagger-api#1404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Python/pythonFlaskConnexion generators validate map keys instead of values for additionalProperties.enum

1 participant