encoding: support weak fields in text and JSON unmarshaling
If the message for a weak field is linked in,
we treat it as if it were identical to a normal known field.
However, if the weak field is not linked in,
we treat it as if the field were not known.
Change-Id: I576d911deec98e13211304024a6353734d055465
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185457
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go
index 66c77f3..31de4d7 100644
--- a/encoding/prototext/decode_test.go
+++ b/encoding/prototext/decode_test.go
@@ -9,12 +9,15 @@
"testing"
"google.golang.org/protobuf/encoding/prototext"
+ "google.golang.org/protobuf/internal/flags"
pimpl "google.golang.org/protobuf/internal/impl"
"google.golang.org/protobuf/proto"
preg "google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/encoding/testprotos/pb2"
"google.golang.org/protobuf/encoding/testprotos/pb3"
+ testpb "google.golang.org/protobuf/internal/testprotos/test"
+ weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
"google.golang.org/protobuf/types/known/anypb"
)
@@ -26,6 +29,7 @@
inputText string
wantMessage proto.Message
wantErr bool // TODO: Verify error message content.
+ skip bool
}{{
desc: "proto2 empty message",
inputMessage: &pb2.Scalars{},
@@ -1484,10 +1488,29 @@
type_url: "pb2.Nested"
`,
wantErr: true,
+ }, {
+ desc: "weak fields",
+ inputMessage: &testpb.TestWeak{},
+ inputText: `weak_message1:{a:1}`,
+ wantMessage: func() *testpb.TestWeak {
+ m := new(testpb.TestWeak)
+ m.SetWeakMessage1(&weakpb.WeakImportMessage1{A: proto.Int32(1)})
+ return m
+ }(),
+ skip: !flags.Proto1Legacy,
+ }, {
+ desc: "weak fields; unknown field",
+ inputMessage: &testpb.TestWeak{},
+ inputText: `weak_message1:{a:1} weak_message2:{a:1}`,
+ wantErr: true, // weak_message2 is unknown since the package containing it is not imported
+ skip: !flags.Proto1Legacy,
}}
for _, tt := range tests {
tt := tt
+ if tt.skip {
+ continue
+ }
t.Run(tt.desc, func(t *testing.T) {
err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage)
if err != nil && !tt.wantErr {