@@ -452,9 +452,14 @@ 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
+ return oneof->field_count () == 1 &&
457
+ oneof->field (0 )->real_containing_oneof () == nullptr ;
458
+ }
459
+
455
460
// Does JSPB ignore this entire oneof? True only if all fields are ignored.
456
461
bool IgnoreOneof (const OneofDescriptor* oneof) {
457
- if (OneofDescriptorLegacy (oneof). is_synthetic ()) return true ;
462
+ if (IsSyntheticOneof (oneof)) { return true ; }
458
463
for (int i = 0 ; i < oneof->field_count (); i++) {
459
464
if (!IgnoreField (oneof->field (i))) {
460
465
return false ;
@@ -563,7 +568,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
563
568
int index = -1 ;
564
569
for (int i = 0 ; i < oneof->containing_type ()->oneof_decl_count (); i++) {
565
570
const OneofDescriptor* o = oneof->containing_type ()->oneof_decl (i);
566
- if (OneofDescriptorLegacy (o). is_synthetic ()) continue ;
571
+ if (IsSyntheticOneof (o)) { continue ; }
567
572
// If at least one field in this oneof is not JSPB-ignored, count the oneof.
568
573
for (int j = 0 ; j < o->field_count (); j++) {
569
574
const FieldDescriptor* f = o->field (j);
@@ -783,8 +788,7 @@ std::string DoubleToString(double value) {
783
788
}
784
789
785
790
bool InRealOneof (const FieldDescriptor* field) {
786
- return field->containing_oneof () &&
787
- !OneofDescriptorLegacy (field->containing_oneof ()).is_synthetic ();
791
+ return field->real_containing_oneof () != nullptr ;
788
792
}
789
793
790
794
// Return true if this is an integral field that should be represented as string
@@ -984,7 +988,7 @@ bool DeclaredReturnTypeIsNullable(const GeneratorOptions& options,
984
988
return false ;
985
989
}
986
990
987
- if (FileDescriptorLegacy ( field->file ()). syntax () == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
991
+ if (! field->has_presence () && !field-> is_repeated () &&
988
992
field->cpp_type () != FieldDescriptor::CPPTYPE_MESSAGE) {
989
993
return false ;
990
994
}
@@ -2345,17 +2349,13 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
2345
2349
printer->Print (" msg.get$getter$()" , " getter" ,
2346
2350
JSGetterName (options, field, BYTES_B64));
2347
2351
} 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 ());
2359
2359
2360
2360
// We don't implement this by calling the accessors, because the semantics
2361
2361
// of the accessors are changing independently of the toObject() semantics.
@@ -2755,9 +2755,7 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
2755
2755
/* force_present = */ false ,
2756
2756
/* singular_if_not_packed = */ false ));
2757
2757
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 ()) {
2761
2759
// Proto3 non-repeated and non-map fields without presence use the
2762
2760
// setProto3*Field function.
2763
2761
printer->Print (
0 commit comments