Skip to content

Commit

Permalink
fix enum serialization of default values
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcunnin committed Apr 17, 2023
1 parent 66aac44 commit e6dd0a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/cpp/src/thrift/generate/t_py_generator.cc
Expand Up @@ -2287,7 +2287,7 @@ void t_py_generator::generate_deserialize_field(ostream& out,
out << endl;
} else if (type->is_enum()) {
if (gen_enum_) {
indent(out) << name << " = " << type_name(type) << "(iprot.readI32()).name";
indent(out) << name << " = " << type_name(type) << "(iprot.readI32()).value";
} else {
indent(out) << name << " = iprot.readI32()";
}
Expand Down Expand Up @@ -2477,7 +2477,7 @@ void t_py_generator::generate_serialize_field(ostream& out, t_field* tfield, str
}
} else if (type->is_enum()) {
if (gen_enum_){
out << "writeI32(" << type_name(type) << "[" << name << "].value)";
out << "writeI32(" << type_name(type) << "(" << name << ").value)";
} else {
out << "writeI32(" << name << ")";
}
Expand Down
4 changes: 4 additions & 0 deletions test/ThriftTest.thrift
Expand Up @@ -420,6 +420,10 @@ struct OptionalSetDefaultTest {
1: optional set<string> with_default = [ "test" ]
}

struct OptionalEnum {
1: optional Numberz e = Numberz.FIVE;
}

struct OptionalBinary {
1: optional set<binary> bin_set = {}
2: optional map<binary,i32> bin_map = {}
Expand Down
11 changes: 11 additions & 0 deletions test/py/SerializationTest.py
Expand Up @@ -33,6 +33,7 @@
VersioningTestV2,
Xtruct,
Xtruct2,
OptionalEnum,
)

from Recursive.ttypes import RecTree
Expand Down Expand Up @@ -193,6 +194,8 @@ def setUp(self):
]
)

self.optional_enum = OptionalEnum()

def _serialize(self, obj):
trans = TTransport.TMemoryBuffer()
prot = self.protocol_factory.getProtocol(trans)
Expand Down Expand Up @@ -351,6 +354,12 @@ def testRecVector(self):
out_list = self._collapseLinkedList(cur_list)
self.assertEqual(golden_list, out_list)

def testDefaultEnum(self):
"""Ensure default enum values are serializable"""
obj = self._deserialize(OptionalEnum, self._serialize(self.optional_enum))
self.assertEquals(obj, self.optional_enum)



class NormalBinaryTest(AbstractTest):
protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Expand Down Expand Up @@ -451,6 +460,8 @@ def _enumerate_enum(enum_class):
self.assertEquals(obj, objcopy)




def suite():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
Expand Down
4 changes: 4 additions & 0 deletions test/v0.16/ThriftTest.thrift
Expand Up @@ -416,3 +416,7 @@ struct OptionalBinary {
1: optional set<binary> bin_set = {}
2: optional map<binary,i32> bin_map = {}
}

struct OptionalEnum {
1: optional Numberz e = Numberz.FIVE;
}

0 comments on commit e6dd0a6

Please sign in to comment.