Skip to content

Commit ac9cad8

Browse files
committed
fix: add support for protobuf 5.x
1 parent 828ffe1 commit ac9cad8

File tree

7 files changed

+182
-43
lines changed

7 files changed

+182
-43
lines changed

.github/workflows/docs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
run: |
1818
python -m pip install --upgrade setuptools pip wheel
1919
python -m pip install nox
20+
- name: Install pytest
21+
run: |
22+
python -m pip install pytest
2023
- name: Run docs
2124
run: |
2225
nox -s docs
@@ -33,6 +36,9 @@ jobs:
3336
run: |
3437
python -m pip install --upgrade setuptools pip wheel
3538
python -m pip install nox
39+
- name: Install pytest
40+
run: |
41+
python -m pip install pytest
3642
- name: Run docfx
3743
run: |
3844
nox -s docfx

.github/workflows/lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
run: |
1818
python -m pip install --upgrade setuptools pip wheel
1919
python -m pip install nox
20+
- name: Install pytest
21+
run: |
22+
python -m pip install pytest
2023
- name: Run lint
2124
run: |
2225
nox -s lint

.github/workflows/mypy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
run: |
1818
python -m pip install --upgrade setuptools pip wheel
1919
python -m pip install nox
20+
- name: Install pytest
21+
run: |
22+
python -m pip install pytest
2023
- name: Run mypy
2124
run: |
2225
nox -s mypy

.github/workflows/unittest.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
option: ["", "_grpc_gcp", "_wo_grpc"]
14+
option: ["", "_grpc_gcp", "_wo_grpc", "_prerelease"]
1515
python:
1616
- "3.7"
1717
- "3.8"
@@ -26,6 +26,16 @@ jobs:
2626
python: 3.8
2727
- option: "_wo_grpc"
2828
python: 3.9
29+
- option: "_prerelease"
30+
python: 3.7
31+
- option: "_prerelease"
32+
python: 3.8
33+
- option: "_prerelease"
34+
python: 3.9
35+
- option: "_prerelease"
36+
python: 3.10
37+
- option: "_prerelease"
38+
python: 3.11
2939
steps:
3040
- name: Checkout
3141
uses: actions/checkout@v4
@@ -37,6 +47,9 @@ jobs:
3747
run: |
3848
python -m pip install --upgrade setuptools pip wheel
3949
python -m pip install nox
50+
- name: Install pytest
51+
run: |
52+
python -m pip install pytest
4053
- name: Run unit tests
4154
env:
4255
COVERAGE_FILE: .coverage${{ matrix.option }}-${{matrix.python }}

google/api_core/operations_v1/transports/rest.py

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
from google.longrunning import operations_pb2 # type: ignore
3030
from google.protobuf import empty_pb2 # type: ignore
3131
from google.protobuf import json_format # type: ignore
32+
import google.protobuf
33+
3234
import grpc
3335
from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO, OperationsTransport
3436

37+
PROTOBUF_VERSION = google.protobuf.__version__
38+
3539
OptionalRetry = Union[retries.Retry, object]
3640

3741
DEFAULT_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

Comments
 (0)