Ensure that dataWrapper feature is checked before using the 'data' value of a
reponse.
Reviewed: https://codereview.appspot.com/6737065/
Fixes: http://code.google.com/p/google-api-python-client/issues/detail?id=208
diff --git a/apiclient/model.py b/apiclient/model.py
index 0e31053..12fcab6 100644
--- a/apiclient/model.py
+++ b/apiclient/model.py
@@ -261,7 +261,7 @@
def deserialize(self, content):
body = simplejson.loads(content)
- if isinstance(body, dict) and 'data' in body:
+ if self._data_wrapper and isinstance(body, dict) and 'data' in body:
body = body['data']
return body
diff --git a/tests/test_json_model.py b/tests/test_json_model.py
index ececdab..d6cf0c0 100644
--- a/tests/test_json_model.py
+++ b/tests/test_json_model.py
@@ -157,7 +157,6 @@
except HttpError, e:
self.assertTrue('not authorized' in str(e))
-
def test_good_response(self):
model = JsonModel(data_wrapper=True)
resp = httplib2.Response({'status': '200'})
@@ -242,6 +241,31 @@
'--response-end--')
apiclient.model.logging = old_logging
+ def test_no_data_wrapper_deserialize(self):
+ model = JsonModel(data_wrapper=False)
+ resp = httplib2.Response({'status': '200'})
+ resp.reason = 'OK'
+ content = '{"data": "is good"}'
+ content = model.response(resp, content)
+ self.assertEqual(content, {'data': 'is good'})
+
+ def test_data_wrapper_deserialize(self):
+ model = JsonModel(data_wrapper=True)
+ resp = httplib2.Response({'status': '200'})
+ resp.reason = 'OK'
+ content = '{"data": "is good"}'
+ content = model.response(resp, content)
+ self.assertEqual(content, 'is good')
+
+ def test_data_wrapper_deserialize_nodata(self):
+ model = JsonModel(data_wrapper=True)
+ resp = httplib2.Response({'status': '200'})
+ resp.reason = 'OK'
+ content = '{"atad": "is good"}'
+ content = model.response(resp, content)
+ self.assertEqual(content, {'atad': 'is good'})
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/test_mocks.py b/tests/test_mocks.py
index 6822335..5c51f1a 100644
--- a/tests/test_mocks.py
+++ b/tests/test_mocks.py
@@ -52,7 +52,7 @@
def test_simple_response(self):
requestBuilder = RequestMockBuilder({
- 'plus.activities.get': (None, '{"data": {"foo": "bar"}}')
+ 'plus.activities.get': (None, '{"foo": "bar"}')
})
plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)