From 2467afac0b5ab4f41bb382b83e526a0fcc6b2908 Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Wed, 20 Jun 2012 12:21:25 -0400 Subject: [PATCH] Parameter values of None should be treated as missing. Fixes issue #144. Reviewed in http://codereview.appspot.com/6295108/. --- apiclient/discovery.py | 9 +++++++-- tests/test_discovery.py | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/apiclient/discovery.py b/apiclient/discovery.py index c2429d9..ce54f88 100644 --- a/apiclient/discovery.py +++ b/apiclient/discovery.py @@ -268,8 +268,6 @@ def _cast(value, schema_type): if type(value) == type('') or type(value) == type(u''): return value else: - if value is None: - raise ValueError('String parameters can not be None.') return str(value) elif schema_type == 'integer': return str(int(value)) @@ -443,10 +441,17 @@ def _createResource(http, baseUrl, model, requestBuilder, def method(self, **kwargs): # Don't bother with doc string, it will be over-written by createMethod. + for name in kwargs.iterkeys(): if name not in argmap: raise TypeError('Got an unexpected keyword argument "%s"' % name) + # Remove args that have a value of None. + keys = kwargs.keys() + for name in keys: + if kwargs[name] is None: + del kwargs[name] + for name in required_params: if name not in kwargs: raise TypeError('Missing required parameter "%s"' % name) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 8d45e2d..8d8effd 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -136,6 +136,13 @@ class Discovery(unittest.TestCase): except TypeError, e: self.assertTrue('Missing' in str(e)) + # Missing required parameters even if supplied as None. + try: + plus.activities().list(collection=None, userId=None) + self.fail() + except TypeError, e: + self.assertTrue('Missing' in str(e)) + # Parameter doesn't match regex try: plus.activities().list(collection='not_a_collection_name', userId='me') @@ -193,15 +200,14 @@ class Discovery(unittest.TestCase): self.assertEqual(q['trace'], ['html']) self.assertEqual(q['fields'], ['description']) - def test_string_params_none_is_invalid(self): + def test_string_params_value_of_none_get_dropped(self): http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', http) - # String isn't None - try: - request = zoo.query(trace=None, fields='description') - self.fail() - except ValueError, e: - self.assertTrue('None' in str(e)) + request = zoo.query(trace=None, fields='description') + + parsed = urlparse.urlparse(request.uri) + q = parse_qs(parsed[4]) + self.assertFalse('trace' in q) def test_model_added_query_parameters(self): http = HttpMock(datafile('zoo.json'), {'status': '200'})