Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #14785
Closed
Open
Issue created Feb 21, 2023 by Administrator@rootContributor

[BUG][python-nextgen] validation of list of enums doesn't work

Created by: t0mk

The template in https://github.com/OpenAPITools/openapi-generator/blob/18ef6ff96a00c5c3895d428679352b3af759fbdd/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache#L44

    def {{{name}}}_validate_enum(cls, v):
        {{^required}}
        if v is None:
            return v

        {{/required}}
        if v not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
            raise ValueError("must validate the enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
        return v

.. is called also when "v" is a list, and it checks ["v1", "v2"] not in ["v1","v2"], and fails for a legit use case.

It should be sth like

    def {{{name}}}_validate_enum(cls, v):
        {{^required}}
        if v is None:
            return v

        {{/required}}
        if type(v) is list:
            for i in v:
                if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
                    raise ValueError("each list item must be one of ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
        else:
            if v not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
                raise ValueError("must be on of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")

        return v

Example of enum list can be found in spec at: https://github.com/t0mk/metal-python/blob/05f4599ebe3ee42727bf5117403571a739fa52ba/metal_openapi.fixed.yaml#L2662

I'm a bit in a hurry but I hope I wrote it clear.

Assignee
Assign to
Time tracking