Closed teamush closed 1 year ago
It seems you can not resolve it in winter but it can be resolved with changing enum like this Because converting from string to Enum is not trivial (my opinion)
class FooParam2(IntEnum): # The same with FooParam1
FOO = 1
BAR = 2
@classmethod
def _missing_(cls, value):
if isinstance(value, str) and value.isdigit():
return cls(int(value))
return super()._missing_(value)
Trivial or not, I'd like we implement it! :-)
Because of it we will have bug(or not) with body: if we have enum with different types(int and string): '1' and 1 which one to use is not defined. Also with this implementation body {"param:": 1} and {"param": "1"} will valid for
@dataclasses.dataclass
class Data:
param: FooParam2
But I think only {"param:": 1} valid and {"param": "1"} is not valid But I think the same with int(I haven't tried), so it is ok
Well, I can understand the concern about having heterogeneous data types inside Enum
inheritors, but for IntEnum
it shouldn't be a problem, as only integers could be used as it's values, but anyway, thanks for clarification and tips.
@berenda-andrey we should probably distinguish between body and query string. In case it's a query string, we can understand that everything there is a string... And we could help with converting it to integer, no?
Both IntEnum and heterogeneous Enum are inspectable at the start time. So I think it can and should be implemented for both original use cases.
@pristupa Yes. I think distinguishing between body and query string can help
@teamush Please notice that the fix will go to 4.2.0. So probably it's a good time to update from 2.1.1 to 4.1.0, look at CHANGELOG.md for migration hints.
Bug description When the argument of GET route is annotated as integer-based enum (
Enum
with integer values orIntEnum
), type conversion fails - received value is perceived as string, which leads to following errors:convert_enum
function fails withValue not in allowed values("1", "2"): "1"'
if pureEnum
with integers is used;convert_int
function fails withCannot convert "1" to integer
ifIntEnum
is used.To Reproduce
route_get
;route_get
like:@route_get('/hello/{?param1,param2}')
;param1=1
orparam2=1
; 5.1 Ifparam1
is given, endpoint returnsHTTP 400 Bad Request
with following error:Value not in allowed values("1", "2"): "1"'
5.2 Ifparam2
is given, endpoint returnsHTTP 400 Bad Request
with following error:Cannot convert "1" to integer
.Expected behavior Parameter(s) are successfully resolved as enum values (or integers) and their belonging to the enum is validated.
Code snippets Minimal code snippet is given above.
Screenshots Screenshots are saying
TemporaryHiringStatus
andHiringStatus
enums, sorry for that, butTemporaryHiringStatus
stands forFooParam1
in the example above, andHiringStatus
forFooParam2
.Environment(please complete the following information):