From 725997981fa97f865428bbcb33c24c911073c4b2 Mon Sep 17 00:00:00 2001 From: Matt Nathan Date: Mon, 10 Jan 2022 15:22:42 +0000 Subject: [PATCH 1/3] Correctly support proto3 optional fields in commonjs+dts .d.ts output Fixes #1072 --- .../net/grpc/web/generator/grpc_generator.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc index 15e9c66b..94db81f0 100644 --- a/javascript/net/grpc/web/generator/grpc_generator.cc +++ b/javascript/net/grpc/web/generator/grpc_generator.cc @@ -825,11 +825,11 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, "set$js_field_name$(value?: $js_field_type$): " "$class_name$;\n"); } - if (field->type() == FieldDescriptor::TYPE_MESSAGE && + if ((field->type() == FieldDescriptor::TYPE_MESSAGE || field->is_optional()) && !field->is_repeated() && !field->is_map()) { printer->Print(vars, "has$js_field_name$(): boolean;\n"); } - if (field->type() == FieldDescriptor::TYPE_MESSAGE || + if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->is_optional() || field->is_repeated() || field->is_map()) { printer->Print(vars, "clear$js_field_name$(): $class_name$;\n"); } @@ -852,10 +852,12 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, for (int i = 0; i < desc->oneof_decl_count(); i++) { const OneofDescriptor* oneof = desc->oneof_decl(i); - vars["js_oneof_name"] = ToUpperCamel(ParseLowerUnderscore(oneof->name())); - printer->Print( - vars, "get$js_oneof_name$Case(): $class_name$.$js_oneof_name$Case;\n"); - printer->Print("\n"); + if (!oneof->is_synthetic()) { + vars["js_oneof_name"] = ToUpperCamel(ParseLowerUnderscore(oneof->name())); + printer->Print( + vars, "get$js_oneof_name$Case(): $class_name$.$js_oneof_name$Case;\n"); + printer->Print("\n"); + } } printer->Print( @@ -885,7 +887,7 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, } vars["js_field_name"] = js_field_name; vars["js_field_type"] = AsObjectFieldType(field, file); - if (field->type() != FieldDescriptor::TYPE_MESSAGE || + if ((field->type() != FieldDescriptor::TYPE_MESSAGE && !field->is_optional()) || field->is_repeated()) { printer->Print(vars, "$js_field_name$: $js_field_type$,\n"); } else { From 75b314524661be6df65ad8da625a3b75308413ef Mon Sep 17 00:00:00 2001 From: Matt Nathan Date: Mon, 10 Jan 2022 16:05:25 +0000 Subject: [PATCH 2/3] Use has_optional_keyword instead of is_optional --- javascript/net/grpc/web/generator/grpc_generator.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc index 94db81f0..a9ac5587 100644 --- a/javascript/net/grpc/web/generator/grpc_generator.cc +++ b/javascript/net/grpc/web/generator/grpc_generator.cc @@ -825,11 +825,11 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, "set$js_field_name$(value?: $js_field_type$): " "$class_name$;\n"); } - if ((field->type() == FieldDescriptor::TYPE_MESSAGE || field->is_optional()) && + if ((field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword()) && !field->is_repeated() && !field->is_map()) { printer->Print(vars, "has$js_field_name$(): boolean;\n"); } - if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->is_optional() || + if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword() || field->is_repeated() || field->is_map()) { printer->Print(vars, "clear$js_field_name$(): $class_name$;\n"); } @@ -887,7 +887,7 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, } vars["js_field_name"] = js_field_name; vars["js_field_type"] = AsObjectFieldType(field, file); - if ((field->type() != FieldDescriptor::TYPE_MESSAGE && !field->is_optional()) || + if ((field->type() != FieldDescriptor::TYPE_MESSAGE && !field->has_optional_keyword()) || field->is_repeated()) { printer->Print(vars, "$js_field_name$: $js_field_type$,\n"); } else { From a0348a2cf938ecd9f306faf4a3fc44702c997d87 Mon Sep 17 00:00:00 2001 From: Matt Nathan Date: Fri, 28 Jan 2022 07:33:42 +0000 Subject: [PATCH 3/3] Improve the readability of the condition guarding hasXxx .d.ts field generation. --- javascript/net/grpc/web/generator/grpc_generator.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc index a9ac5587..498f3e09 100644 --- a/javascript/net/grpc/web/generator/grpc_generator.cc +++ b/javascript/net/grpc/web/generator/grpc_generator.cc @@ -825,8 +825,9 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc, "set$js_field_name$(value?: $js_field_type$): " "$class_name$;\n"); } - if ((field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword()) && - !field->is_repeated() && !field->is_map()) { + if (field->has_optional_keyword() || + (field->type() == FieldDescriptor::TYPE_MESSAGE && + !field->is_repeated() && !field->is_map())) { printer->Print(vars, "has$js_field_name$(): boolean;\n"); } if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword() ||