Skip to content

Commit

Permalink
restapi/client: don't use DRF parser for parsing
Browse files Browse the repository at this point in the history
The DRF parser expect a file-like object containing bytes while
we are passing it a unicode string. The outcome of this is
slumber serialization not working and returning plain bytes
instead of unserialized data.
The fix is trivial and it's just using slumber code for
parsing instead of providing our own.

For reference after making slumber dump exceptions instead of
swallowing here's the root cause:
Traceback (most recent call last): [celery.redirected:235]
File "/venv/lib/python3.5/site-packages/slumber/__init__.py", line 134, in _try_to_serialize_response
  return stype.loads(resp.content.decode(encoding)) [celery.redirected:235]
File "/home/readthedocs/restapi/client.py", line 28, in loads
  return JSONParser().parse(data) [celery.redirected:235]
File "/venv/lib/python3.5/site-packages/rest_framework/parsers.py", line 66, in parse
  return json.load(decoded_stream) [celery.redirected:235]
File "/usr/lib/python3.5/json/__init__.py", line 265, in load
  return loads(fp.read(), [celery.redirected:235]
File "/usr/lib/python3.5/codecs.py", line 493, in read
  newdata = self.stream.read() [celery.redirected:235]
AttributeError: 'str' object has no attribute 'read' [celery.redirected:235]
  • Loading branch information
xrmx committed Jun 5, 2018
1 parent 71fa1d8 commit 80e9d24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 1 addition & 5 deletions readthedocs/restapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import requests
from django.conf import settings
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser


log = logging.getLogger(__name__)
Expand All @@ -20,13 +19,10 @@

class DrfJsonSerializer(serialize.JsonSerializer):

"""Additional serialization help from the DRF parser/renderer"""
"""Additional serialization help from the DRF renderer"""

key = 'json-drf'

def loads(self, data):
return JSONParser().parse(data)

def dumps(self, data):
return JSONRenderer().render(data)

Expand Down
24 changes: 24 additions & 0 deletions readthedocs/rtd_tests/tests/test_restapi_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import, unicode_literals)

from django.test import TestCase

from readthedocs.restapi.client import DrfJsonSerializer


class TestDrfJsonSerializer(TestCase):
data = {
'proper': 'json'
}
serialized_data = '{"proper":"json"}'

def test_serializer_loads_json(self):
serializer = DrfJsonSerializer()
data = serializer.loads(self.serialized_data)
self.assertDictEqual(data, self.data)

def test_serializer_dumps_json(self):
serializer = DrfJsonSerializer()
serialized_data = serializer.dumps(self.data)
self.assertJSONEqual(serialized_data, self.serialized_data)

0 comments on commit 80e9d24

Please sign in to comment.