OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON][MUSTACHE] Generic model wrong validator for enums.

Open
#17,274 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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
  1. Create OpenAPI definition with the schema
  2. Generate python SDK
  3. Try to validate model with object property set to redemption
Related issues/PRs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.