fix(python): validate map values (not keys) for additionalProperties.enum#1405
Open
pvillard31 wants to merge 1 commit intoswagger-api:masterfrom
Open
fix(python): validate map values (not keys) for additionalProperties.enum#1405pvillard31 wants to merge 1 commit intoswagger-api:masterfrom
pvillard31 wants to merge 1 commit intoswagger-api:masterfrom
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
pythonandpythonFlaskConnexiongenerator templates produced setter validation that compared map keys to the enum, while OpenAPI 3.0additionalProperties.enumconstrains map values. Any realistic payload with arbitrary string keys (parameter names, identifiers, tags, etc.) failed deserialization with a spuriousValueError.This PR:
{{#isMapContainer}}branch inside{{#isEnum}}→{{#isContainer}}insrc/main/resources/handlebars/python/model.mustache(lines 131-138) andsrc/main/resources/handlebars/pythonFlaskConnexion/model.mustache(lines 101-108) so setters validate.values()rather than.keys()and the error message readsInvalid values in ...rather thanInvalid keys in ....PythonMapEnumValidationTest, that uses the existing3_0_0/map_of_inner_enum.yamlfixture andGeneratorRunner.runGeneratorto assert the generated Python model contains the corrected validation.The fix is byte-minimal: six substitutions across the two templates.
Before (broken)
After (correct)
Fixes #1404
Type of Change
Checklist
How to Test
./mvnw clean test-compile— should succeed.swagger-codegen-cli 3.0.77from a spec containing aMap<String, Enum>property (for example,3_0_0/map_of_inner_enum.yamlin this repo). Inspectemployee_with_map_of_enum.py; prior to this fix, the setter forproject_rolevalidates.keys()against the enum..values()and the error message readsInvalid values in 'project_role'.PythonMapEnumValidationTest.mapOfEnumSetterValidatesValuesNotKeysexercises 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 currentpom.xmlSurefire configuration has<testNGArtifactName>none:none</testNGArtifactName>at line 93, which excludes TestNG from defaultmvn test/mvn verifyruns. 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 withswagger-codegen-cli 3.0.68. The NiFi spec correctly places the enum onadditionalProperties(per OAS 3.0 semantics), but the generated Python client rejected every response because of theInvalid 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.