internal/impl: revert IsSynthetic() check to fix panic

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>
This commit is contained in:
Michael Stapelberg 2024-12-23 13:18:00 +01:00
parent ce4fa196c4
commit 575aebf635
3 changed files with 25 additions and 10 deletions

View File

@ -85,9 +85,7 @@ func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
mi.oneofs = map[protoreflect.Name]*oneofInfo{} mi.oneofs = map[protoreflect.Name]*oneofInfo{}
for i := 0; i < md.Oneofs().Len(); i++ { for i := 0; i < md.Oneofs().Len(); i++ {
od := md.Oneofs().Get(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) mi.denseFields = make([]*fieldInfo, fds.Len()*2)

View File

@ -7,6 +7,7 @@ package proto_test
import ( import (
"testing" "testing"
test3openpb "google.golang.org/protobuf/internal/testprotos/test3"
testhybridpb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid" testhybridpb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid"
testopaquepb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque" testopaquepb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -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.

View File

@ -583,13 +583,8 @@ func testOneof(t testing.TB, m protoreflect.Message, od protoreflect.OneofDescri
// Set fields explicitly. // Set fields explicitly.
m.Set(fda, newValue(m, fda, 1, nil)) m.Set(fda, newValue(m, fda, 1, nil))
} }
if !od.IsSynthetic() { if got, want := m.WhichOneof(od), fda; got != want {
// Synthetic oneofs are used to represent optional fields in t.Errorf("after setting oneof field %q:\nWhichOneof(%q) = %v, want %v", fda.FullName(), fda.Name(), got, want)
// 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)
}
} }
for j := 0; j < od.Fields().Len(); j++ { for j := 0; j < od.Fields().Len(); j++ {
fdb := od.Fields().Get(j) fdb := od.Fields().Get(j)