internal/impl: skip synthetic oneofs in messageInfo

Calling WhichOneof should not be possible for synthetic oneofs;
on the reflection level, these fields should work as if they
were regular fields, not as if they were oneofs:

> Reflection for proto3 optional fields should work properly. For example, a
> method like Reflection::HasField() should know to look for the hasbit for a
> proto3 optional field. It should not be fooled by the synthetic oneof into
> thinking that there is a case member for the oneof.

From the Protobuf docs at:
https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md#updating-reflection

This change was tested Google-internally as CL 701866153.

Change-Id: Id9500d4aa492acf4ebc6d2d84be07ed81201d2aa
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/632735
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-11-29 13:03:45 +01:00
parent 30f628eeb3
commit bdcc7adc94
2 changed files with 10 additions and 3 deletions

View File

@ -85,7 +85,9 @@ 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)
mi.oneofs[od.Name()] = makeOneofInfo(od, si, mi.Exporter)
if !od.IsSynthetic() {
mi.oneofs[od.Name()] = makeOneofInfo(od, si, mi.Exporter)
}
}
mi.denseFields = make([]*fieldInfo, fds.Len()*2)

View File

@ -579,8 +579,13 @@ func testOneof(t testing.TB, m protoreflect.Message, od protoreflect.OneofDescri
// Set fields explicitly.
m.Set(fda, newValue(m, fda, 1, nil))
}
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 !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)
}
}
for j := 0; j < od.Fields().Len(); j++ {
fdb := od.Fields().Get(j)