diff --git a/format.go b/format.go index bec125df..8672ef0c 100644 --- a/format.go +++ b/format.go @@ -136,10 +136,25 @@ type Formatter func(proto.Message) (string, error) func NewJSONFormatter(emitDefaults bool, resolver jsonpb.AnyResolver) Formatter { marshaler := jsonpb.Marshaler{ EmitDefaults: emitDefaults, - Indent: " ", AnyResolver: resolver, } - return marshaler.MarshalToString + // Workaround for indentation issue in jsonpb with Any messages. + // Bug was originally fixed in https://github.com/golang/protobuf/pull/834 + // but later re-introduced before the module was deprecated and frozen. + // If jsonpb is ever replaced with google.golang.org/protobuf/encoding/protojson + // this workaround will no longer be needed. + formatter := func(message proto.Message) (string, error) { + output, err := marshaler.MarshalToString(message) + if err != nil { + return "", err + } + var buf bytes.Buffer + if err := json.Indent(&buf, []byte(output), "", " "); err != nil { + return "", err + } + return buf.String(), nil + } + return formatter } // NewTextFormatter returns a formatter that returns strings in the protobuf @@ -274,11 +289,11 @@ func (r *anyResolver) Resolve(typeUrl string) (proto.Message, error) { if !ok { return nil, fmt.Errorf("unknown message: %s", typeUrl) } - // populate any extensions for this message, too - if exts, err := r.source.AllExtensionsForType(mname); err != nil { - return nil, err - } else if err := r.er.AddExtension(exts...); err != nil { - return nil, err + // populate any extensions for this message, too (if there are any) + if exts, err := r.source.AllExtensionsForType(mname); err == nil { + if err := r.er.AddExtension(exts...); err != nil { + return nil, err + } } if r.mf == nil {