Skip to content

Commit

Permalink
JSONSerializer throws BadRequest upon failed deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSaico committed Mar 10, 2016
1 parent a4994eb commit 8471463
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions restless/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .exceptions import BadRequest
from .utils import json, MoreTypesJSONEncoder


Expand Down Expand Up @@ -58,9 +59,12 @@ def deserialize(self, body):
:returns: The deserialized data
:rtype: ``list`` or ``dict``
"""
if isinstance(body, bytes):
return json.loads(body.decode('utf-8'))
return json.loads(body)
try:
if isinstance(body, bytes):
return json.loads(body.decode('utf-8'))
return json.loads(body)
except ValueError:
raise BadRequest('Request body is not valid JSON')

def serialize(self, data):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
import uuid

from restless.exceptions import BadRequest
from restless.serializers import JSONSerializer


Expand Down Expand Up @@ -33,3 +34,7 @@ def test_deserialize(self):
self.assertEqual(self.serializer.deserialize('{"more": "things"}'), {
'more': 'things',
})

def test_deserialize_invalid(self):
with self.assertRaises(BadRequest):
self.serializer.deserialize('not valid!')

0 comments on commit 8471463

Please sign in to comment.