Skip to content

fix: remove trailing underscore in transcoding #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
40 changes: 20 additions & 20 deletions google/api_core/path_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ def transcode(http_options, message=None, **request_kwargs):
# Assign body and query params
body = http_option.get("body")

# gapic-generator-python appends an underscore to field names
# that collide with python keywords.
# `_` is stripped away as it is not possible to
# natively define a field with a trailing underscore in protobuf.
# See related issue
# https://github.com/googleapis/python-api-core/issues/227
if isinstance(leftovers, dict):
leftovers = {key.rstrip("_"): val for key, val in leftovers.items()}

if body:
if body == "*":
request["body"] = leftovers
Expand All @@ -308,27 +317,18 @@ def transcode(http_options, message=None, **request_kwargs):
else:
try:
if message:
try:
request["body"] = getattr(leftovers, body)
delete_field(leftovers, body)
except AttributeError as e:
# gapic-generator-python appends underscores to field names
# that collide with python keywords.
# `_` is stripped away as it is not possible to
# natively define a field with a trailing underscore in protobuf.
# See related issue
# https://github.com/googleapis/python-api-core/issues/227
if hasattr(leftovers, body + "_"):
request["body"] = getattr(leftovers, body + "_")
delete_field(leftovers, body + "_")
else:
raise e
else:
# gapic-generator-python appends underscores to field names
# gapic-generator-python append an underscores to field names
# that collide with python keywords.
leftovers = {
key.rstrip("_"): val for key, val in leftovers.items()
}
# `_` is stripped away as it is not possible to
# natively define a field with a trailing underscore in protobuf.
# See related issue
# https://github.com/googleapis/python-api-core/issues/227
field_suffix = ""
if hasattr(leftovers, body + "_"):
field_suffix = "_"
request["body"] = getattr(leftovers, f"{body}{field_suffix}")
delete_field(leftovers, f"{body}{field_suffix}")
else:
request["body"] = leftovers.pop(body)
except (KeyError, AttributeError):
continue
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/test_path_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Breakpoint(proto.Message):
class SomeMessage(proto.Message):
breakpoint_ = proto.Field(Breakpoint, number=1)
debuggee_id = proto.Field(proto.STRING, number=2)
stacktrace_ = proto.Field(proto.STRING, number=3)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -434,13 +435,15 @@ def test_transcode_with_wildcard(
# Single field body with reserved keyword, using message where field name has trailing underscore
[
[["post", "/v1/no/template", "breakpoint"]],
SomeMessage(breakpoint_=Breakpoint(name="test"), debuggee_id="test")._pb,
SomeMessage(
breakpoint_=Breakpoint(name="foo"), debuggee_id="bar", stacktrace_="baz"
)._pb,
{},
[
"post",
"/v1/no/template",
Breakpoint(name="test")._pb,
SomeMessage(debuggee_id="test")._pb,
Breakpoint(name="foo")._pb,
SomeMessage(debuggee_id="bar", stacktrace_="baz")._pb,
],
],
[
Expand Down