@@ -298,6 +298,15 @@ def transcode(http_options, message=None, **request_kwargs):
298
298
# Assign body and query params
299
299
body = http_option .get ("body" )
300
300
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
+
301
310
if body :
302
311
if body == "*" :
303
312
request ["body" ] = leftovers
@@ -308,27 +317,18 @@ def transcode(http_options, message=None, **request_kwargs):
308
317
else :
309
318
try :
310
319
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 :
327
320
# gapic-generator-python appends underscores to field names
328
321
# 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 :
332
332
request ["body" ] = leftovers .pop (body )
333
333
except (KeyError , AttributeError ):
334
334
continue
0 commit comments