Skip to content

Commit 7317d62

Browse files
committed
Requested PR changes
1 parent 9d12610 commit 7317d62

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

ask-sdk-core/ask_sdk_core/serialize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def serialize(self, obj):
6767
If obj is list, serialize each element in the list.
6868
If obj is dict, return the dict with serialized values.
6969
If obj is ask sdk model, return the dict with keys resolved
70-
from model's ``attribute_map`` and values serialized
71-
based on ``deserialized_types``.
70+
from the union of model's ``attribute_map`` and ``deserialized_types``
71+
and values serialized based on ``deserialized_types``.
7272
7373
:param obj: The data to serialize.
7474
:type obj: object

ask-sdk-core/tests/unit/data/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
# specific language governing permissions and limitations under the
1616
# License.
1717
#
18+
1819
from .model_enum_object import ModelEnumObject
1920
from .model_test_object_1 import ModelTestObject1
2021
from .model_test_object_2 import ModelTestObject2
22+
from .model_test_object_3 import ModelTestObject3
23+
from .model_test_object_4 import ModelTestObject4
2124
from .invalid_model_object import InvalidModelObject
2225
from .model_abstract_parent_object import ModelAbstractParentObject
2326
from .model_child_objects import ModelChildObject1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights
4+
# Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License").
7+
# You may not use this file except in compliance with the License.
8+
# A copy of the License is located at
9+
#
10+
# http://aws.amazon.com/apache2.0/
11+
#
12+
# or in the "license" file accompanying this file. This file is
13+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
14+
# OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the
16+
# License.
17+
#
18+
19+
20+
class ModelTestObject3(object):
21+
deserialized_types = {
22+
'str_var': 'str',
23+
'int_var': 'int'
24+
}
25+
26+
def __init__(self, str_var=None, int_var=None):
27+
self.str_var = str_var
28+
self.int_var = int_var
29+
30+
def __eq__(self, other):
31+
return self.__dict__ == other.__dict__
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights
4+
# Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License").
7+
# You may not use this file except in compliance with the License.
8+
# A copy of the License is located at
9+
#
10+
# http://aws.amazon.com/apache2.0/
11+
#
12+
# or in the "license" file accompanying this file. This file is
13+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
14+
# OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the
16+
# License.
17+
#
18+
19+
20+
class ModelTestObject4(object):
21+
deserialized_types = {
22+
'str_var': 'str',
23+
'float_var': 'float'
24+
}
25+
26+
attribute_map = {
27+
'float_var': 'floatingValue'
28+
}
29+
30+
def __init__(self, str_var=None, float_var=None):
31+
self.str_var = str_var
32+
self.float_var = float_var
33+
34+
def __eq__(self, other):
35+
return self.__dict__ == other.__dict__

ask-sdk-core/tests/unit/test_serialize.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ def test_model_obj_serialization(self):
132132
assert self.test_serializer.serialize(test_model_obj_1) == expected_serialized_obj, \
133133
"Default Serializer serialized model object incorrectly"
134134

135+
def test_model_obj_without_attrmap_serialization(self):
136+
test_obj_inst = data.ModelTestObject3(str_var="test", int_var=123)
137+
expected_dict = {
138+
"str_var": "test",
139+
"int_var": 123
140+
}
141+
assert self.test_serializer.serialize(test_obj_inst) == expected_dict, \
142+
"Default Serializer serialized object without attribute_map incorrectly"
143+
144+
def test_model_obj_with_incomplete_attrmap_serialization(self):
145+
test_obj_inst = data.ModelTestObject4(str_var="test", float_var=3.14)
146+
expected_dict = {
147+
"str_var": "test",
148+
"floatingValue": 3.14
149+
}
150+
assert self.test_serializer.serialize(test_obj_inst) == expected_dict, \
151+
"Default Serializer serialized object with incomplete attribute map incorrectly"
152+
135153
def test_enum_obj_serialization(self):
136154
test_model_obj_2 = data.ModelTestObject2(int_var=123)
137155
test_enum_obj = data.ModelEnumObject("ENUM_VAL_1")
@@ -406,6 +424,34 @@ def test_model_obj_with_additional_params_in_payload_deserialization(self):
406424
test_payload, test_obj_type) == expected_obj, (
407425
"Default Serializer deserialized model object incorrectly when payload has additional parameters")
408426

427+
def test_model_obj_without_attrmap_deserialization(self):
428+
test_payload = {
429+
"str_var": "Test",
430+
"int_var": 123
431+
}
432+
test_obj_type = data.ModelTestObject3
433+
expected_obj = data.ModelTestObject3(str_var="Test", int_var=123)
434+
435+
with patch("json.loads") as mock_json_loader:
436+
mock_json_loader.return_value = test_payload
437+
assert self.test_serializer.deserialize(
438+
test_payload, test_obj_type) == expected_obj, (
439+
"Default Serializer deserialized model object without attribute map incorrectly")
440+
441+
def test_model_obj_with_incomplete_attrmap_deserialization(self):
442+
test_payload = {
443+
"str_var": "Test",
444+
"floatingValue": 3.14
445+
}
446+
test_obj_type = data.ModelTestObject4
447+
expected_obj = data.ModelTestObject4(str_var="Test", float_var=3.14)
448+
449+
with patch("json.loads") as mock_json_loader:
450+
mock_json_loader.return_value = test_payload
451+
assert self.test_serializer.deserialize(
452+
test_payload, test_obj_type) == expected_obj, (
453+
"Default Serializer deserialized model object with incomplete attribute map incorrectly")
454+
409455
def test_invalid_model_obj_deserialization(self):
410456
test_payload = {
411457
"var_1": "some value"

0 commit comments

Comments
 (0)