Skip to content

Commit f63a88d

Browse files
committed
Prefix grpc method 'call' and 'request' arguments with '$' to avoid shadowing user methods with the same name
Fixes google#159 Fixes google#963
1 parent 610943a commit f63a88d

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

protoc_plugin/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* `protoc_plugin` and generated files now require Dart 3.3.0. (#953)
1616
* Fix performance issues when handling documentation comments in protobufs.
1717
([#935], [#955])
18+
* Fix grpc methods with names 'call' and 'request' conflicting with other
19+
methods in the generated code and causing compile-time errors. ([#963],
20+
[#159])
1821

1922
[#738]: https://github.com/google/protobuf.dart/issues/738
2023
[#903]: https://github.com/google/protobuf.dart/pull/903
@@ -26,6 +29,8 @@
2629
[#953]: https://github.com/google/protobuf.dart/pull/953
2730
[#935]: https://github.com/google/protobuf.dart/pull/935
2831
[#955]: https://github.com/google/protobuf.dart/pull/955
32+
[#963]: https://github.com/google/protobuf.dart/issues/963
33+
[#153]: https://github.com/google/protobuf.dart/issues/153
2934

3035
## 21.1.2
3136

protoc_plugin/lib/src/grpc_generator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,20 @@ class _GrpcMethod {
276276
if (_clientStreaming) return;
277277

278278
out.addBlock(
279-
'$_serverReturnType ${_dartName}_Pre($_serviceCall call, $_future<$_requestType> request) async${_serverStreaming ? '*' : ''} {',
279+
'$_serverReturnType ${_dartName}_Pre($_serviceCall \$call, $_future<$_requestType> \$request) async${_serverStreaming ? '*' : ''} {',
280280
'}', () {
281281
if (_serverStreaming) {
282-
out.println('yield* $_dartName(call, await request);');
282+
out.println('yield* $_dartName(\$call, await \$request);');
283283
} else {
284-
out.println('return $_dartName(call, await request);');
284+
out.println('return $_dartName(\$call, await \$request);');
285285
}
286286
});
287287
out.println();
288288
}
289289

290290
void generateServiceMethodStub(IndentingWriter out) {
291291
out.println(
292-
'$_serverReturnType $_dartName($_serviceCall call, $_argumentType request);');
292+
'$_serverReturnType $_dartName($_serviceCall \$call, $_argumentType \$request);');
293293
}
294294

295295
static final String _serviceCall = '$grpcImportPrefix.ServiceCall';

protoc_plugin/test/file_generator_test.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,49 @@ void main() {
288288
..outputType = '.Output'
289289
..clientStreaming = false
290290
..serverStreaming = false;
291+
291292
final clientStreaming = MethodDescriptorProto()
292293
..name = 'ClientStreaming'
293294
..inputType = '.Input'
294295
..outputType = '.Output'
295296
..clientStreaming = true
296297
..serverStreaming = false;
298+
297299
final serverStreaming = MethodDescriptorProto()
298300
..name = 'ServerStreaming'
299301
..inputType = '.Input'
300302
..outputType = '.Output'
301303
..clientStreaming = false
302304
..serverStreaming = true;
305+
303306
final bidirectional = MethodDescriptorProto()
304307
..name = 'Bidirectional'
305308
..inputType = '.Input'
306309
..outputType = '.Output'
307310
..clientStreaming = true
308311
..serverStreaming = true;
309312

313+
// A method with name 'call' to test that it doesn't conflict with the
314+
// method arguments with the same name, see issue #963.
315+
final keywordCall = MethodDescriptorProto()
316+
..name = 'Call'
317+
..inputType = '.Input'
318+
..outputType = '.Output'
319+
..clientStreaming = false
320+
..serverStreaming = false;
321+
322+
// A method with name 'request' to test that it doesn't conflict with the
323+
// method arguments with the same name, see issue #159.
324+
final keywordRequest = MethodDescriptorProto()
325+
..name = 'Request'
326+
..inputType = '.Input'
327+
..outputType = '.Output'
328+
..clientStreaming = false
329+
..serverStreaming = false;
330+
310331
final sd = ServiceDescriptorProto()
311332
..name = 'Test'
312-
..method.addAll([unary, clientStreaming, serverStreaming, bidirectional]);
333+
..method.addAll([unary, clientStreaming, serverStreaming, bidirectional, keywordCall, keywordRequest]);
313334

314335
final fd = FileDescriptorProto()
315336
..name = 'test'

protoc_plugin/test/goldens/grpc_service.pbgrpc

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class TestClient extends $grpc.Client {
3737
'/Test/Bidirectional',
3838
($0.Input value) => value.writeToBuffer(),
3939
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
40+
static final _$call = $grpc.ClientMethod<$0.Input, $0.Output>(
41+
'/Test/Call',
42+
($0.Input value) => value.writeToBuffer(),
43+
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
44+
static final _$request = $grpc.ClientMethod<$0.Input, $0.Output>(
45+
'/Test/Request',
46+
($0.Input value) => value.writeToBuffer(),
47+
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
4048

4149
TestClient($grpc.ClientChannel channel,
4250
{$grpc.CallOptions? options,
@@ -59,6 +67,14 @@ class TestClient extends $grpc.Client {
5967
$grpc.ResponseStream<$0.Output> bidirectional($async.Stream<$0.Input> request, {$grpc.CallOptions? options}) {
6068
return $createStreamingCall(_$bidirectional, request, options: options);
6169
}
70+
71+
$grpc.ResponseFuture<$0.Output> call($0.Input request, {$grpc.CallOptions? options}) {
72+
return $createUnaryCall(_$call, request, options: options);
73+
}
74+
75+
$grpc.ResponseFuture<$0.Output> request($0.Input request, {$grpc.CallOptions? options}) {
76+
return $createUnaryCall(_$request, request, options: options);
77+
}
6278
}
6379

6480
@$pb.GrpcServiceName('Test')
@@ -94,18 +110,42 @@ abstract class TestServiceBase extends $grpc.Service {
94110
true,
95111
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
96112
($0.Output value) => value.writeToBuffer()));
113+
$addMethod($grpc.ServiceMethod<$0.Input, $0.Output>(
114+
'Call',
115+
call_Pre,
116+
false,
117+
false,
118+
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
119+
($0.Output value) => value.writeToBuffer()));
120+
$addMethod($grpc.ServiceMethod<$0.Input, $0.Output>(
121+
'Request',
122+
request_Pre,
123+
false,
124+
false,
125+
($core.List<$core.int> value) => $0.Input.fromBuffer(value),
126+
($0.Output value) => value.writeToBuffer()));
127+
}
128+
129+
$async.Future<$0.Output> unary_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
130+
return unary($call, await $request);
131+
}
132+
133+
$async.Stream<$0.Output> serverStreaming_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async* {
134+
yield* serverStreaming($call, await $request);
97135
}
98136

99-
$async.Future<$0.Output> unary_Pre($grpc.ServiceCall call, $async.Future<$0.Input> request) async {
100-
return unary(call, await request);
137+
$async.Future<$0.Output> call_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
138+
return call($call, await $request);
101139
}
102140

103-
$async.Stream<$0.Output> serverStreaming_Pre($grpc.ServiceCall call, $async.Future<$0.Input> request) async* {
104-
yield* serverStreaming(call, await request);
141+
$async.Future<$0.Output> request_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
142+
return request($call, await $request);
105143
}
106144

107-
$async.Future<$0.Output> unary($grpc.ServiceCall call, $0.Input request);
108-
$async.Future<$0.Output> clientStreaming($grpc.ServiceCall call, $async.Stream<$0.Input> request);
109-
$async.Stream<$0.Output> serverStreaming($grpc.ServiceCall call, $0.Input request);
110-
$async.Stream<$0.Output> bidirectional($grpc.ServiceCall call, $async.Stream<$0.Input> request);
145+
$async.Future<$0.Output> unary($grpc.ServiceCall $call, $0.Input $request);
146+
$async.Future<$0.Output> clientStreaming($grpc.ServiceCall $call, $async.Stream<$0.Input> $request);
147+
$async.Stream<$0.Output> serverStreaming($grpc.ServiceCall $call, $0.Input $request);
148+
$async.Stream<$0.Output> bidirectional($grpc.ServiceCall $call, $async.Stream<$0.Input> $request);
149+
$async.Future<$0.Output> call($grpc.ServiceCall $call, $0.Input $request);
150+
$async.Future<$0.Output> request($grpc.ServiceCall $call, $0.Input $request);
111151
}

0 commit comments

Comments
 (0)