OpenAPITools / OpenAPITools/openapi-generator
[BUG][PYTHON][MUSTACHE] Generic model wrong validator for enums.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
model_generic.mustache has wrong implementation for def {{{name}}}_validate_enum(cls, value): function due to creating validation for enum with single value. See more details below.
For me it looks like a bug but maybe for someone this is correct behaviour so it could be breaking change.
openapi-generator version
7.0.1 7.1.0 7.2.0 have this bug
OpenAPI declaration file content or url
Main schema:
"RedemptionRollback": {
"title": "Redemption Rollback",
"type": "object",
"description": "This is an object representing a redemption rollback.",
"properties": {
"object": {
"type": "string",
"description": "The type of object represented by the JSON",
"enum": [
"redemption_rollback"
]
}
}
}
Generation Details
Generated
@validator('object')
def object_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('redemption_rollback'):
raise ValueError("must be one of enum values ('redemption_rollback')")
return value
Should be
@validator('object')
def object_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('redemption_rollback',):
raise ValueError("must be one of enum values ('redemption_rollback')")
return value
When generating the single element tuple it's treated as a string not tuple. Consequently if value not in ('redemption_rollback') will return False even when value would be redemption or rollback.
Steps to reproduce
- Create OpenAPI definition with the schema
- Generate python SDK
- Try to validate model with
objectproperty set toredemption
Related issues/PRs
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 model_generic.mustache and inspect the {{{name}}}_validate_enum implementation described in the issue. Generate a Python SDK from the provided single-value enum schema, then validate both an allowed value and an invalid value; done means the generated validator rejects values such as redemption or rollback while accepting redemption_rollback.
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
- 25/100