Skip to content

Commit 0df24a1

Browse files
kbandesKenneth Bandes
andauthored
fix: methods returning Operation w/o operation_info are now allowed. (#1047)
Formerly, a method that returned google.longrunning.Operation but did not have the google.longrunning.operation_info extension was considered an error; but there are valid cases for this, particularly in the Operation service itself. This change makes it acceptable to have a method like this. It is not treated as a long-running operation. Co-authored-by: Kenneth Bandes <kbandes@google.com>
1 parent 7bdf3d1 commit 0df24a1

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

packages/gapic-generator/gapic/schema/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,10 @@ def _maybe_get_lro(
817817
# If the output type is google.longrunning.Operation, we use
818818
# a specialized object in its place.
819819
if meth_pb.output_type.endswith('google.longrunning.Operation'):
820+
if not meth_pb.options.HasExtension(operations_pb2.operation_info):
821+
# This is not a long running operation even though it returns
822+
# an Operation.
823+
return None
820824
op = meth_pb.options.Extensions[operations_pb2.operation_info]
821825
if not op.response_type or not op.metadata_type:
822826
raise TypeError(

packages/gapic-generator/tests/unit/schema/test_api.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,19 +944,69 @@ def test_lro():
944944
assert len(lro_proto.messages) == 1
945945

946946

947-
def test_lro_missing_annotation():
947+
def test_lro_operation_no_annotation():
948+
# A method that returns google.longrunning.Operation,
949+
# but has no operation_info option, is treated as not lro.
950+
948951
# Set up a prior proto that mimics google/protobuf/empty.proto
949952
lro_proto = api.Proto.build(make_file_pb2(
950953
name='operations.proto', package='google.longrunning',
951954
messages=(make_message_pb2(name='Operation'),),
952955
), file_to_generate=False, naming=make_naming())
953956

954-
# Set up a method with an LRO but no annotation.
957+
# Set up a method that returns an Operation, but has no annotation.
958+
method_pb2 = descriptor_pb2.MethodDescriptorProto(
959+
name='GetOperation',
960+
input_type='google.example.v3.GetOperationRequest',
961+
output_type='google.longrunning.Operation',
962+
)
963+
964+
# Set up the service with an RPC.
965+
service_pb = descriptor_pb2.ServiceDescriptorProto(
966+
name='OperationService',
967+
method=(method_pb2,),
968+
)
969+
970+
# Set up the messages, including the annotated ones.
971+
messages = (
972+
make_message_pb2(name='GetOperationRequest', fields=()),
973+
)
974+
975+
# Finally, set up the file that encompasses these.
976+
fdp = make_file_pb2(
977+
package='google.example.v3',
978+
messages=messages,
979+
services=(service_pb,),
980+
)
981+
982+
# Make the proto object.
983+
proto = api.Proto.build(fdp, file_to_generate=True, prior_protos={
984+
'google/longrunning/operations.proto': lro_proto,
985+
}, naming=make_naming())
986+
987+
service = proto.services['google.example.v3.OperationService']
988+
method = service.methods['GetOperation']
989+
assert method.lro is None
990+
991+
992+
def test_lro_bad_annotation():
993+
# Set up a prior proto that mimics google/protobuf/empty.proto
994+
lro_proto = api.Proto.build(make_file_pb2(
995+
name='operations.proto', package='google.longrunning',
996+
messages=(make_message_pb2(name='Operation'),),
997+
), file_to_generate=False, naming=make_naming())
998+
999+
# Set up a method with an LRO and incomplete annotation.
9551000
method_pb2 = descriptor_pb2.MethodDescriptorProto(
9561001
name='AsyncDoThing',
9571002
input_type='google.example.v3.AsyncDoThingRequest',
9581003
output_type='google.longrunning.Operation',
9591004
)
1005+
method_pb2.options.Extensions[operations_pb2.operation_info].MergeFrom(
1006+
operations_pb2.OperationInfo(
1007+
response_type='google.example.v3.AsyncDoThingResponse',
1008+
),
1009+
)
9601010

9611011
# Set up the service with an RPC.
9621012
service_pb = descriptor_pb2.ServiceDescriptorProto(

0 commit comments

Comments
 (0)