Commit 70968b0b authored by Xavier Thompson's avatar Xavier Thompson

[exp] slapconfiguration: Accept str for int

Accept parameters received with type str when the expected type
is int, if the string is convertible to an integer.

This make requests more permissive and allows for using str and
int interchangeably in requests. Flat parameter dicts that only
include str and int can be sent in xml even when json-in-xml is
expected, without information loss.

Ajust tests accordingly.
parent 5e664028
......@@ -292,6 +292,15 @@ class JsonSchema(Recipe):
validator_cls = jsonschema.validators.validator_for(schema)
validate_original = validator_cls.VALIDATORS["properties"]
def validate_with_defaults(validator, properties, instance, schema):
# Attempt to adjust str to int if required
for key, subschema in properties.items():
if subschema.get('type') == 'integer':
value = instance.get(key)
if type(value) is str:
try:
instance[key] = int(value)
except ValueError:
pass
# Set defaults
for key, subschema in properties.items():
if "default" in subschema:
......
......@@ -126,6 +126,9 @@ class SlapConfigurationTest(unittest.TestCase):
expected_dict.update(sent_parameters)
self.assertEqual(received_parameters, expected_dict)
def recoverStrToInt(self, sent_parameters, keys):
return {k : int(v) if k in keys else v for k, v in sent_parameters.items()}
def test_jsonschema_json_in_xml_valid_xml_input_defaults(self):
self.writeJsonSchema()
parameters = {"number": 1}
......@@ -154,9 +157,25 @@ class SlapConfigurationTest(unittest.TestCase):
received = self.receiveParameters()
self.checkParameters(received, parameters)
def test_jsonschema_json_in_xml_wrong_type_xml_input(self):
def test_jsonschema_json_in_xml_recover_str_to_int_xml_input(self):
self.writeJsonSchema()
parameters = {"number": "1"}
with self.patchSlap(parameters, False):
received = self.receiveParameters()
recovered = self.recoverStrToInt(parameters, ('number',))
self.checkParameters(received, recovered)
def test_jsonschema_json_in_xml_recover_str_to_int_json_input(self):
self.writeJsonSchema()
parameters = {"number": "1"}
with self.patchSlap(parameters, True):
received = self.receiveParameters()
recovered = self.recoverStrToInt(parameters, ('number',))
self.checkParameters(received, recovered)
def test_jsonschema_json_in_xml_wrong_type_xml_input(self):
self.writeJsonSchema()
parameters = {"number": True}
with self.patchSlap(parameters, False):
self.assertRaises(
slapconfiguration.UserError,
......@@ -165,13 +184,14 @@ class SlapConfigurationTest(unittest.TestCase):
def test_jsonschema_json_in_xml_wrong_type_json_input(self):
self.writeJsonSchema()
parameters = {"number": "1"}
parameters = {"number": True}
with self.patchSlap(parameters, True):
self.assertRaises(
slapconfiguration.UserError,
self.receiveParameters,
)
def test_jsonschema_json_in_xml_incomplete_xml_input(self):
self.writeJsonSchema()
parameters = {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment