Skip to content

Commit 57fe24b

Browse files
committed
adds array of map and map of array handling for unboxing function in ApiHelper
1 parent d78ac1c commit 57fe24b

3 files changed

Lines changed: 61 additions & 21 deletions

File tree

apimatic_core/utilities/api_helper.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,28 @@ def json_deserialize(json, unboxing_function=None, as_dict=False):
119119
if unboxing_function is None:
120120
return decoded
121121

122-
return ApiHelper.apply_unboxing_function(decoded, unboxing_function, as_dict)
122+
if as_dict:
123+
return {k: unboxing_function(v) for k, v in decoded.items()}
124+
elif isinstance(decoded, list):
125+
return [unboxing_function(element) for element in decoded]
126+
127+
return unboxing_function(decoded)
123128

124129
@staticmethod
125-
def apply_unboxing_function(obj, unboxing_function, as_dict=False):
126-
if as_dict:
127-
return {k: unboxing_function(v) for k, v in obj.items()}
128-
elif isinstance(obj, list):
129-
return [unboxing_function(element) for element in obj]
130+
def apply_unboxing_function(value, unboxing_function, is_array=False, is_dict=False, is_array_of_map=False,
131+
is_map_of_array=False):
132+
if is_dict:
133+
if is_map_of_array:
134+
return {k: list(map(unboxing_function, v)) for k, v in value.items()}
135+
else:
136+
return {k: unboxing_function(v) for k, v in value.items()}
137+
elif is_array:
138+
if is_array_of_map:
139+
return list(map(lambda x: {k: unboxing_function(v) for k, v in x.items()}, value))
140+
else:
141+
return list(map(unboxing_function, value))
130142

131-
return unboxing_function(obj)
143+
return unboxing_function(value)
132144

133145
@staticmethod
134146
def dynamic_deserialize(dynamic_response):

tests/apimatic_core/mocks/models/model_with_additional_properties.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def from_dictionary(cls,
120120

121121
additional_properties = ApiHelper.get_additional_properties(
122122
dictionary={k: v for k, v in dictionary.items() if k not in cls._names.values()},
123-
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: int(item)))
123+
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: int(item), is_array=True))
124124

125125
# Return an object of this model
126126
return cls(email, additional_properties)
@@ -181,7 +181,7 @@ def from_dictionary(cls,
181181

182182
additional_properties = ApiHelper.get_additional_properties(
183183
dictionary={k: v for k, v in dictionary.items() if k not in cls._names.values()},
184-
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: int(item), as_dict=True))
184+
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: int(item), is_dict=True))
185185

186186
# Return an object of this model
187187
return cls(email, additional_properties)
@@ -303,7 +303,7 @@ def from_dictionary(cls,
303303

304304
additional_properties = ApiHelper.get_additional_properties(
305305
dictionary={k: v for k, v in dictionary.items() if k not in cls._names.values()},
306-
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: Lion.from_dictionary(item)))
306+
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(x, lambda item: Lion.from_dictionary(item), is_array=True))
307307

308308
# Return an object of this model
309309
return cls(email, additional_properties)
@@ -366,7 +366,7 @@ def from_dictionary(cls,
366366
dictionary={k: v for k, v in dictionary.items() if k not in cls._names.values()},
367367
unboxing_function=lambda x: ApiHelper.apply_unboxing_function(
368368
x, lambda item: Lion.from_dictionary(item),
369-
as_dict=True))
369+
is_dict=True))
370370

371371
# Return an object of this model
372372
return cls(email, additional_properties)

tests/apimatic_core/utility_tests/test_api_helper.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,18 +1040,20 @@ def test_to_lower_case(self, input_list, expected_output):
10401040
assert actual_output == expected_output
10411041

10421042
@pytest.mark.parametrize(
1043-
"dictionary, expected_result, unboxing_func, is_dict",
1043+
"dictionary, expected_result, unboxing_func, is_array, is_dict",
10441044
[
1045-
({}, {}, lambda x: int(x), False),
1046-
({"a": 1, "b": 2}, {"a": 1, "b": 2}, lambda x: int(x), False),
1047-
({"a": "1", "b": "2"}, {"a": "1", "b": "2"}, lambda x: str(x), False),
1048-
({"a": "Test 1", "b": "Test 2"}, {}, lambda x: int(x), False),
1049-
({"a": [1, 2], "b": [3, 4]}, {"a": [1, 2], "b": [3, 4]}, lambda x: int(x), False),
1050-
({"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, {"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, lambda x: int(x), True),
1045+
({}, {}, lambda x: int(x), False, False),
1046+
({"a": 1, "b": 2}, {"a": 1, "b": 2}, lambda x: int(x), False, False),
1047+
({"a": "1", "b": "2"}, {"a": "1", "b": "2"}, lambda x: str(x), False, False),
1048+
({"a": "Test 1", "b": "Test 2"}, {}, lambda x: int(x), False, False),
1049+
({"a": [1, 2], "b": [3, 4]}, {"a": [1, 2], "b": [3, 4]}, lambda x: int(x), True, False),
1050+
({"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, {"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, lambda x: int(x), False, True),
10511051
],
10521052
)
1053-
def test_get_additional_properties_success(self, dictionary, expected_result, unboxing_func, is_dict):
1054-
result = ApiHelper.get_additional_properties(dictionary, lambda x: ApiHelper.apply_unboxing_function(x, unboxing_func, is_dict))
1053+
def test_get_additional_properties_success(self, dictionary, expected_result, unboxing_func, is_array, is_dict):
1054+
result = ApiHelper.get_additional_properties(
1055+
dictionary, lambda x: ApiHelper.apply_unboxing_function(
1056+
x, unboxing_func, is_array, is_dict))
10551057
assert result == expected_result
10561058

10571059
@pytest.mark.parametrize(
@@ -1063,4 +1065,30 @@ def test_get_additional_properties_success(self, dictionary, expected_result, un
10631065
)
10641066
def test_get_additional_properties_exception(self, dictionary):
10651067
result = ApiHelper.get_additional_properties(dictionary, ApiHelper.apply_unboxing_function)
1066-
assert result == {} # expected result when exception occurs
1068+
assert result == {} # expected result when exception occurs
1069+
1070+
@pytest.mark.parametrize(
1071+
"value, unboxing_function, is_array, is_dict, is_array_of_map, is_map_of_array, expected",
1072+
[
1073+
# Test case 1: Simple object
1074+
(5, lambda x: x * 2, False, False, False, False, 10),
1075+
# Test case 2: Array
1076+
([1, 2, 3], lambda x: x * 2, True, False, False, False, [2, 4, 6]),
1077+
# Test case 3: Dictionary
1078+
({"a": 1, "b": 2}, lambda x: x * 2, False, True, False, False, {"a": 2, "b": 4}),
1079+
# Test case 4: Array of maps
1080+
([{"a": 1}, {"b": 2}], lambda x: x * 2, True, False, True, False, [{"a": 2}, {"b": 4}]),
1081+
# Test case 5: Map of arrays
1082+
({"a": [1, 2], "b": [3, 4]}, lambda x: x * 2, False, True, False, True, {"a": [2, 4], "b": [6, 8]}),
1083+
],
1084+
)
1085+
def test_apply_unboxing_function(self, value, unboxing_function, is_array, is_dict,
1086+
is_array_of_map, is_map_of_array, expected):
1087+
result = ApiHelper.apply_unboxing_function(
1088+
value,
1089+
unboxing_function,
1090+
is_array,
1091+
is_dict,
1092+
is_array_of_map,
1093+
is_map_of_array)
1094+
assert result == expected

0 commit comments

Comments
 (0)