Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: methods returning Operation w/o operation_info are now allowed. #1047

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions gapic/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ def _maybe_get_lro(
# If the output type is google.longrunning.Operation, we use
# a specialized object in its place.
if meth_pb.output_type.endswith('google.longrunning.Operation'):
if not meth_pb.options.HasExtension(operations_pb2.operation_info):
# This is not a long running operation even though it returns
# an Operation.
return None
op = meth_pb.options.Extensions[operations_pb2.operation_info]
if not op.response_type or not op.metadata_type:
raise TypeError(
Expand Down
54 changes: 52 additions & 2 deletions tests/unit/schema/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,19 +944,69 @@ def test_lro():
assert len(lro_proto.messages) == 1


def test_lro_missing_annotation():
def test_lro_operation_no_annotation():
# A method that returns google.longrunning.Operation,
# but has no operation_info option, is treated as not lro.

# Set up a prior proto that mimics google/protobuf/empty.proto
lro_proto = api.Proto.build(make_file_pb2(
name='operations.proto', package='google.longrunning',
messages=(make_message_pb2(name='Operation'),),
), file_to_generate=False, naming=make_naming())

# Set up a method with an LRO but no annotation.
# Set up a method that returns an Operation, but has no annotation.
method_pb2 = descriptor_pb2.MethodDescriptorProto(
name='GetOperation',
input_type='google.example.v3.GetOperationRequest',
output_type='google.longrunning.Operation',
)

# Set up the service with an RPC.
service_pb = descriptor_pb2.ServiceDescriptorProto(
name='OperationService',
method=(method_pb2,),
)

# Set up the messages, including the annotated ones.
messages = (
make_message_pb2(name='GetOperationRequest', fields=()),
)

# Finally, set up the file that encompasses these.
fdp = make_file_pb2(
package='google.example.v3',
messages=messages,
services=(service_pb,),
)

# Make the proto object.
proto = api.Proto.build(fdp, file_to_generate=True, prior_protos={
'google/longrunning/operations.proto': lro_proto,
}, naming=make_naming())

service = proto.services['google.example.v3.OperationService']
method = service.methods['GetOperation']
assert method.lro is None


def test_lro_bad_annotation():
# Set up a prior proto that mimics google/protobuf/empty.proto
lro_proto = api.Proto.build(make_file_pb2(
name='operations.proto', package='google.longrunning',
messages=(make_message_pb2(name='Operation'),),
), file_to_generate=False, naming=make_naming())

# Set up a method with an LRO and incomplete annotation.
method_pb2 = descriptor_pb2.MethodDescriptorProto(
name='AsyncDoThing',
input_type='google.example.v3.AsyncDoThingRequest',
output_type='google.longrunning.Operation',
)
method_pb2.options.Extensions[operations_pb2.operation_info].MergeFrom(
operations_pb2.OperationInfo(
response_type='google.example.v3.AsyncDoThingResponse',
),
)

# Set up the service with an RPC.
service_pb = descriptor_pb2.ServiceDescriptorProto(
Expand Down