[ 
https://issues.apache.org/jira/browse/THRIFT-5885?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Adrian Stachlewski updated THRIFT-5885:
---------------------------------------
    Description: 
When using {{TBinaryProtocolAccelerated}}, {{IntEnum}} fields are incorrectly 
deserialized to {{None}}.

This issue occurs when the {{read}} function is executed via {{\_fast_decode}}. 
In this path, the custom deserialization logic {{CustomEnum(iprot.readI32())}} 
is skipped. Instead, an integer value ({{i32}}) is directly passed to 
{{\_\_setattr\_\_}}, where the value is resolved using:
{code:python}
Operation.__members__.get(value) {code}
However, {{\_\_members\_\_}} maps enum names (not values) to enum members, 
which results in the call returning None. This leads to the enum field being 
set to {{None}} instead of the expected enum instance. A minimal and effective 
fix could be to update the {{\_\_setattr\_\_}} method logic to:
{code:python}
if name == "enum_field":
    super().__setattr__(name, value if hasattr(value, 'value') or value is None 
else Operation(value)) {code}
This would ensure that the enum value is correctly reconstructed from its 
integer representation during deserialization.
 

  was:
When using {{TBinaryProtocolAccelerated}}, {{IntEnum}} fields are incorrectly 
deserialized to {{None}}.

This issue occurs when the {{read}} function is executed via {{\_fast_decode}}. 
In this path, the custom deserialization logic {{CustomEnum(iprot.readI32())}} 
is skipped. Instead, an integer value ({{i32}}) is directly passed to 
{{__setattr__}}, where the value is resolved using:
{code:python}
Operation.__members__.get(value) {code}
However, __members__ maps enum names (not values) to enum members, which 
results in the call returning None. This leads to the enum field being set to 
None instead of the expected enum instance. A minimal and effective fix could 
be to update the __setattr__ method logic to:
{code:python}
if name == "enum_field":
    super().__setattr__(name, value if hasattr(value, 'value') or value is None 
else Operation(value)) {code}
This would ensure that the enum value is correctly reconstructed from its 
integer representation during deserialization.
 


> TBinaryProtocolAccelerated incorrectly deserializes IntEnum to None
> -------------------------------------------------------------------
>
>                 Key: THRIFT-5885
>                 URL: https://issues.apache.org/jira/browse/THRIFT-5885
>             Project: Thrift
>          Issue Type: Bug
>          Components: Python - Compiler, Python - Library
>    Affects Versions: 0.22.0
>         Environment: thrift==0.22.0
>            Reporter: Adrian Stachlewski
>            Priority: Major
>
> When using {{TBinaryProtocolAccelerated}}, {{IntEnum}} fields are incorrectly 
> deserialized to {{None}}.
> This issue occurs when the {{read}} function is executed via 
> {{\_fast_decode}}. In this path, the custom deserialization logic 
> {{CustomEnum(iprot.readI32())}} is skipped. Instead, an integer value 
> ({{i32}}) is directly passed to {{\_\_setattr\_\_}}, where the value is 
> resolved using:
> {code:python}
> Operation.__members__.get(value) {code}
> However, {{\_\_members\_\_}} maps enum names (not values) to enum members, 
> which results in the call returning None. This leads to the enum field being 
> set to {{None}} instead of the expected enum instance. A minimal and 
> effective fix could be to update the {{\_\_setattr\_\_}} method logic to:
> {code:python}
> if name == "enum_field":
>     super().__setattr__(name, value if hasattr(value, 'value') or value is 
> None else Operation(value)) {code}
> This would ensure that the enum value is correctly reconstructed from its 
> integer representation during deserialization.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to