Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

Commit 0ebbe41

Browse files
committed
fix: remove trailing underscore in transcoding
1 parent 2a6a36b commit 0ebbe41

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

google/api_core/path_template.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,24 @@ def transcode(http_options, message=None, **request_kwargs):
308308
else:
309309
try:
310310
if message:
311-
request["body"] = getattr(leftovers, body)
311+
try:
312+
request["body"] = getattr(leftovers, body)
313+
except AttributeError as e:
314+
# gapic-generator-python appends underscores to field names
315+
# that collide with python keywords.
316+
# `_` is stripped away as it is not possible to
317+
# natively define a field with a trailing underscore in protobuf.
318+
# See related issue
319+
# https://github.com/googleapis/python-api-core/issues/227
320+
if hasattr(leftovers, body + "_"):
321+
request["body"] = getattr(leftovers, body + "_")
322+
else:
323+
raise e
312324
delete_field(leftovers, body)
313325
else:
326+
# gapic-generator-python appends underscores to field names
327+
# that collide with python keywords.
328+
leftovers = {key.rstrip('_') : val for key, val in leftovers.items()}
314329
request["body"] = leftovers.pop(body)
315330
except (KeyError, AttributeError):
316331
continue

tests/unit/test_path_template.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ def test_transcode_with_wildcard(
413413
{"data": {"id": 1, "info": "some info"}, "foo": "bar"},
414414
["post", "/v1/no/template", {"id": 1, "info": "some info"}, {"foo": "bar"}],
415415
],
416+
# Single field body with trailing underscore
417+
[
418+
[["post", "/v1/no/template", "data"]],
419+
None,
420+
{"data": {"id": 1, "info": "some info"}, "foo_": "bar"},
421+
["post", "/v1/no/template", {"id": 1, "info": "some info"}, {"foo": "bar"}],
422+
],
416423
[
417424
[["post", "/v1/no/template", "oauth"]],
418425
auth_pb2.AuthenticationRule(

0 commit comments

Comments
 (0)