[REQ] python-flask enums needs allowable_values as per other python types
Created by: Johnlon
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'