@@ -452,9 +452,19 @@ bool IgnoreField(const FieldDescriptor* field) {
452
452
// Do we ignore this message type?
453
453
bool IgnoreMessage (const Descriptor* d) { return d->options ().map_entry (); }
454
454
455
+ bool IsSyntheticOneof (const OneofDescriptor* oneof) {
456
+ if (oneof->field_count () == 1 &&
457
+ oneof->field (0 )->real_containing_oneof () == nullptr ) {
458
+ return true ;
459
+ }
460
+ // if (OneofDescriptorLegacy(oneof).is_synthetic()) return true;
461
+
462
+ return false ;
463
+ }
464
+
455
465
// Does JSPB ignore this entire oneof? True only if all fields are ignored.
456
466
bool IgnoreOneof (const OneofDescriptor* oneof) {
457
- if (OneofDescriptorLegacy (oneof). is_synthetic ()) return true ;
467
+ if (IsSyntheticOneof (oneof)) { return true ; }
458
468
for (int i = 0 ; i < oneof->field_count (); i++) {
459
469
if (!IgnoreField (oneof->field (i))) {
460
470
return false ;
@@ -563,7 +573,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
563
573
int index = -1 ;
564
574
for (int i = 0 ; i < oneof->containing_type ()->oneof_decl_count (); i++) {
565
575
const OneofDescriptor* o = oneof->containing_type ()->oneof_decl (i);
566
- if (OneofDescriptorLegacy (o). is_synthetic ()) continue ;
576
+ if (IsSyntheticOneof (o)) { continue ; }
567
577
// If at least one field in this oneof is not JSPB-ignored, count the oneof.
568
578
for (int j = 0 ; j < o->field_count (); j++) {
569
579
const FieldDescriptor* f = o->field (j);
@@ -783,8 +793,7 @@ std::string DoubleToString(double value) {
783
793
}
784
794
785
795
bool InRealOneof (const FieldDescriptor* field) {
786
- return field->containing_oneof () &&
787
- !OneofDescriptorLegacy (field->containing_oneof ()).is_synthetic ();
796
+ return field->real_containing_oneof () != nullptr ;
788
797
}
789
798
790
799
// Return true if this is an integral field that should be represented as string
@@ -984,7 +993,7 @@ bool DeclaredReturnTypeIsNullable(const GeneratorOptions& options,
984
993
return false ;
985
994
}
986
995
987
- if (FileDescriptorLegacy ( field->file ()). syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
996
+ if (! field->has_presence () && !field-> is_repeated () &&
988
997
field->cpp_type () != FieldDescriptor::CPPTYPE_MESSAGE) {
989
998
return false ;
990
999
}
@@ -2345,17 +2354,13 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
2345
2354
printer->Print (" msg.get$getter$()" , " getter" ,
2346
2355
JSGetterName (options, field, BYTES_B64));
2347
2356
} 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
- }
2357
+ // We rely on the default field value if it is explicit in the .proto file
2358
+ // or if the field in question doesn't have presence semantics (consider
2359
+ // proto3 fields without optional, repeated fields)
2360
+ // Repeated fields get initialized to their default in the constructor
2361
+ // (why?), so we emit a plain getField() call for them.
2362
+ const bool use_default = !field->is_repeated () &&
2363
+ (field->has_default_value () || !field->has_presence ());
2359
2364
2360
2365
// We don't implement this by calling the accessors, because the semantics
2361
2366
// of the accessors are changing independently of the toObject() semantics.
@@ -2755,9 +2760,7 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
2755
2760
/* force_present = */ false ,
2756
2761
/* singular_if_not_packed = */ false ));
2757
2762
2758
- if (FileDescriptorLegacy (field->file ()).syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
2759
- !field->is_repeated () && !field->is_map () &&
2760
- !HasFieldPresence (options, field)) {
2763
+ if (!field->is_repeated () && !field->is_map () && !field->has_presence ()) {
2761
2764
// Proto3 non-repeated and non-map fields without presence use the
2762
2765
// setProto3*Field function.
2763
2766
printer->Print (
0 commit comments