@@ -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
0 commit comments