Skip to content

Commit e377892

Browse files
committed
Address review comments
1 parent 9d73085 commit e377892

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

google/api_core/path_template.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ def transcode(http_options, message=None, **request_kwargs):
298298
# Assign body and query params
299299
body = http_option.get("body")
300300

301+
# gapic-generator-python appends underscores to field names
302+
# that collide with python keywords.
303+
# `_` is stripped away as it is not possible to
304+
# natively define a field with a trailing underscore in protobuf.
305+
# See related issue
306+
# https://github.com/googleapis/python-api-core/issues/227
307+
if isinstance(leftovers, dict):
308+
leftovers = {key.rstrip("_"): val for key, val in leftovers.items()}
309+
301310
if body:
302311
if body == "*":
303312
request["body"] = leftovers
@@ -308,27 +317,18 @@ def transcode(http_options, message=None, **request_kwargs):
308317
else:
309318
try:
310319
if message:
311-
try:
312-
request["body"] = getattr(leftovers, body)
313-
delete_field(leftovers, body)
314-
except AttributeError as e:
315-
# gapic-generator-python appends underscores to field names
316-
# that collide with python keywords.
317-
# `_` is stripped away as it is not possible to
318-
# natively define a field with a trailing underscore in protobuf.
319-
# See related issue
320-
# https://github.com/googleapis/python-api-core/issues/227
321-
if hasattr(leftovers, body + "_"):
322-
request["body"] = getattr(leftovers, body + "_")
323-
delete_field(leftovers, body + "_")
324-
else:
325-
raise e
326-
else:
327320
# gapic-generator-python appends underscores to field names
328321
# that collide with python keywords.
329-
leftovers = {
330-
key.rstrip("_"): val for key, val in leftovers.items()
331-
}
322+
# `_` is stripped away as it is not possible to
323+
# natively define a field with a trailing underscore in protobuf.
324+
# See related issue
325+
# https://github.com/googleapis/python-api-core/issues/227
326+
field_suffix = ""
327+
if hasattr(leftovers, body + "_"):
328+
field_suffix = "_"
329+
request["body"] = getattr(leftovers, f"{body}{field_suffix}")
330+
delete_field(leftovers, f"{body}{field_suffix}")
331+
else:
332332
request["body"] = leftovers.pop(body)
333333
except (KeyError, AttributeError):
334334
continue

tests/unit/test_path_template.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Breakpoint(proto.Message):
3030
class SomeMessage(proto.Message):
3131
breakpoint_ = proto.Field(Breakpoint, number=1)
3232
debuggee_id = proto.Field(proto.STRING, number=2)
33+
stacktrace_ = proto.Field(proto.STRING, number=3)
3334

3435

3536
@pytest.mark.parametrize(
@@ -434,13 +435,15 @@ def test_transcode_with_wildcard(
434435
# Single field body with reserved keyword, using message where field name has trailing underscore
435436
[
436437
[["post", "/v1/no/template", "breakpoint"]],
437-
SomeMessage(breakpoint_=Breakpoint(name="test"), debuggee_id="test")._pb,
438+
SomeMessage(
439+
breakpoint_=Breakpoint(name="foo"), debuggee_id="bar", stacktrace_="baz"
440+
)._pb,
438441
{},
439442
[
440443
"post",
441444
"/v1/no/template",
442-
Breakpoint(name="test")._pb,
443-
SomeMessage(debuggee_id="test")._pb,
445+
Breakpoint(name="foo")._pb,
446+
SomeMessage(debuggee_id="bar", stacktrace_="baz")._pb,
444447
],
445448
],
446449
[

0 commit comments

Comments
 (0)