Skip to content

Commit

Permalink
internal/impl: revert IsSynthetic() check to fix panic
Browse files Browse the repository at this point in the history
This reverts change https://go.dev/cl/632735, in which
I misunderstood what the Protobuf documentation wanted to convey:
The quoted docs in CL 632735 refer to reflection for proto3 optional
fields, not to reflection for proto3 synthetic oneofs.

Synthetic oneofs should remain visible through reflection.
For the Open API, this change restores the old behavior.
For the Opaque API, another fix is needed and will be sent
in a separate, follow-up CL (follow golang/protobuf#1659).

For golang/protobuf#1659

Thanks to Joshua Humphries for the report and reproducer!

Change-Id: I3a924eed6d2425581adc65651395a68fc1576f4d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/638495
Reviewed-by: Chressie Himpel <chressie@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
stapelberg committed Dec 23, 2024
1 parent ce4fa19 commit 575aebf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 1 addition & 3 deletions internal/impl/message_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
mi.oneofs = map[protoreflect.Name]*oneofInfo{}
for i := 0; i < md.Oneofs().Len(); i++ {
od := md.Oneofs().Get(i)
if !od.IsSynthetic() {
mi.oneofs[od.Name()] = makeOneofInfo(od, si, mi.Exporter)
}
mi.oneofs[od.Name()] = makeOneofInfo(od, si, mi.Exporter)
}

mi.denseFields = make([]*fieldInfo, fds.Len()*2)
Expand Down
22 changes: 22 additions & 0 deletions proto/oneof_which_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package proto_test
import (
"testing"

test3openpb "google.golang.org/protobuf/internal/testprotos/test3"
testhybridpb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid"
testopaquepb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -187,3 +188,24 @@ func TestOpaqueWhich(t *testing.T) {
}
}
}

func TestSyntheticOneof(t *testing.T) {
msg := test3openpb.TestAllTypes{}
md := msg.ProtoReflect().Descriptor()
ood := md.Oneofs().ByName("_optional_int32")
if ood == nil {
t.Fatal("failed to find oneof _optional_int32")
}
if !ood.IsSynthetic() {
t.Fatal("oneof _optional_int32 should be synthetic")
}
if msg.ProtoReflect().WhichOneof(ood) != nil {
t.Error("oneof _optional_int32 should not have a field set yet")
}
msg.OptionalInt32 = proto.Int32(123)
if msg.ProtoReflect().WhichOneof(ood) == nil {
t.Error("oneof _optional_int32 should have a field set")
}
}

// TODO(stapelberg): add test variants for the Hybrid API and Opaque API.
9 changes: 2 additions & 7 deletions testing/prototest/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,8 @@ func testOneof(t testing.TB, m protoreflect.Message, od protoreflect.OneofDescri
// Set fields explicitly.
m.Set(fda, newValue(m, fda, 1, nil))
}
if !od.IsSynthetic() {
// Synthetic oneofs are used to represent optional fields in
// proto3. While they show up in protoreflect, WhichOneof does
// not work on these (only on non-synthetic, explicit oneofs).
if got, want := m.WhichOneof(od), fda; got != want {
t.Errorf("after setting oneof field %q:\nWhichOneof(%q) = %v, want %v", fda.FullName(), fda.Name(), got, want)
}
if got, want := m.WhichOneof(od), fda; got != want {
t.Errorf("after setting oneof field %q:\nWhichOneof(%q) = %v, want %v", fda.FullName(), fda.Name(), got, want)
}
for j := 0; j < od.Fields().Len(); j++ {
fdb := od.Fields().Get(j)
Expand Down

0 comments on commit 575aebf

Please sign in to comment.