Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oaf): allow nullable value in the type validation #52

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apimatic_core/utilities/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ def is_file_wrapper_instance(param):
return isinstance(param, FileWrapper)

@staticmethod
def is_valid_type(value, type_callable):
def is_valid_type(value, type_callable, is_value_nullable=False):
if value is None and is_value_nullable:
return True

if isinstance(value, list):
return all(ApiHelper.is_valid_type(item, type_callable) for item in value)
elif isinstance(value, dict):
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.7',
version='0.2.8',
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
66 changes: 34 additions & 32 deletions tests/apimatic_core/utility_tests/test_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,38 +849,40 @@ def test_resolve_template_placeholders_using_json_pointer(self, input_placeholde
input_template)
assert actual_message == expected_message

@pytest.mark.parametrize('input_value, input_callable, expected_value', [
(100, lambda value: isinstance(value, int), True),
('100', lambda value: isinstance(value, str), True),
("Sunday", lambda value: Days.validate(value), True),
(100.5, lambda value: isinstance(value, str), False),
("Invalid", lambda value: Days.validate(value), False),
(None, lambda value: isinstance(value, str), False),
(None, None, False),

([100, 200], lambda value: isinstance(value, int), True),
(['100', '200'], lambda value: isinstance(value, str), True),
(["Sunday", "Monday"], lambda value: Days.validate(value), True),
([100.5, 200], lambda value: isinstance(value, str), False),
(["Invalid1", "Invalid2"], lambda value: Days.validate(value), False),
([None, None], lambda value: isinstance(value, str), False),

([[100, 200], [300, 400]], lambda value: isinstance(value, int), True),
([['100', '200'], ['abc', 'def']], lambda value: isinstance(value, str), True),
([["Sunday", "Monday"], ["Tuesday", "Friday"]], lambda value: Days.validate(value), True),
([[100.5, 200], [400, 500]], lambda value: isinstance(value, str), False),
([["Invalid1", "Invalid2"], ["Sunday", "Invalid4"]], lambda value: Days.validate(value), False),
([[None, None], [None, None]], lambda value: isinstance(value, str), False),

({'key0': 100, 'key2': 200}, lambda value: isinstance(value, int), True),
({'key0': 'abc', 'key2': 'def'}, lambda value: isinstance(value, str), True),
({'key0': 'Sunday', 'key2': 'Tuesday'}, lambda value: Days.validate(value), True),
({'key0': 100.5, 'key2': 200}, lambda value: isinstance(value, str), False),
({'key0': "Invalid1", 'key2': "Invalid2"}, lambda value: Days.validate(value), False),
({'key0': None, 'key2': None}, lambda value: isinstance(value, str), False),
])
def test_is_valid_type(self, input_value, input_callable, expected_value):
actual_value = ApiHelper.is_valid_type(input_value, input_callable)
@pytest.mark.parametrize('input_value, input_callable, is_value_nullable, expected_value', [
(100, lambda value: isinstance(value, int), False, True),
('100', lambda value: isinstance(value, str), False, True),
("Sunday", lambda value: Days.validate(value), False, True),
(100.5, lambda value: isinstance(value, str), False, False),
("Invalid", lambda value: Days.validate(value), False, False),
(None, lambda value: isinstance(value, str), False, False),
(None, lambda value: isinstance(value, str), True, True),
(None, None, False, False),
(None, None, True, True),

([100, 200], lambda value: isinstance(value, int), False, True),
(['100', '200'], lambda value: isinstance(value, str), False, True),
(["Sunday", "Monday"], lambda value: Days.validate(value), False, True),
([100.5, 200], lambda value: isinstance(value, str), False, False),
(["Invalid1", "Invalid2"], lambda value: Days.validate(value), False, False),
([None, None], lambda value: isinstance(value, str), False, False),

([[100, 200], [300, 400]], lambda value: isinstance(value, int), False, True),
([['100', '200'], ['abc', 'def']], lambda value: isinstance(value, str), False, True),
([["Sunday", "Monday"], ["Tuesday", "Friday"]], lambda value: Days.validate(value), False, True),
([[100.5, 200], [400, 500]], lambda value: isinstance(value, str), False, False),
([["Invalid1", "Invalid2"], ["Sunday", "Invalid4"]], lambda value: Days.validate(value), False, False),
([[None, None], [None, None]], lambda value: isinstance(value, str), False, False),

({'key0': 100, 'key2': 200}, lambda value: isinstance(value, int), False, True),
({'key0': 'abc', 'key2': 'def'}, lambda value: isinstance(value, str), False, True),
({'key0': 'Sunday', 'key2': 'Tuesday'}, lambda value: Days.validate(value), False, True),
({'key0': 100.5, 'key2': 200}, lambda value: isinstance(value, str), False, False),
({'key0': "Invalid1", 'key2': "Invalid2"}, lambda value: Days.validate(value), False, False),
({'key0': None, 'key2': None}, lambda value: isinstance(value, str), False, False),
])
def test_is_valid_type(self, input_value, input_callable, is_value_nullable, expected_value):
actual_value = ApiHelper.is_valid_type(input_value, input_callable, is_value_nullable)
assert actual_value == expected_value

@pytest.mark.parametrize('input_value, input_union_type, input_should_deserialize, expected_value', [
Expand Down
Loading