Skip to content

Commit

Permalink
fix bug googleapis#1570: manages JSONDecodeError exception
Browse files Browse the repository at this point in the history
test: copes with possible future regressions
  • Loading branch information
gparonitti committed Oct 19, 2021
1 parent f4773f6 commit 14215dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 8 additions & 3 deletions googleapiclient/model.py
Expand Up @@ -273,13 +273,18 @@ def serialize(self, body_value):
return json.dumps(body_value)

def deserialize(self, content):
body = {}
try:
content = content.decode("utf-8")
except AttributeError:
pass
body = json.loads(content)
if self._data_wrapper and isinstance(body, dict) and "data" in body:
body = body["data"]
try:
body = json.loads(content)
if self._data_wrapper and isinstance(body, dict) and "data" in body:
body = body["data"]
except json.decoder.JSONDecodeError:
if isinstance(content, str):
body = content
return body

@property
Expand Down
9 changes: 8 additions & 1 deletion tests/test_json_model.py
Expand Up @@ -31,7 +31,6 @@

import googleapiclient.model


from googleapiclient.errors import HttpError
from googleapiclient.model import JsonModel

Expand Down Expand Up @@ -290,6 +289,14 @@ def test_no_data_wrapper_deserialize(self):
content = model.response(resp, content)
self.assertEqual(content, {"data": "is good"})

def test_no_data_wrapper_deserialize_text_format(self):
model = JsonModel(data_wrapper=False)
resp = httplib2.Response({"status": "200"})
resp.reason = "OK"
content = 'column1,column2,column3\nstring1,1.2,string2'
content = model.response(resp, content)
self.assertEqual(content, 'column1,column2,column3\nstring1,1.2,string2')

def test_data_wrapper_deserialize(self):
model = JsonModel(data_wrapper=True)
resp = httplib2.Response({"status": "200"})
Expand Down

0 comments on commit 14215dc

Please sign in to comment.