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

Jens Geyer updated THRIFT-5693:
-------------------------------
    Summary: python enum fields with specified default value not serializable  
(was: [python] enum fields with specified default value not serializable)

> python enum fields with specified default value not serializable
> ----------------------------------------------------------------
>
>                 Key: THRIFT-5693
>                 URL: https://issues.apache.org/jira/browse/THRIFT-5693
>             Project: Thrift
>          Issue Type: Bug
>          Components: Python - Compiler
>            Reporter: Andrew Cunningham
>            Priority: Major
>
> In the generated Python code, default values for enums are set as enum 
> objects (ie, <name: value>), but thrift objects expect this value to be 
> stored either by its name _or_ its value. This results in a keyerror during 
> the write method. 
> For example, if I have the following thrift schema
>  
> {code:java}
> enum MyEnum {
>   test_a,
>   test_b,
>   test_c
> }
> struct MyStruct {
>   1: MyEnum enum_field = MyEnum.test_c
> }
>  
> {code}
> and then in the python code:
> {code:java}
> new_struct = MyStruct()
> thrift.TSerialization.serialize(new_struct)
> {code}
> it'd get a `keyerror` from the code generated 
> [here|https://github.com/apache/thrift/blob/0.17.0/compiler/cpp/src/thrift/generate/t_py_generator.cc#L2479]
>  
> {code:java}
> oprot.writeI32(MyEnum[new_struct.enum_field].value)
>   File "[my-path]/lib/python3.9/enum.py", line 432, in __getitem__
>     return cls._member_map_[name]
> KeyError: <MyEnum.test_c: 2> {code}



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

Reply via email to