2929from google .longrunning import operations_pb2 # type: ignore
3030from google .protobuf import empty_pb2 # type: ignore
3131from google .protobuf import json_format # type: ignore
32+ import google .protobuf
33+
3234import grpc
3335from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO , OperationsTransport
3436
37+ PROTOBUF_VERSION = google .protobuf .__version__
38+
3539OptionalRetry = Union [retries .Retry , object ]
3640
3741DEFAULT_CLIENT_INFO = gapic_v1 .client_info .ClientInfo (
@@ -184,11 +188,22 @@ def _list_operations(
184188 "google.longrunning.Operations.ListOperations"
185189 ]
186190
187- request_kwargs = json_format .MessageToDict (
188- request ,
189- preserving_proto_field_name = True ,
190- including_default_value_fields = True ,
191- )
191+ # For backwards compatibility with protobuf 3.x 4.x
192+ # Remove once support for protobuf 3.x and 4.x is dropped
193+ # https://github.com/googleapis/python-api-core/issues/643
194+ if PROTOBUF_VERSION [0 :2 ] in ["3." , "4." ]:
195+ request_kwargs = json_format .MessageToDict (
196+ request ,
197+ preserving_proto_field_name = True ,
198+ including_default_value_fields = True , # type: ignore # backward compatibility
199+ )
200+ else :
201+ request_kwargs = json_format .MessageToDict (
202+ request ,
203+ preserving_proto_field_name = True ,
204+ always_print_fields_with_no_presence = True ,
205+ )
206+
192207 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
193208
194209 uri = transcoded_request ["uri" ]
@@ -199,7 +214,6 @@ def _list_operations(
199214 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
200215 query_params = json_format .MessageToDict (
201216 query_params_request ,
202- including_default_value_fields = False ,
203217 preserving_proto_field_name = False ,
204218 use_integers_for_enums = False ,
205219 )
@@ -265,11 +279,22 @@ def _get_operation(
265279 "google.longrunning.Operations.GetOperation"
266280 ]
267281
268- request_kwargs = json_format .MessageToDict (
269- request ,
270- preserving_proto_field_name = True ,
271- including_default_value_fields = True ,
272- )
282+ # For backwards compatibility with protobuf 3.x 4.x
283+ # Remove once support for protobuf 3.x and 4.x is dropped
284+ # https://github.com/googleapis/python-api-core/issues/643
285+ if PROTOBUF_VERSION [0 :2 ] in ["3." , "4." ]:
286+ request_kwargs = json_format .MessageToDict (
287+ request ,
288+ preserving_proto_field_name = True ,
289+ including_default_value_fields = True , # type: ignore # backward compatibility
290+ )
291+ else :
292+ request_kwargs = json_format .MessageToDict (
293+ request ,
294+ preserving_proto_field_name = True ,
295+ always_print_fields_with_no_presence = True ,
296+ )
297+
273298 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
274299
275300 uri = transcoded_request ["uri" ]
@@ -280,7 +305,6 @@ def _get_operation(
280305 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
281306 query_params = json_format .MessageToDict (
282307 query_params_request ,
283- including_default_value_fields = False ,
284308 preserving_proto_field_name = False ,
285309 use_integers_for_enums = False ,
286310 )
@@ -339,11 +363,21 @@ def _delete_operation(
339363 "google.longrunning.Operations.DeleteOperation"
340364 ]
341365
342- request_kwargs = json_format .MessageToDict (
343- request ,
344- preserving_proto_field_name = True ,
345- including_default_value_fields = True ,
346- )
366+ # For backwards compatibility with protobuf 3.x 4.x
367+ # Remove once support for protobuf 3.x and 4.x is dropped
368+ # https://github.com/googleapis/python-api-core/issues/643
369+ if PROTOBUF_VERSION [0 :2 ] in ["3." , "4." ]:
370+ request_kwargs = json_format .MessageToDict (
371+ request ,
372+ preserving_proto_field_name = True ,
373+ including_default_value_fields = True , # type: ignore # backward compatibility
374+ )
375+ else :
376+ request_kwargs = json_format .MessageToDict (
377+ request ,
378+ preserving_proto_field_name = True ,
379+ always_print_fields_with_no_presence = True ,
380+ )
347381 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
348382
349383 uri = transcoded_request ["uri" ]
@@ -354,7 +388,6 @@ def _delete_operation(
354388 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
355389 query_params = json_format .MessageToDict (
356390 query_params_request ,
357- including_default_value_fields = False ,
358391 preserving_proto_field_name = False ,
359392 use_integers_for_enums = False ,
360393 )
@@ -411,19 +444,28 @@ def _cancel_operation(
411444 "google.longrunning.Operations.CancelOperation"
412445 ]
413446
414- request_kwargs = json_format .MessageToDict (
415- request ,
416- preserving_proto_field_name = True ,
417- including_default_value_fields = True ,
418- )
447+ # For backwards compatibility with protobuf 3.x 4.x
448+ # Remove once support for protobuf 3.x and 4.x is dropped
449+ # https://github.com/googleapis/python-api-core/issues/643
450+ if PROTOBUF_VERSION [0 :2 ] in ["3." , "4." ]:
451+ request_kwargs = json_format .MessageToDict (
452+ request ,
453+ preserving_proto_field_name = True ,
454+ including_default_value_fields = True , # type: ignore # backward compatibility
455+ )
456+ else :
457+ request_kwargs = json_format .MessageToDict (
458+ request ,
459+ preserving_proto_field_name = True ,
460+ always_print_fields_with_no_presence = True ,
461+ )
419462 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
420463
421464 # Jsonify the request body
422465 body_request = operations_pb2 .CancelOperationRequest ()
423466 json_format .ParseDict (transcoded_request ["body" ], body_request )
424467 body = json_format .MessageToDict (
425468 body_request ,
426- including_default_value_fields = False ,
427469 preserving_proto_field_name = False ,
428470 use_integers_for_enums = False ,
429471 )
@@ -435,7 +477,6 @@ def _cancel_operation(
435477 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
436478 query_params = json_format .MessageToDict (
437479 query_params_request ,
438- including_default_value_fields = False ,
439480 preserving_proto_field_name = False ,
440481 use_integers_for_enums = False ,
441482 )
0 commit comments