swagger-api / swagger-api/swagger-codegen-generators
[Bug]: Python/pythonFlaskConnexion generators validate map keys instead of values for additionalProperties.enum
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
Description
The python and pythonFlaskConnexion generators emit incorrect setter validation for properties of type Map<String, Enum> declared in OpenAPI 3.0 as:
type: object
additionalProperties:
type: string
enum: [A, B]
Per OpenAPI 3.0, additionalProperties describes the shape of map values, so an enum inside it constrains the values. The generated Python setter validates the map keys instead, which is both semantically wrong and breaks deserialization of legitimate API responses whose keys are arbitrary strings.
Affected templates
src/main/resources/handlebars/python/model.mustache, lines 131-138 ({{#isMapContainer}}branch inside{{#isEnum}}→{{#isContainer}}).src/main/resources/handlebars/pythonFlaskConnexion/model.mustache, lines 101-108 (same{{#isMapContainer}}branch).
Both blocks currently emit:
if not set(x.keys()).issubset(set(allowed_values)):
raise ValueError(
"Invalid keys in `x` [{0}], must be a subset of [{1}]"
.format(", ".join(map(str, set(x.keys()) - set(allowed_values))),
", ".join(map(str, allowed_values)))
)
They should emit:
if not set(x.values()).issubset(set(allowed_values)):
raise ValueError(
"Invalid values in `x` [{0}], must be a subset of [{1}]"
.format(", ".join(map(str, set(x.values()) - set(allowed_values))),
", ".join(map(str, allowed_values)))
)
Version
v1.0.77 (also reproduced against current master).
Can you identify when the issue first appeared?
The same broken branch exists at least as far back as v1.0.41 (and in the legacy swagger-api/swagger-codegen v2 Python template), so the bug is long-standing. Bumping from 3.0.68 to 3.0.77 does not fix it.
Language / Generator Affected
pythonpythonFlaskConnexion
OpenAPI/Swagger Spec
Minimal reproducer:
openapi: 3.0.1
info:
title: repro
version: "1.0"
paths: {}
components:
schemas:
ParameterSensitivityMap:
type: object
properties:
sensitivities:
type: object
additionalProperties:
type: string
enum:
- SENSITIVE
- NON_SENSITIVE
This mirrors the real-world shape emitted by Apache NiFi for ParameterGroupConfigurationEntity.parameterSensitivities — a map of arbitrary parameter names to a sensitivity enum.
Command Line or Configuration Used
swagger-codegen generate -l python -i repro.yaml -o out
Steps to Reproduce
- Save the spec above as
repro.yaml. - Run the command above using
swagger-codegen-cli 3.0.77(which pulls this repo'sv1.0.77templates). - Open
out/swagger_client/models/parameter_sensitivity_map.pyand inspect the setter forsensitivities.
Expected Behavior
The setter should validate the map values against the enum:
allowed_values = ["SENSITIVE", "NON_SENSITIVE"]
if not set(sensitivities.values()).issubset(set(allowed_values)):
raise ValueError(
"Invalid values in `sensitivities` [{0}], must be a subset of [{1}]"
...
)
So {"POSTGRES_PWD": "SENSITIVE", "DB_HOST": "NON_SENSITIVE"} is accepted, and {"POSTGRES_PWD": "BOGUS"} raises.
Actual Behavior
The setter validates the map keys against the enum:
allowed_values = ["SENSITIVE", "NON_SENSITIVE"]
if not set(sensitivities.keys()).issubset(set(allowed_values)):
raise ValueError(
"Invalid keys in `sensitivities` [{0}], must be a subset of [{1}]"
...
)
Any realistic payload raises on deserialization, for example:
ValueError: Invalid keys in `parameter_sensitivities` [POSTGRES_PWD], must be a subset of [SENSITIVE, NON_SENSITIVE]
Related Issues / Repos
Discovered via the Apache NiFi REST API → nipyapi Python client, which is generated from NiFi's OpenAPI 3.0 spec using this repo's Python template. Tracking locally for the nipyapi-side workaround is planned; this issue is the root-cause fix.
No existing issue or PR in swagger-api/swagger-codegen-generators or swagger-api/swagger-codegen matches this bug (searched titles/bodies for additionalProperties enum python, map keys issubset, Invalid keys map, isMapContainer, parameterSensitivities, python Map String Enum, etc.).
Environment
OS: macOS 14
Java Version: OpenJDK 17
Build Tool: CLI (swagger-codegen-cli 3.0.77) / Maven
swagger-codegen-generators Version: v1.0.77 (also reproduces on master)
Additional Context
Proposed fix: in both templates, change the {{#isMapContainer}} block under {{#isEnum}} → {{#isContainer}} so it iterates .values() instead of .keys() and renames the error message from Invalid keys in to Invalid values in. The adjacent {{#isListContainer}} branch is already correct and does not need changes. I intend to open a PR with this fix plus a regression test using the existing GeneratorRunner harness.
Checklist
- I have searched the existing issues to avoid duplicates.
- I have included a minimal reproducible spec.
- I have clearly described the steps to reproduce the issue.
- I have specified which generator or template is affected.
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 with the isMapContainer branches in src/main/resources/handlebars/python/model.mustache and src/main/resources/handlebars/pythonFlaskConnexion/model.mustache, then review the existing GeneratorRunner harness. Confirm the generated setters validate map values rather than keys, preserve the existing list-container behavior, and add a regression test covering the provided OpenAPI map-of-enum example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 86/100