Skip to content
Snippets Groups Projects
Commit d88a9c59 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

api/serializers: Force json module use to decode requests text response

When the simplejson module is present in the Phython environment, requests will
use it to decode json but a call to response.json(cls=...) will raise an exception
as the arguments accepted by JSONDecoder are inconsistent between the standard library
json and simplejson (more details here: https://github.com/psf/requests/issues/4842).

So ensure to use standard json module for decoding response texts.
parent 91910d96
No related branches found
No related tags found
1 merge request!93Force json module use to decode requests text response
......@@ -5,7 +5,7 @@
import base64
import datetime
from json import JSONDecoder, JSONEncoder
import json
import types
from uuid import UUID
......@@ -28,7 +28,7 @@ def decode_response(response):
if content_type.startswith('application/x-msgpack'):
r = msgpack_loads(response.content)
elif content_type.startswith('application/json'):
r = response.json(cls=SWHJSONDecoder)
r = json.loads(response.text, cls=SWHJSONDecoder)
else:
raise ValueError('Wrong content type `%s` for API response'
% content_type)
......@@ -36,7 +36,7 @@ def decode_response(response):
return r
class SWHJSONEncoder(JSONEncoder):
class SWHJSONEncoder(json.JSONEncoder):
"""JSON encoder for data structures generated by Software Heritage.
This JSON encoder extends the default Python JSON encoder and adds
......@@ -101,7 +101,7 @@ class SWHJSONEncoder(JSONEncoder):
return list(iterable)
class SWHJSONDecoder(JSONDecoder):
class SWHJSONDecoder(json.JSONDecoder):
"""JSON decoder for data structures encoded with SWHJSONEncoder.
This JSON decoder extends the default Python JSON decoder,
......
......@@ -9,12 +9,15 @@ import unittest
from uuid import UUID
import arrow
import requests
import requests_mock
from swh.core.api.serializers import (
SWHJSONDecoder,
SWHJSONEncoder,
msgpack_dumps,
msgpack_loads
msgpack_loads,
decode_response
)
......@@ -79,3 +82,11 @@ class Serializers(unittest.TestCase):
def test_generator_msgpack(self):
data = msgpack_dumps(self.generator)
self.assertEqual(self.gen_lst, msgpack_loads(data))
@requests_mock.Mocker()
def test_decode_response_json(self, mock_requests):
mock_requests.get('https://example.org/test/data',
json=self.encoded_data,
headers={'content-type': 'application/json'})
response = requests.get('https://example.org/test/data')
assert decode_response(response) == self.data
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment