Skip to content

Commit

Permalink
fix optional type deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
keotl committed Dec 26, 2019
1 parent 2753c6b commit fbe4c98
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def deserialize(self, obj, declared_type: Optional[T]) -> Optional[T]:
if obj is None:
return obj

non_nil_declared_type = Stream(declared_type.__args__).firstMatch(lambda x: x != type(None))
non_nil_declared_type = Stream(declared_type.__args__).firstMatch(lambda x: x != type(None)).get()
return self.deserializer.deserialize(obj, non_nil_declared_type)
6 changes: 6 additions & 0 deletions test/serialization/test_dto_serialization_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def test_givenMissingKey_whenDeserializingOptionalParameter_thenAssignNoneToTheA
self.assertIsInstance(dto, ADtoWithOptionalValue)
self.assertIsNone(dto.name)

def test_givenOptionalParameter_whenValueIsPresent_thenAssignTheParameter(self):
dto = self.serialization_handler.deserialize({"name": "foubraque"}, ADtoWithOptionalValue)

self.assertIsInstance(dto, ADtoWithOptionalValue)
self.assertEqual("foubraque", dto.name)

def test_givenNestedDtos_whenSerializing_thenRecursivelySerializeDtos(self):
child = ChildDto()
child.name = "a name"
Expand Down
26 changes: 26 additions & 0 deletions test/serialization/test_list_deserialization_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest

from jivago.serialization.deserialization.list_deserialization_strategy import ListDeserializationStrategy
from jivago.wsgi.invocation.incorrect_attribute_type_exception import IncorrectAttributeTypeException


class ListDeserializationStrategyTests(unittest.TestCase):

def setUp(self):
self.strategy = ListDeserializationStrategy()

def test_deserializes_untyped_list(self):
result = self.strategy.deserialize([1, 2, 3], list)

self.assertEqual([1, 2, 3], result)

def test_givenIncorrectType_whenDeserializing_shouldRaiseException(self):
with self.assertRaises(IncorrectAttributeTypeException):
self.strategy.deserialize("", list)

def test_canHandleListDeserializationOnly(self):
can_handle_list = self.strategy.can_handle_deserialization(list)
can_handle_other = self.strategy.can_handle_deserialization(dict)

self.assertTrue(can_handle_list)
self.assertFalse(can_handle_other)

0 comments on commit fbe4c98

Please sign in to comment.