Skip to content

Commit 95fb93e

Browse files
author
Jon Wayne Parrott
authored
api_core: Fix handling of gapic metadata when specified as 'None'. (#4701)
1 parent 04c812e commit 95fb93e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

google/api_core/gapic_v1/method.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ def __call__(self, *args, **kwargs):
127127

128128
# Add the user agent metadata to the call.
129129
if self._metadata is not None:
130-
metadata = list(kwargs.get('metadata', []))
130+
metadata = kwargs.get('metadata', [])
131+
# Due to the nature of invocation, None should be treated the same
132+
# as not specified.
133+
if metadata is None:
134+
metadata = []
135+
metadata = list(metadata)
131136
metadata.extend(self._metadata)
132137
kwargs['metadata'] = metadata
133138

tests/unit/gapic/test_method.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ def test_invoke_wrapped_method_with_metadata():
9494
assert ('a', 'b') in metadata
9595

9696

97+
def test_invoke_wrapped_method_with_metadata_as_none():
98+
method = mock.Mock(spec=['__call__'])
99+
100+
wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)
101+
102+
wrapped_method(mock.sentinel.request, metadata=None)
103+
104+
method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
105+
metadata = method.call_args[1]['metadata']
106+
# Metadata should have just one items: the client info metadata.
107+
assert len(metadata) == 1
108+
109+
97110
@mock.patch('time.sleep')
98111
def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
99112
method = mock.Mock(

0 commit comments

Comments
 (0)