Skip to content

Commit a751c05

Browse files
committed
adds multi-dimensional array handling for additional properties
1 parent 57fe24b commit a751c05

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

apimatic_core/utilities/api_helper.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,31 @@ def json_deserialize(json, unboxing_function=None, as_dict=False):
128128

129129
@staticmethod
130130
def apply_unboxing_function(value, unboxing_function, is_array=False, is_dict=False, is_array_of_map=False,
131-
is_map_of_array=False):
131+
is_map_of_array=False, dimension_count=1):
132132
if is_dict:
133133
if is_map_of_array:
134-
return {k: list(map(unboxing_function, v)) for k, v in value.items()}
134+
return {k: ApiHelper.apply_unboxing_function(v,
135+
unboxing_function,
136+
is_array=True,
137+
dimension_count=dimension_count)
138+
for k, v in value.items()}
135139
else:
136140
return {k: unboxing_function(v) for k, v in value.items()}
137141
elif is_array:
138142
if is_array_of_map:
139-
return list(map(lambda x: {k: unboxing_function(v) for k, v in x.items()}, value))
143+
return [
144+
ApiHelper.apply_unboxing_function(element,
145+
unboxing_function,
146+
is_dict=True,
147+
dimension_count=dimension_count)
148+
for element in value]
149+
elif dimension_count > 1:
150+
return [ApiHelper.apply_unboxing_function(element, unboxing_function,
151+
is_array=True,
152+
dimension_count=dimension_count - 1)
153+
for element in value]
140154
else:
141-
return list(map(unboxing_function, value))
155+
return [unboxing_function(element) for element in value]
142156

143157
return unboxing_function(value)
144158

tests/apimatic_core/utility_tests/test_api_helper.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,27 +1068,35 @@ def test_get_additional_properties_exception(self, dictionary):
10681068
assert result == {} # expected result when exception occurs
10691069

10701070
@pytest.mark.parametrize(
1071-
"value, unboxing_function, is_array, is_dict, is_array_of_map, is_map_of_array, expected",
1071+
"value, unboxing_function, is_array, is_dict, is_array_of_map, is_map_of_array, dimension_count, expected",
10721072
[
10731073
# Test case 1: Simple object
1074-
(5, lambda x: x * 2, False, False, False, False, 10),
1074+
(5, lambda x: x * 2, False, False, False, False, 0, 10),
10751075
# Test case 2: Array
1076-
([1, 2, 3], lambda x: x * 2, True, False, False, False, [2, 4, 6]),
1076+
([1, 2, 3], lambda x: x * 2, True, False, False, False, 0, [2, 4, 6]),
10771077
# Test case 3: Dictionary
1078-
({"a": 1, "b": 2}, lambda x: x * 2, False, True, False, False, {"a": 2, "b": 4}),
1078+
({"a": 1, "b": 2}, lambda x: x * 2, False, True, False, False, 0, {"a": 2, "b": 4}),
10791079
# Test case 4: Array of maps
1080-
([{"a": 1}, {"b": 2}], lambda x: x * 2, True, False, True, False, [{"a": 2}, {"b": 4}]),
1080+
([{"a": 1}, {"b": 2}], lambda x: x * 2, True, False, True, False, 0, [{"a": 2}, {"b": 4}]),
10811081
# 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]}),
1082+
({"a": [1, 2], "b": [3, 4]}, lambda x: x * 2, False, True, False, True, 0, {"a": [2, 4], "b": [6, 8]}),
1083+
# Test case 6: Multi-dimensional array
1084+
([[1], [2, 3], [4]], lambda x: x * 2, True, False, False, False, 2, [[2], [4, 6], [8]]),
1085+
# Test case 7: Array of arrays
1086+
([[1, 2], [3, 4]], lambda x: x * 2, True, False, False, False, 2, [[2, 4], [6, 8]]),
1087+
# Test case 8: Array of arrays of arrays
1088+
([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], lambda x: x * 2, True, False, False, False, 3,
1089+
[[[2, 4], [6, 8]], [[10, 12], [14, 16]]]),
10831090
],
10841091
)
10851092
def test_apply_unboxing_function(self, value, unboxing_function, is_array, is_dict,
1086-
is_array_of_map, is_map_of_array, expected):
1093+
is_array_of_map, is_map_of_array, dimension_count, expected):
10871094
result = ApiHelper.apply_unboxing_function(
10881095
value,
10891096
unboxing_function,
10901097
is_array,
10911098
is_dict,
10921099
is_array_of_map,
1093-
is_map_of_array)
1100+
is_map_of_array,
1101+
dimension_count)
10941102
assert result == expected

0 commit comments

Comments
 (0)