Skip to content

Commit

Permalink
fix(empty-response): handle empty response in json deserialize (#67)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sufyankhanrao authored Jul 9, 2024
1 parent efd954e commit b3d0d28
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apimatic_core/utilities/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def json_deserialize(json, unboxing_function=None, as_dict=False):
JSON serialized string.
"""
if json is None:
if json is None or json.strip() == '':
return None

try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='apimatic-core',
version='0.2.14',
version='0.2.15',
description='A library that contains core logic and utilities for '
'consuming REST APIs using Python SDKs generated by APIMatic.',
long_description=long_description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ def test_simple_response_body_with_convertor(self, input_http_response, input_re

@pytest.mark.parametrize('input_http_response, expected_response_body', [
(Base.response(text=ApiHelper.json_serialize(Base.employee_model())),
ApiHelper.json_serialize(Base.employee_model()))
ApiHelper.json_serialize(Base.employee_model())),
(Base.response(), None),
(Base.response(text=''), None),
(Base.response(text=' '), None)
])
def test_custom_type_response_body(self, input_http_response, expected_response_body):
http_response = self.new_response_handler \
Expand All @@ -164,7 +167,10 @@ def test_custom_type_response_body(self, input_http_response, expected_response_
(Base.response(text='[1, 2, 3, 4]'), '[1, 2, 3, 4]'),
(Base.response(text='{"key1": "value1", "key2": "value2"}'), '{"key1": "value1", "key2": "value2"}'),
(Base.response(text='{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}'),
'{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}')
'{"key1": "value1", "key2": [1, 2, 3, {"key1": "value1", "key2": "value2"}]}'),
(Base.response(), None),
(Base.response(text=''), None),
(Base.response(text=' '), None)
])
def test_json_response_body(self, input_http_response, expected_response_body):
http_response = self.new_response_handler \
Expand Down Expand Up @@ -267,7 +273,9 @@ def test_api_response_convertor(self, input_http_response, expected_response_bod

@pytest.mark.parametrize('input_http_response, expected_response_body, expected_error_list', [
(Base.response(text='{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}'),
'{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}', ['e1', 'e2'])
'{"key1": "value1", "key2": "value2", "errors": ["e1", "e2"]}', ['e1', 'e2']),
(Base.response(text=''), None, None),
(Base.response(text=' '), None, None)
])
def test_api_response_with_errors(self, input_http_response, expected_response_body, expected_error_list):
api_response = self.new_response_handler \
Expand Down
14 changes: 5 additions & 9 deletions tests/apimatic_core/utility_tests/test_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def test_json_serialize(self, input_value, expected_value):
@pytest.mark.parametrize('input_json_value, unboxing_function, as_dict, expected_value', [
(None, None, False, None),
('true', None, False, 'true'),
('', None, False, None),
(' ', None, False, None),
(ApiHelper.json_serialize(Base.employee_model()), Employee.from_dictionary, False,
ApiHelper.json_serialize(Base.employee_model())),
(ApiHelper.json_serialize([Base.employee_model(), Base.employee_model()]),
Expand All @@ -176,14 +178,6 @@ def test_json_deserialize(self, input_json_value, unboxing_function, as_dict, ex
deserialized_value = ApiHelper.json_deserialize(input_json_value, unboxing_function, as_dict)
assert ApiHelper.json_serialize(deserialized_value) == expected_value

@pytest.mark.parametrize('input_json', [
True
])
def test_json_deserialize_value_error(self, input_json):
with pytest.raises(ValueError):
raise ValueError(input_json)
ApiHelper.json_deserialize(input_json)

@pytest.mark.parametrize('input_url, input_file_value, expected_value', [
('C:\\PYTHON_GENERIC_LIB\\Tester\\models\\test_file.py', "test_file",
'C:\\PYTHON_GENERIC_LIB\\Tester\\schemas\\TestFile.json'),
Expand Down Expand Up @@ -789,7 +783,9 @@ def test_get_content_type(self, input_value, output_value):
@pytest.mark.parametrize('input_value, output_value', [
('{"method": "GET", "body": {}, "uploadCount": 0}', {'body': {}, 'method': 'GET', 'uploadCount': 0}),
('I am a string', 'I am a string'),
(None, None)
(None, None),
('', None),
(' ', None)
])
def test_dynamic_deserialize(self, input_value, output_value):
assert ApiHelper.dynamic_deserialize(input_value) == output_value
Expand Down

0 comments on commit b3d0d28

Please sign in to comment.