@@ -452,9 +452,14 @@ bool IgnoreField(const FieldDescriptor* field) {
452452// Do we ignore this message type?
453453bool IgnoreMessage (const Descriptor* d) { return d->options ().map_entry (); }
454454
455+ bool IsSyntheticOneof (const OneofDescriptor* oneof) {
456+ return oneof->field_count () == 1 &&
457+ oneof->field (0 )->real_containing_oneof () == nullptr ;
458+ }
459+
455460// Does JSPB ignore this entire oneof? True only if all fields are ignored.
456461bool IgnoreOneof (const OneofDescriptor* oneof) {
457- if (OneofDescriptorLegacy (oneof). is_synthetic ()) return true ;
462+ if (IsSyntheticOneof (oneof)) { return true ; }
458463 for (int i = 0 ; i < oneof->field_count (); i++) {
459464 if (!IgnoreField (oneof->field (i))) {
460465 return false ;
@@ -563,7 +568,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
563568 int index = -1 ;
564569 for (int i = 0 ; i < oneof->containing_type ()->oneof_decl_count (); i++) {
565570 const OneofDescriptor* o = oneof->containing_type ()->oneof_decl (i);
566- if (OneofDescriptorLegacy (o). is_synthetic ()) continue ;
571+ if (IsSyntheticOneof (o)) { continue ; }
567572 // If at least one field in this oneof is not JSPB-ignored, count the oneof.
568573 for (int j = 0 ; j < o->field_count (); j++) {
569574 const FieldDescriptor* f = o->field (j);
@@ -783,8 +788,7 @@ std::string DoubleToString(double value) {
783788}
784789
785790bool InRealOneof (const FieldDescriptor* field) {
786- return field->containing_oneof () &&
787- !OneofDescriptorLegacy (field->containing_oneof ()).is_synthetic ();
791+ return field->real_containing_oneof () != nullptr ;
788792}
789793
790794// Return true if this is an integral field that should be represented as string
@@ -984,7 +988,7 @@ bool DeclaredReturnTypeIsNullable(const GeneratorOptions& options,
984988 return false ;
985989 }
986990
987- if (FileDescriptorLegacy ( field->file ()). syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
991+ if (! field->has_presence () && !field-> is_repeated () &&
988992 field->cpp_type () != FieldDescriptor::CPPTYPE_MESSAGE) {
989993 return false ;
990994 }
@@ -2345,17 +2349,13 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
23452349 printer->Print (" msg.get$getter$()" , " getter" ,
23462350 JSGetterName (options, field, BYTES_B64));
23472351 } else {
2348- bool use_default = field->has_default_value ();
2349-
2350- if (FileDescriptorLegacy (field->file ()).syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
2351- // Repeated fields get initialized to their default in the constructor
2352- // (why?), so we emit a plain getField() call for them.
2353- !field->is_repeated ()) {
2354- // Proto3 puts all defaults (including implicit defaults) in toObject().
2355- // But for proto2 we leave the existing semantics unchanged: unset fields
2356- // without default are unset.
2357- use_default = true ;
2358- }
2352+ // We rely on the default field value if it is explicit in the .proto file
2353+ // or if the field in question doesn't have presence semantics (consider
2354+ // proto3 fields without optional, repeated fields)
2355+ // Repeated fields get initialized to their default in the constructor
2356+ // (why?), so we emit a plain getField() call for them.
2357+ const bool use_default = !field->is_repeated () &&
2358+ (field->has_default_value () || !field->has_presence ());
23592359
23602360 // We don't implement this by calling the accessors, because the semantics
23612361 // of the accessors are changing independently of the toObject() semantics.
@@ -2755,9 +2755,7 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
27552755 /* force_present = */ false ,
27562756 /* singular_if_not_packed = */ false ));
27572757
2758- if (FileDescriptorLegacy (field->file ()).syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
2759- !field->is_repeated () && !field->is_map () &&
2760- !HasFieldPresence (options, field)) {
2758+ if (!field->is_repeated () && !field->is_map () && !field->has_presence ()) {
27612759 // Proto3 non-repeated and non-map fields without presence use the
27622760 // setProto3*Field function.
27632761 printer->Print (
0 commit comments