OpenAPITools / OpenAPITools/openapi-generator

[REQ] python-flask enums needs allowable_values as per other python types

Open
#10,953 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Se https://github.com/OpenAPITools/openapi-generator/pull/10954

We need a collection of allowable_values on the generated flask enum classes so that we can validate the string values before transmitting them.

allowable_values = [ HAPPPY, SAD ]

Describe the solution you'd like

A product like this the below that carries the allowable values and a utility function 'of' for validation.
Notice '######### ADDITIONAL LINES START' and '######### ADDITIONAL LINES END'

# coding: utf-8

from __future__ import absolute_import
from datetime import date, datetime  # noqa: F401

from typing import List, Dict  # noqa: F401

from petstore.models.base_model_ import Model
from petstore import util


class MyEnum(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    """
    allowed enum values
    """
    HAPPY = "happy"
    SAD = "sad"

    ######### ADDITIONAL LINES START
    allowable_values = [HAPPY, SAD]  # noqa: E501
    enum_items = {  "HAPPY" : HAPPY,  "SAD" : SAD }  # noqa: E501

    @staticmethod
    def of(value):
        """
        return value if it is a valid enum value or none
        """
        if not value or value in MyEnum.allowable_values:
            return value

        raise ValueError(
            "Invalid enum value for `MyEnum` ({0}), must be one of {1}"  # noqa: E501
            .format(value, MyEnum.allowable_values)
        )
    ######### ADDITIONAL LINES END

    def __init__(self):  # noqa: E501
        """MyEnum - a model defined in OpenAPI

        """
        self.openapi_types = {
        }

        self.attribute_map = {
        }

    @classmethod
    def from_dict(cls, dikt) -> 'MyEnum':
        """Returns the dict as a model

        :param dikt: A dict.
        :type: dict
        :return: The MyEnum of this MyEnum.  # noqa: E501
        :rtype: MyEnum
        """
        return util.deserialize_model(dikt, cls)

Which allows a usage like ....

    candidate_value = "happy"
    some_coms_field = MyEnum.of(candidate_value)

.. so that we get a validation of the value 'candidate_value' before it is serialised to JSON

Describe alternatives you've considered

None

Additional context

Many other variants have 'allowable_values'

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

The issue targets generated python-flask enum classes and requests allowable_values, enum_items, and an of validator. Start by locating the python-flask enum template and comparing it with variants that already provide allowable_values. Done means generated enum classes expose those values and validate candidates before serialization; add or update relevant generator coverage if the repository has it.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, java, python
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.