Skip to content

Commit dfb7cfc

Browse files
authored
Merge pull request #1897 from murgatroid99/proto-loader_client_method_conflicts
proto-loader: Avoid generating conflicting method names in service clients
2 parents fe6d9ef + c95219b commit dfb7cfc

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/proto-loader/bin/proto-loader-gen-types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,38 @@ function generateEnumInterface(formatter: TextFormatter, enumType: Protobuf.Enum
454454
formatter.writeLine('}');
455455
}
456456

457+
/**
458+
* This is a list of methods that are exist in the generic Client class in the
459+
* gRPC libraries. TypeScript has a problem with methods in subclasses with the
460+
* same names as methods in the superclass, but with mismatched APIs. So, we
461+
* avoid generating methods with these names in the service client interfaces.
462+
* We always generate two service client methods per service method: one camel
463+
* cased, and one with the original casing. So we will still generate one
464+
* service client method for any conflicting name.
465+
*
466+
* Technically, at runtime conflicting name in the service client method
467+
* actually shadows the original method, but TypeScript does not have a good
468+
* way to represent that. So this change is not 100% accurate, but it gets the
469+
* generated code to compile.
470+
*
471+
* This is just a list of the methods in the Client class definitions in
472+
473+
*/
474+
const CLIENT_RESERVED_METHOD_NAMES = new Set([
475+
'close',
476+
'getChannel',
477+
'waitForReady',
478+
'makeUnaryRequest',
479+
'makeClientStreamRequest',
480+
'makeServerStreamRequest',
481+
'makeBidiStreamRequest',
482+
'resolveCallInterceptors',
483+
/* These methods are private, but TypeScript is not happy with overriding even
484+
* private methods with mismatched APIs. */
485+
'checkOptionalUnaryResponseArguments',
486+
'checkMetadataAndOptions'
487+
]);
488+
457489
function generateServiceClientInterface(formatter: TextFormatter, serviceType: Protobuf.Service, options: GeneratorOptions) {
458490
if (options.includeComments) {
459491
formatComment(formatter, serviceType.comment);
@@ -463,6 +495,9 @@ function generateServiceClientInterface(formatter: TextFormatter, serviceType: P
463495
for (const methodName of Object.keys(serviceType.methods).sort()) {
464496
const method = serviceType.methods[methodName];
465497
for (const name of [methodName, camelCase(methodName)]) {
498+
if (CLIENT_RESERVED_METHOD_NAMES.has(name)) {
499+
continue;
500+
}
466501
if (options.includeComments) {
467502
formatComment(formatter, method.comment);
468503
}

packages/proto-loader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grpc/proto-loader",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"author": "Google Inc.",
55
"contributors": [
66
{

0 commit comments

Comments
 (0)