Skip to content

Commit f2248ac

Browse files
committed
reflect/protodesc: add missing IsProto3Optional initialization
Initialization of L2 is symmetric: there is the filedesc initialization in internal/filedesc/desc_lazy.go and the protodesc initialization in reflect/protodesc/desc_init.go. The IsProto3Optional field was only initialized in filedesc/desc_lazy.go. Fixes golang/protobuf#1709 Change-Id: I0d6b6afa2d495c023b451c68f902543f2611844c Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/737480 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Lasse Folger <lassefolger@google.com>
1 parent 86e4605 commit f2248ac

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

reflect/protodesc/desc_init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescript
201201
return nil, err
202202
}
203203
x.L1.EditionFeatures = mergeEditionFeatures(parent, xd.GetOptions().GetFeatures())
204+
x.L2.IsProto3Optional = xd.GetProto3Optional()
204205
if opts := xd.GetOptions(); opts != nil {
205206
opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
206207
x.L2.Options = func() protoreflect.ProtoMessage { return opts }

reflect/protodesc/file_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,46 @@ func TestNewFile(t *testing.T) {
958958
}
959959
}
960960

961+
func TestNewFileProto3OptionalExt(t *testing.T) {
962+
fdset := &descriptorpb.FileDescriptorSet{
963+
File: []*descriptorpb.FileDescriptorProto{
964+
mustParseFile(`
965+
name: "google/protobuf/descriptor.proto"
966+
package: "google.protobuf"
967+
message_type: [{
968+
name: "EnumValueOptions"
969+
extension_range: [{start:1 end:536870912}]
970+
}]
971+
`),
972+
mustParseFile(`
973+
syntax: "proto3"
974+
name: "test.proto"
975+
package: "test"
976+
dependency: "google/protobuf/descriptor.proto"
977+
extension: [{
978+
name: "optional_field"
979+
number: 100000
980+
label: LABEL_OPTIONAL
981+
type: TYPE_STRING
982+
extendee: ".google.protobuf.EnumValueOptions"
983+
proto3_optional: true
984+
}]
985+
`),
986+
},
987+
}
988+
f, err := NewFiles(fdset)
989+
if err != nil {
990+
t.Fatalf("NewFile(test.proto): %v", err)
991+
}
992+
ext, err := f.FindDescriptorByName("test.optional_field")
993+
if err != nil {
994+
t.Fatalf(`f.FindDescriptorByName("test.optional_field") = %v`, err)
995+
}
996+
if !ext.(protoreflect.ExtensionDescriptor).HasOptionalKeyword() {
997+
t.Errorf("HasOptionalKeyword() = false, want true for proto3 optional extension field")
998+
}
999+
}
1000+
9611001
func TestNewFiles(t *testing.T) {
9621002
fdset := &descriptorpb.FileDescriptorSet{
9631003
File: []*descriptorpb.FileDescriptorProto{

0 commit comments

Comments
 (0)