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