Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom lookup serializer for class error [API-1669] #603

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hazelcast/serialization/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ def lookup_custom_serializer(self, obj_type):
serializer = self._type_dict.get(obj_type, None)
if serializer is not None:
return serializer
for super_type in obj_type.__subclasses__():

for super_type in obj_type.__mro__:
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
serializer = self.register_from_super_type(obj_type, super_type)
if serializer is not None:
return serializer
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/serialization/custom_global_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def __eq__(self, other):
return False


class TheOtherCustomClass(CustomClass):
def __init__(self, uid, name, text, source=None):
super(TheOtherCustomClass, self).__init__(uid, name, text, source)

def __eq__(self, other):
return super(TheOtherCustomClass, self).__eq__(other)


class CustomSerializer(StreamSerializer):
def write(self, out, obj):
if isinstance(obj, CustomClass):
Expand Down Expand Up @@ -128,3 +136,30 @@ def test_double_register_custom_serializer(self):

with self.assertRaises(ValueError):
service._registry.safe_register_serializer(TheOtherCustomSerializer, CustomClass)

mdumandag marked this conversation as resolved.
Show resolved Hide resolved
def test_serializing_class_instances(self):
config = Config()
config.custom_serializers = {CustomClass: CustomSerializer}
service = SerializationServiceV1(config)

data1 = service.to_data(CustomClass)
deserialized1 = service.to_object(data1)

self.assertEqual(CustomClass, deserialized1)

data2 = service.to_data(TheOtherCustomClass)
deserialized2 = service.to_object(data2)

self.assertEqual(TheOtherCustomClass, deserialized2)
mdumandag marked this conversation as resolved.
Show resolved Hide resolved

def test_serializing_child_class_instances_with_super_class_serializer(self):
config = Config()
config.custom_serializers = {TheOtherCustomClass: TheOtherCustomSerializer}
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
service = SerializationServiceV1(config)

obj = TheOtherCustomClass("uid", "some name", "description text", "CUSTOM")
data = service.to_data(obj)
deserialized = service.to_object(data)

self.assertTrue(isinstance(deserialized, CustomClass))
self.assertEqual(obj, deserialized)
mdumandag marked this conversation as resolved.
Show resolved Hide resolved