I have the following api definition for flask-restx (though should also work with flask-restplus).
Is there someway to convert the enum-field in the request body the the Enum MyEnum without too much overhead or using DAOs?
class MyEnum(Enum):
FOO = auto()
BAR = auto()
@dataclass(frozen=True)
class MyClass:
enum: MyEnum
api = Namespace('ns')
model = api.model('Model', {
'enum': fields.String(enum=[x.name for x in MyEnum]),
})
@api.route('/')
class MyClass(Resource):
@api.expect(Model)
def post(self) -> None:
c = MyClass(**api.payload)
print(type(c.enum)) # <class 'str'> (but I want <enum 'MyEnum'>)
assert(type(c.enum) == MyEnum) # Fails
Ok I have written a decorator which will replace the enum value with the enum
The usage would be like this
The
replace_itemfunction was inspired by this SO Answer: https://stackoverflow.com/a/45335542/6900162