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 a7e6956 commit 857c37b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions googleapiclient/model.py
Expand Up @@ -280,11 +280,12 @@ def deserialize(self, content):
pass
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
else:
if self._data_wrapper and isinstance(body, dict) and "data" in body:
body = body["data"]
return body

@property
Expand Down
5 changes: 3 additions & 2 deletions tests/test_json_model.py
Expand Up @@ -35,6 +35,7 @@
from googleapiclient.model import JsonModel

_LIBRARY_VERSION = pkg_resources.get_distribution("google-api-python-client").version
CSV_TEXT_MOCK = 'column1,column2,column3\nstring1,1.2,string2'


class Model(unittest.TestCase):
Expand Down Expand Up @@ -293,9 +294,9 @@ 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 = CSV_TEXT_MOCK
content = model.response(resp, content)
self.assertEqual(content, 'column1,column2,column3\nstring1,1.2,string2')
self.assertEqual(content, CSV_TEXT_MOCK)

def test_data_wrapper_deserialize(self):
model = JsonModel(data_wrapper=True)
Expand Down

0 comments on commit 857c37b

Please sign in to comment.