api_core: Fix handling of gapic metadata when specified as 'None'. (#4701)
diff --git a/google/api_core/gapic_v1/method.py b/google/api_core/gapic_v1/method.py
index 50d4356..9c4cf03 100644
--- a/google/api_core/gapic_v1/method.py
+++ b/google/api_core/gapic_v1/method.py
@@ -127,7 +127,12 @@
# Add the user agent metadata to the call.
if self._metadata is not None:
- metadata = list(kwargs.get('metadata', []))
+ metadata = kwargs.get('metadata', [])
+ # Due to the nature of invocation, None should be treated the same
+ # as not specified.
+ if metadata is None:
+ metadata = []
+ metadata = list(metadata)
metadata.extend(self._metadata)
kwargs['metadata'] = metadata
diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py
index 4592cb1..4ce4e8e 100644
--- a/tests/unit/gapic/test_method.py
+++ b/tests/unit/gapic/test_method.py
@@ -94,6 +94,19 @@
assert ('a', 'b') in metadata
+def test_invoke_wrapped_method_with_metadata_as_none():
+ method = mock.Mock(spec=['__call__'])
+
+ wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)
+
+ wrapped_method(mock.sentinel.request, metadata=None)
+
+ method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
+ metadata = method.call_args[1]['metadata']
+ # Metadata should have just one items: the client info metadata.
+ assert len(metadata) == 1
+
+
@mock.patch('time.sleep')
def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
method = mock.Mock(