Open bruno-robert opened 2 years ago
Have you tried class Formats(str, Enum):
Sorry for the delayed reply. First off, thank you for the response! I have been very busy lately and haven't had time to try this. I'll update this thread when I do ;)
I'm running into the same issue. @cardoe is the ask here to have all enum classes extend str
? This seems like it would have adverse effects on type safety
I'm running into the same issue. @cardoe is the ask here to have all enum classes extend
str
? This seems like it would have adverse effects on type safety
No. You need to extend it with the actual type you are serializing your enum data out with. JSON cannot encode enum's natively so you use another type. So in OPs example they are using str
values. So they need to do that. If you use numbers then you should use int
. Does that make sense?
This error is occurring for me when the body of an endpoint receives a
pydantic.BaseModel
that contains a field of typeEnum
and the validation fail, the error message returned is a JSON serialization error instead of a ValidationError.Example validation that fail correctly:
response:
the response details why the validation fails
Example validation that fails with a JSON serialization error because of an
Enum
:response (with stack trace):
When I was expecting an response of type:
It seems like the flask's JSONEncoder is used instead of std json. If I modify
flask.json.JSONEncoder
'sdefault
method in order to add:the program functions as expected.