diff --git a/packages/grpc-tools/README.md b/packages/grpc-tools/README.md index 980ce93eb..d7407c429 100644 --- a/packages/grpc-tools/README.md +++ b/packages/grpc-tools/README.md @@ -20,3 +20,7 @@ one of the following: gRPC library, and instead generates `PackageDefinition` objects that can be passed to the `loadPackageDefinition` function provided by both the `grpc` and `@grpc/grpc-js` libraries. + - `omit_serialize_instanceof`: Omit the `instanceof` check for messages in + client code. This is useful when the message was renamed or is from a + different package, and serialization would fail with + `Expected argument of type …`. diff --git a/packages/grpc-tools/src/node_generator.cc b/packages/grpc-tools/src/node_generator.cc index 0d55dc39c..455554d5d 100644 --- a/packages/grpc-tools/src/node_generator.cc +++ b/packages/grpc-tools/src/node_generator.cc @@ -120,7 +120,9 @@ grpc::string NodeObjectPath(const Descriptor* descriptor) { } // Prints out the message serializer and deserializer functions -void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) { +void PrintMessageTransformer(const Descriptor* descriptor, + Printer* out, + const Parameters& params) { map template_vars; grpc::string full_name = descriptor->full_name(); template_vars["identifier_name"] = MessageIdentifierName(full_name); @@ -129,12 +131,14 @@ void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) { // Print the serializer out->Print(template_vars, "function serialize_$identifier_name$(arg) {\n"); out->Indent(); - out->Print(template_vars, "if (!(arg instanceof $node_name$)) {\n"); - out->Indent(); - out->Print(template_vars, - "throw new Error('Expected argument of type $name$');\n"); - out->Outdent(); - out->Print("}\n"); + if (!params.omit_serialize_instanceof) { + out->Print(template_vars, "if (!(arg instanceof $node_name$)) {\n"); + out->Indent(); + out->Print(template_vars, + "throw new Error('Expected argument of type $name$');\n"); + out->Outdent(); + out->Print("}\n"); + } out->Print("return Buffer.from(arg.serializeBinary());\n"); out->Outdent(); out->Print("}\n\n"); @@ -232,12 +236,14 @@ void PrintImports(const FileDescriptor* file, Printer* out, out->Print("\n"); } -void PrintTransformers(const FileDescriptor* file, Printer* out) { +void PrintTransformers(const FileDescriptor* file, + Printer* out, + const Parameters& params) { map messages = GetAllMessages(file); for (std::map::iterator it = messages.begin(); it != messages.end(); it++) { - PrintMessageTransformer(it->second, out); + PrintMessageTransformer(it->second, out, params); } out->Print("\n"); } @@ -273,7 +279,7 @@ grpc::string GenerateFile(const FileDescriptor* file, PrintImports(file, &out, params); - PrintTransformers(file, &out); + PrintTransformers(file, &out, params); PrintServices(file, &out, params); diff --git a/packages/grpc-tools/src/node_generator.h b/packages/grpc-tools/src/node_generator.h index a88d54a93..9ac7c4b8e 100644 --- a/packages/grpc-tools/src/node_generator.h +++ b/packages/grpc-tools/src/node_generator.h @@ -28,6 +28,8 @@ struct Parameters { bool generate_package_definition; // Use pure JavaScript gRPC Client bool grpc_js; + // Omit instanceof check for request messages + bool omit_serialize_instanceof; }; grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file, diff --git a/packages/grpc-tools/src/node_plugin.cc b/packages/grpc-tools/src/node_plugin.cc index b847bcaba..f703ba5eb 100644 --- a/packages/grpc-tools/src/node_plugin.cc +++ b/packages/grpc-tools/src/node_plugin.cc @@ -39,6 +39,7 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { grpc_node_generator::Parameters generator_parameters; generator_parameters.generate_package_definition = false; generator_parameters.grpc_js = false; + generator_parameters.omit_serialize_instanceof = false; if (!parameter.empty()) { std::vector parameters_list = grpc_generator::tokenize(parameter, ","); @@ -48,6 +49,8 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { generator_parameters.generate_package_definition = true; } else if (*parameter_string == "grpc_js") { generator_parameters.grpc_js = true; + } else if (*parameter_string == "omit_serialize_instanceof") { + generator_parameters.omit_serialize_instanceof = true; } } }