Skip to content

Commit dd7bf33

Browse files
authored
try to convert strings to integer when the API expects one (#51)
sometimes the API will return a value as a string, while expecting to get the very same value as an integer, see https://projects.theforeman.org/issues/27673
1 parent f080053 commit dd7bf33

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

apypie/action.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ def _validate(self, params, values, data=None, files=None, path=None):
8383
self._validate(param_description.params, item, path=self._add_to_path(path, param_description.name, num))
8484
elif param_description.expected_type == 'hash':
8585
self._validate(param_description.params, value, path=self._add_to_path(path, param_description.name))
86+
if (param_description.expected_type == 'numeric' and isinstance(value, basestring)):
87+
try:
88+
value = int(value)
89+
except ValueError:
90+
# this will be caught in the next check
91+
pass
8692
if ((param_description.expected_type == 'boolean' and not isinstance(value, bool) and not (isinstance(value, int) and value in [0, 1]))
8793
or (param_description.expected_type == 'numeric' and not isinstance(value, int))
8894
or (param_description.expected_type == 'string' and not isinstance(value, (basestring, int)))):

tests/test_action.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ def test_action_validate_valid_numeric(api):
138138
action.validate({'id': 1})
139139

140140

141+
def test_action_validate_valid_numeric_as_string(api):
142+
action = api.resource('comments').action('archive')
143+
action.validate({'id': '1'})
144+
145+
141146
def test_action_validate_invalid_string(resource):
142147
action = resource.action('create')
143148
with pytest.raises(ValueError) as excinfo:

0 commit comments

Comments
 (0)