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

Prefix grpc method 'call' and 'request' arguments with '$' to avoid shadowing user methods with the same name #964

Merged
merged 4 commits into from
Mar 7, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* `protoc_plugin` and generated files now require Dart 3.3.0. (#953)
* Fix performance issues when handling documentation comments in protobufs.
([#935], [#955])
* Fix grpc methods with names 'call' and 'request' conflicting with other
methods in the generated code and causing compile-time errors. ([#963],
[#159])

[#738]: https://github.com/google/protobuf.dart/issues/738
[#903]: https://github.com/google/protobuf.dart/pull/903
Expand All @@ -26,6 +29,8 @@
[#953]: https://github.com/google/protobuf.dart/pull/953
[#935]: https://github.com/google/protobuf.dart/pull/935
[#955]: https://github.com/google/protobuf.dart/pull/955
[#963]: https://github.com/google/protobuf.dart/issues/963
[#153]: https://github.com/google/protobuf.dart/issues/153

## 21.1.2

Expand Down
6 changes: 3 additions & 3 deletions protoc_plugin/lib/src/grpc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ class _GrpcMethod {
if (_clientStreaming) return;

out.addBlock(
'$_serverReturnType ${_dartName}_Pre($_serviceCall call, $_future<$_requestType> request) async${_serverStreaming ? '*' : ''} {',
'$_serverReturnType ${_dartName}_Pre($_serviceCall \$call, $_future<$_requestType> \$request) async${_serverStreaming ? '*' : ''} {',
'}', () {
if (_serverStreaming) {
out.println('yield* $_dartName(call, await request);');
out.println('yield* $_dartName(\$call, await \$request);');
} else {
out.println('return $_dartName(call, await request);');
out.println('return $_dartName(\$call, await \$request);');
}
});
out.println();
Expand Down
30 changes: 29 additions & 1 deletion protoc_plugin/test/file_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,56 @@ void main() {
..outputType = '.Output'
..clientStreaming = false
..serverStreaming = false;

final clientStreaming = MethodDescriptorProto()
..name = 'ClientStreaming'
..inputType = '.Input'
..outputType = '.Output'
..clientStreaming = true
..serverStreaming = false;

final serverStreaming = MethodDescriptorProto()
..name = 'ServerStreaming'
..inputType = '.Input'
..outputType = '.Output'
..clientStreaming = false
..serverStreaming = true;

final bidirectional = MethodDescriptorProto()
..name = 'Bidirectional'
..inputType = '.Input'
..outputType = '.Output'
..clientStreaming = true
..serverStreaming = true;

// A method with name 'call' to test that it doesn't conflict with the
// method arguments with the same name, see issue #963.
final keywordCall = MethodDescriptorProto()
..name = 'Call'
..inputType = '.Input'
..outputType = '.Output'
..clientStreaming = false
..serverStreaming = false;

// A method with name 'request' to test that it doesn't conflict with the
// method arguments with the same name, see issue #159.
final keywordRequest = MethodDescriptorProto()
..name = 'Request'
..inputType = '.Input'
..outputType = '.Output'
..clientStreaming = false
..serverStreaming = false;

final sd = ServiceDescriptorProto()
..name = 'Test'
..method.addAll([unary, clientStreaming, serverStreaming, bidirectional]);
..method.addAll([
unary,
clientStreaming,
serverStreaming,
bidirectional,
keywordCall,
keywordRequest
]);

final fd = FileDescriptorProto()
..name = 'test'
Expand Down
48 changes: 44 additions & 4 deletions protoc_plugin/test/goldens/grpc_service.pbgrpc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class TestClient extends $grpc.Client {
'/Test/Bidirectional',
($0.Input value) => value.writeToBuffer(),
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
static final _$call = $grpc.ClientMethod<$0.Input, $0.Output>(
'/Test/Call',
($0.Input value) => value.writeToBuffer(),
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
static final _$request = $grpc.ClientMethod<$0.Input, $0.Output>(
'/Test/Request',
($0.Input value) => value.writeToBuffer(),
($core.List<$core.int> value) => $0.Output.fromBuffer(value));

TestClient($grpc.ClientChannel channel,
{$grpc.CallOptions? options,
Expand All @@ -59,6 +67,14 @@ class TestClient extends $grpc.Client {
$grpc.ResponseStream<$0.Output> bidirectional($async.Stream<$0.Input> request, {$grpc.CallOptions? options}) {
return $createStreamingCall(_$bidirectional, request, options: options);
}

$grpc.ResponseFuture<$0.Output> call($0.Input request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$call, request, options: options);
}

$grpc.ResponseFuture<$0.Output> request($0.Input request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$request, request, options: options);
}
}

@$pb.GrpcServiceName('Test')
Expand Down Expand Up @@ -94,18 +110,42 @@ abstract class TestServiceBase extends $grpc.Service {
true,
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
($0.Output value) => value.writeToBuffer()));
$addMethod($grpc.ServiceMethod<$0.Input, $0.Output>(
'Call',
call_Pre,
false,
false,
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
($0.Output value) => value.writeToBuffer()));
$addMethod($grpc.ServiceMethod<$0.Input, $0.Output>(
'Request',
request_Pre,
false,
false,
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
($0.Output value) => value.writeToBuffer()));
}

$async.Future<$0.Output> unary_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
return unary($call, await $request);
}

$async.Stream<$0.Output> serverStreaming_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async* {
yield* serverStreaming($call, await $request);
}

$async.Future<$0.Output> unary_Pre($grpc.ServiceCall call, $async.Future<$0.Input> request) async {
return unary(call, await request);
$async.Future<$0.Output> call_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
return call($call, await $request);
}

$async.Stream<$0.Output> serverStreaming_Pre($grpc.ServiceCall call, $async.Future<$0.Input> request) async* {
yield* serverStreaming(call, await request);
$async.Future<$0.Output> request_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
return request($call, await $request);
}

$async.Future<$0.Output> unary($grpc.ServiceCall call, $0.Input request);
$async.Future<$0.Output> clientStreaming($grpc.ServiceCall call, $async.Stream<$0.Input> request);
$async.Stream<$0.Output> serverStreaming($grpc.ServiceCall call, $0.Input request);
$async.Stream<$0.Output> bidirectional($grpc.ServiceCall call, $async.Stream<$0.Input> request);
$async.Future<$0.Output> call($grpc.ServiceCall call, $0.Input request);
$async.Future<$0.Output> request($grpc.ServiceCall call, $0.Input request);
}