proto: check for required fields in encoding/decoding
Change-Id: I0555a92e0399782f075b1dcd248e880dd48c7d6d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170579
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/textpb/decode.go b/encoding/textpb/decode.go
index 38b512e..731ee64 100644
--- a/encoding/textpb/decode.go
+++ b/encoding/textpb/decode.go
@@ -503,7 +503,10 @@
return err
}
// Serialize the embedded message and assign the resulting bytes to the value field.
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m.Interface())
+ b, err := proto.MarshalOptions{
+ AllowPartial: o.AllowPartial,
+ Deterministic: true,
+ }.Marshal(m.Interface())
if !nerr.Merge(err) {
return err
}
diff --git a/encoding/textpb/decode_test.go b/encoding/textpb/decode_test.go
index 2f14029..7c45641 100644
--- a/encoding/textpb/decode_test.go
+++ b/encoding/textpb/decode_test.go
@@ -1405,9 +1405,10 @@
m := &pb2.PartialRequired{
OptString: scalar.String("embedded inside Any"),
}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- // TODO: Marshal may fail due to required field not set at some
- // point. Need to ignore required not set error here.
+ b, err := proto.MarshalOptions{
+ AllowPartial: true,
+ Deterministic: true,
+ }.Marshal(m)
if err != nil {
t.Fatalf("error in binary marshaling message for Any.value: %v", err)
}
diff --git a/encoding/textpb/encode.go b/encoding/textpb/encode.go
index 4fd5d7d..2ea370f 100644
--- a/encoding/textpb/encode.go
+++ b/encoding/textpb/encode.go
@@ -353,7 +353,9 @@
}
em := emt.New().Interface()
// TODO: Need to set types registry in binary unmarshaling.
- err = proto.Unmarshal(value.Bytes(), em)
+ err = proto.UnmarshalOptions{
+ AllowPartial: o.AllowPartial,
+ }.Unmarshal(value.Bytes(), em)
if !nerr.Merge(err) {
return text.Value{}, err
}
diff --git a/encoding/textpb/encode_test.go b/encoding/textpb/encode_test.go
index c929549..5b9ee38 100644
--- a/encoding/textpb/encode_test.go
+++ b/encoding/textpb/encode_test.go
@@ -1160,9 +1160,10 @@
m := &pb2.PartialRequired{
OptString: scalar.String("embedded inside Any"),
}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- // TODO: Marshal may fail due to required field not set at some
- // point. Need to ignore required not set error here.
+ b, err := proto.MarshalOptions{
+ AllowPartial: true,
+ Deterministic: true,
+ }.Marshal(m)
if err != nil {
t.Fatalf("error in binary marshaling message for Any.value: %v", err)
}