Raise an InvalidInput if you get a ValueError from JSON data.
When you parse JSON, if some parts of the message have the wrong type of data you would get a ValueError, returning a 500 HTTP status code. Now the ValueError gets wrapped in an InvalidInput which will result in a 400 HTTP response sent back to the client. Closes-Bug: #1392409 Change-Id: I7702b824a98185d194a29d593b23e47dfd9bca3e
This commit is contained in:
parent
8d9f82d7fe
commit
f66cf4c3cc
@ -222,7 +222,10 @@ def parse(s, datatypes, bodyarg, encoding='utf8'):
|
||||
raise ClientSideError("Request is not in valid JSON format")
|
||||
if bodyarg:
|
||||
argname = list(datatypes.keys())[0]
|
||||
kw = {argname: fromjson(datatypes[argname], jdata)}
|
||||
try:
|
||||
kw = {argname: fromjson(datatypes[argname], jdata)}
|
||||
except ValueError as e:
|
||||
raise InvalidInput(argname, jdata, e.message)
|
||||
else:
|
||||
kw = {}
|
||||
extra_args = []
|
||||
@ -230,7 +233,10 @@ def parse(s, datatypes, bodyarg, encoding='utf8'):
|
||||
if key not in datatypes:
|
||||
extra_args.append(key)
|
||||
else:
|
||||
kw[key] = fromjson(datatypes[key], jdata[key])
|
||||
try:
|
||||
kw[key] = fromjson(datatypes[key], jdata[key])
|
||||
except ValueError as e:
|
||||
raise InvalidInput(key, jdata[key], e.message)
|
||||
if extra_args:
|
||||
raise UnknownArgument(', '.join(extra_args))
|
||||
return kw
|
||||
|
@ -9,10 +9,11 @@ try:
|
||||
except:
|
||||
import json # noqa
|
||||
|
||||
from wsme.rest.json import fromjson, tojson
|
||||
from wsme.rest.json import fromjson, tojson, parse
|
||||
from wsme.utils import parse_isodatetime, parse_isotime, parse_isodate
|
||||
from wsme.types import isarray, isdict, isusertype, register_type
|
||||
from wsme.rest import expose, validate
|
||||
from wsme.exc import InvalidInput
|
||||
|
||||
|
||||
import six
|
||||
@ -297,6 +298,36 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
||||
decimal.Decimal, [int], {int: int}):
|
||||
assert fromjson(dt, None) is None
|
||||
|
||||
def test_parse_valid_date(self):
|
||||
j = parse('{"a": "2011-01-01"}', {'a': datetime.date}, False)
|
||||
assert isinstance(j['a'], datetime.date)
|
||||
|
||||
def test_invalid_date_fromjson(self):
|
||||
jdate = "2015-01-invalid"
|
||||
try:
|
||||
parse('{"a": "%s"}' % jdate, {'a': datetime.date}, False)
|
||||
assert False
|
||||
except Exception as e:
|
||||
assert isinstance(e, InvalidInput)
|
||||
assert e.fieldname == 'a'
|
||||
assert e.value == jdate
|
||||
assert e.msg == "'%s' is not a legal date value" % jdate
|
||||
|
||||
def test_parse_valid_date_bodyarg(self):
|
||||
j = parse('"2011-01-01"', {'a': datetime.date}, True)
|
||||
assert isinstance(j['a'], datetime.date)
|
||||
|
||||
def test_invalid_date_fromjson_bodyarg(self):
|
||||
jdate = "2015-01-invalid"
|
||||
try:
|
||||
parse('"%s"' % jdate, {'a': datetime.date}, True)
|
||||
assert False
|
||||
except Exception as e:
|
||||
assert isinstance(e, InvalidInput)
|
||||
assert e.fieldname == 'a'
|
||||
assert e.value == jdate
|
||||
assert e.msg == "'%s' is not a legal date value" % jdate
|
||||
|
||||
def test_nest_result(self):
|
||||
self.root.protocols[0].nest_result = True
|
||||
r = self.app.get('/returntypes/getint.json')
|
||||
|
Loading…
x
Reference in New Issue
Block a user