diff --git a/wsme/rest/json.py b/wsme/rest/json.py index 77277ce..42a9559 100644 --- a/wsme/rest/json.py +++ b/wsme/rest/json.py @@ -166,9 +166,10 @@ def dict_fromjson(datatype, value): @fromjson.when_object(six.binary_type) def str_fromjson(datatype, value): - if value is None: - return None - return value.encode('utf8') + if (isinstance(value, six.string_types) + or isinstance(value, six.integer_types) + or isinstance(value, float)): + return six.text_type(value).encode('utf8') @fromjson.when_object(wsme.types.text) diff --git a/wsme/tests/test_api.py b/wsme/tests/test_api.py index f08b74b..008a928 100644 --- a/wsme/tests/test_api.py +++ b/wsme/tests/test_api.py @@ -86,6 +86,30 @@ Invalid value (should be one of:")) self.assertIn('None', res.json_body['faultstring']) self.assertEqual(res.status_int, 400) + def test_validate_enum_with_wrong_type(self): + class Version(object): + number = types.Enum(str, 'v1', 'v2', None) + + class MyWS(WSRoot): + @expose(str) + @validate(Version) + def setcplx(self, version): + pass + + r = MyWS(['restjson']) + app = webtest.TestApp(r.wsgiapp()) + res = app.post_json('/setcplx', params={'version': {'number': 1}}, + expect_errors=True, + headers={'Accept': 'application/json'}) + self.assertTrue( + res.json_body['faultstring'].startswith( + "Invalid input for field/attribute number. Value: '1'. \ +Invalid value (should be one of:")) + self.assertIn('v1', res.json_body['faultstring']) + self.assertIn('v2', res.json_body['faultstring']) + self.assertIn('None', res.json_body['faultstring']) + self.assertEqual(res.status_int, 400) + def test_scan_api(self): class NS(object): @expose(int)