encoding/protojson, encoding/prototext: set wire unmarshal resolver
The text and JSON encodings for the google.protobuf.Any well-known type
require a call to proto.Unmarshal. Plumb through the resolver from the
UnmarshalOptions.
Change-Id: Iccc1a9d56acd9dd214f2b289216bd50acc2ef074
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/182980
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/protojson/encode.go b/encoding/protojson/encode.go
index 45d052c..fa28897 100644
--- a/encoding/protojson/encode.go
+++ b/encoding/protojson/encode.go
@@ -39,6 +39,7 @@
// Resolver is used for looking up types when expanding google.protobuf.Any
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
Resolver interface {
+ protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}
diff --git a/encoding/protojson/well_known_types.go b/encoding/protojson/well_known_types.go
index 8f1205c..8895e5b 100644
--- a/encoding/protojson/well_known_types.go
+++ b/encoding/protojson/well_known_types.go
@@ -178,12 +178,12 @@
}
em := emt.New()
- // TODO: Need to set types registry in binary unmarshaling.
// TODO: If binary unmarshaling returns required not set error, need to
// return another required not set error that contains both the path to this
// field and the path inside the embedded message.
err = proto.UnmarshalOptions{
AllowPartial: o.AllowPartial,
+ Resolver: o.Resolver,
}.Unmarshal(valueVal.Bytes(), em.Interface())
if !nerr.Merge(err) {
return errors.New("%s: unable to unmarshal %q: %v", m.Descriptor().FullName(), typeURL, err)
diff --git a/encoding/prototext/encode.go b/encoding/prototext/encode.go
index c3a91ca..bf2d6a5 100644
--- a/encoding/prototext/encode.go
+++ b/encoding/prototext/encode.go
@@ -42,6 +42,7 @@
// Resolver is used for looking up types when expanding google.protobuf.Any
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
Resolver interface {
+ protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}
}
@@ -353,12 +354,12 @@
return text.Value{}, err
}
em := emt.New().Interface()
- // TODO: Need to set types registry in binary unmarshaling.
// TODO: If binary unmarshaling returns required not set error, need to
// return another required not set error that contains both the path to this
// field and the path inside the embedded message.
err = proto.UnmarshalOptions{
AllowPartial: o.AllowPartial,
+ Resolver: o.Resolver,
}.Unmarshal(value.Bytes(), em)
if !nerr.Merge(err) {
return text.Value{}, err