mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-20 06:39:56 +00:00
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>
212 lines
6.3 KiB
Go
212 lines
6.3 KiB
Go
// Copyright 2024 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
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"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
func TestOpenWhich(t *testing.T) {
|
|
var x *testhybridpb.TestAllTypes
|
|
if x.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField on nil returned %d, expected %d", x.WhichOneofField(), 0)
|
|
}
|
|
x = &testhybridpb.TestAllTypes{}
|
|
if x.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", x.WhichOneofField(), 0)
|
|
}
|
|
tab := []struct {
|
|
m *testhybridpb.TestAllTypes
|
|
v protoreflect.FieldNumber
|
|
}{
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofUint32: proto.Uint32(46),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofUint32_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofNestedMessage: testhybridpb.TestAllTypes_NestedMessage_builder{A: proto.Int32(46)}.Build(),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofNestedMessage_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofString: proto.String("foo"),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofString_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofBytes: []byte("foo"),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofBytes_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofBool: proto.Bool(true),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofBool_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofUint64: proto.Uint64(0),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofUint64_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofFloat: proto.Float32(0.0),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofFloat_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofDouble: proto.Float64(1.1),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofDouble_case),
|
|
},
|
|
{
|
|
m: testhybridpb.TestAllTypes_builder{
|
|
OneofEnum: testhybridpb.TestAllTypes_BAZ.Enum(),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testhybridpb.TestAllTypes_OneofEnum_case),
|
|
},
|
|
}
|
|
|
|
for _, mv := range tab {
|
|
if protoreflect.FieldNumber(mv.m.WhichOneofField()) != mv.v {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", mv.m.WhichOneofField(), mv.v)
|
|
}
|
|
if !mv.m.HasOneofField() {
|
|
t.Errorf("HasOneofField returned %t, expected true", mv.m.HasOneofField())
|
|
|
|
}
|
|
mv.m.ClearOneofField()
|
|
if mv.m.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", mv.m.WhichOneofField(), 0)
|
|
}
|
|
if mv.m.HasOneofField() {
|
|
t.Errorf("HasOneofField returned %t, expected false", mv.m.HasOneofField())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpaqueWhich(t *testing.T) {
|
|
var x *testopaquepb.TestAllTypes
|
|
if x.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField on nil returned %d, expected %d", x.WhichOneofField(), 0)
|
|
}
|
|
x = &testopaquepb.TestAllTypes{}
|
|
if x.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", x.WhichOneofField(), 0)
|
|
}
|
|
en := testopaquepb.TestAllTypes_BAZ
|
|
tab := []struct {
|
|
m *testopaquepb.TestAllTypes
|
|
v protoreflect.FieldNumber
|
|
}{
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofUint32: proto.Uint32(46),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofUint32_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofNestedMessage: testopaquepb.TestAllTypes_NestedMessage_builder{A: proto.Int32(46)}.Build(),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofNestedMessage_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofString: proto.String("foo"),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofString_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofBytes: []byte("foo"),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofBytes_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofBool: proto.Bool(true),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofBool_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofUint64: proto.Uint64(0),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofUint64_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofFloat: proto.Float32(0.0),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofFloat_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofDouble: proto.Float64(1.1),
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofDouble_case),
|
|
},
|
|
{
|
|
m: testopaquepb.TestAllTypes_builder{
|
|
OneofEnum: &en,
|
|
}.Build(),
|
|
v: protoreflect.FieldNumber(testopaquepb.TestAllTypes_OneofEnum_case),
|
|
},
|
|
}
|
|
|
|
for _, mv := range tab {
|
|
if protoreflect.FieldNumber(mv.m.WhichOneofField()) != mv.v {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", mv.m.WhichOneofField(), mv.v)
|
|
}
|
|
if !mv.m.HasOneofField() {
|
|
t.Errorf("HasOneofField returned %t, expected true", mv.m.HasOneofField())
|
|
|
|
}
|
|
mv.m.ClearOneofField()
|
|
if mv.m.WhichOneofField() != 0 {
|
|
t.Errorf("WhichOneofField returned %d, expected %d", mv.m.WhichOneofField(), 0)
|
|
}
|
|
if mv.m.HasOneofField() {
|
|
t.Errorf("HasOneofField returned %t, expected false", mv.m.HasOneofField())
|
|
}
|
|
}
|
|
}
|
|
|
|
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.
|