diff --git a/internal/impl/message_reflect.go b/internal/impl/message_reflect.go index ecb4623d..98ab94ae 100644 --- a/internal/impl/message_reflect.go +++ b/internal/impl/message_reflect.go @@ -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) diff --git a/testing/prototest/message.go b/testing/prototest/message.go index ebe1b871..eaf53cfe 100644 --- a/testing/prototest/message.go +++ b/testing/prototest/message.go @@ -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)