Skip to content

Commit b3d0d28

Browse files
fix(empty-response): handle empty response in json deserialize (#67)
This commit fixes an issue where ApiHelper.json_deserialize returned the same empty payload (None or empty string) instead of None when the API response was missing. The change ensures the utility behaves as expected and returns None for empty responses.
1 parent efd954e commit b3d0d28

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

apimatic_core/utilities/api_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def json_deserialize(json, unboxing_function=None, as_dict=False):
108108
JSON serialized string.
109109
110110
"""
111-
if json is None:
111+
if json is None or json.strip() == '':
112112
return None
113113

114114
try:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='apimatic-core',
15-
version='0.2.14',
15+
version='0.2.15',
1616
description='A library that contains core logic and utilities for '
1717
'consuming REST APIs using Python SDKs generated by APIMatic.',
1818
long_description=long_description,

tests/apimatic_core/response_handler_tests/test_response_handler.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ def test_simple_response_body_with_convertor(self, input_http_response, input_re
151151

152152
@pytest.mark.parametrize('input_http_response, expected_response_body', [
153153
(Base.response(text=ApiHelper.json_serialize(Base.employee_model())),
154-
ApiHelper.json_serialize(Base.employee_model()))
154+
ApiHelper.json_serialize(Base.employee_model())),
155+
(Base.response(), None),
156+
(Base.response(text=''), None),
157+
(Base.response(text=' '), None)
155158
])
156159
def test_custom_type_response_body(self, input_http_response, expected_response_body):
157160
http_response = self.new_response_handler \
@@ -164,7 +167,10 @@ def test_custom_type_response_body(self, input_http_response, expected_response_
164167
(Base.response(text='[1, 2, 3, 4]'), '[1, 2, 3, 4]'),
165168
(Base.response(text='{"key1": "value1", "key2": "value2"}'), '{"key1": "value1", "key2": "value2"}'),
166169
(Base.response(text='{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}'),
167-
'{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}')
170+
'{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}'),
171+
(Base.response(), None),
172+
(Base.response(text=''), None),
173+
(Base.response(text=' '), None)
168174
])
169175
def test_json_response_body(self, input_http_response, expected_response_body):
170176
http_response = self.new_response_handler \
@@ -267,7 +273,9 @@ def test_api_response_convertor(self, input_http_response, expected_response_bod
267273

268274
@pytest.mark.parametrize('input_http_response, expected_response_body, expected_error_list', [
269275
(Base.response(text='{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}'),
270-
'{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}', ['e1', 'e2'])
276+
'{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}', ['e1', 'e2']),
277+
(Base.response(text=''), None, None),
278+
(Base.response(text=' '), None, None)
271279
])
272280
def test_api_response_with_errors(self, input_http_response, expected_response_body, expected_error_list):
273281
api_response = self.new_response_handler \

tests/apimatic_core/utility_tests/test_api_helper.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def test_json_serialize(self, input_value, expected_value):
152152
@pytest.mark.parametrize('input_json_value, unboxing_function, as_dict, expected_value', [
153153
(None, None, False, None),
154154
('true', None, False, 'true'),
155+
('', None, False, None),
156+
(' ', None, False, None),
155157
(ApiHelper.json_serialize(Base.employee_model()), Employee.from_dictionary, False,
156158
ApiHelper.json_serialize(Base.employee_model())),
157159
(ApiHelper.json_serialize([Base.employee_model(), Base.employee_model()]),
@@ -176,14 +178,6 @@ def test_json_deserialize(self, input_json_value, unboxing_function, as_dict, ex
176178
deserialized_value = ApiHelper.json_deserialize(input_json_value, unboxing_function, as_dict)
177179
assert ApiHelper.json_serialize(deserialized_value) == expected_value
178180

179-
@pytest.mark.parametrize('input_json', [
180-
True
181-
])
182-
def test_json_deserialize_value_error(self, input_json):
183-
with pytest.raises(ValueError):
184-
raise ValueError(input_json)
185-
ApiHelper.json_deserialize(input_json)
186-
187181
@pytest.mark.parametrize('input_url, input_file_value, expected_value', [
188182
('C:\\PYTHON_GENERIC_LIB\\Tester\\models\\test_file.py', "test_file",
189183
'C:\\PYTHON_GENERIC_LIB\\Tester\\schemas\\TestFile.json'),
@@ -789,7 +783,9 @@ def test_get_content_type(self, input_value, output_value):
789783
@pytest.mark.parametrize('input_value, output_value', [
790784
('{"method": "GET", "body": {}, "uploadCount": 0}', {'body': {}, 'method': 'GET', 'uploadCount': 0}),
791785
('I am a string', 'I am a string'),
792-
(None, None)
786+
(None, None),
787+
('', None),
788+
(' ', None)
793789
])
794790
def test_dynamic_deserialize(self, input_value, output_value):
795791
assert ApiHelper.dynamic_deserialize(input_value) == output_value

0 commit comments

Comments
 (0)