From a2765e27b43e1086d90a553a959f1d363ef4ad07 Mon Sep 17 00:00:00 2001 From: Comolli <47921612+Comolli@users.noreply.github.com> Date: Thu, 19 May 2022 22:37:44 +0800 Subject: [PATCH] feat: decode google.protobuf.BytesValue compatible base64.URLEncoding. (#1991) --- encoding/form/form_test.go | 14 ++++++++++++++ encoding/form/proto_decode.go | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/encoding/form/form_test.go b/encoding/form/form_test.go index f437ddf1a..384869c52 100644 --- a/encoding/form/form_test.go +++ b/encoding/form/form_test.go @@ -1,6 +1,7 @@ package form import ( + "encoding/base64" "reflect" "testing" @@ -178,3 +179,16 @@ func TestDecodeStructPb(t *testing.T) { t.Errorf("except %v, got %v", "go-kratos", req.Data.GetFields()["name2"].GetStringValue()) } } + +func TestDecodeBytesValuePb(t *testing.T) { + url := "https://example.com/xx/?a=1&b=2&c=3" + val := base64.URLEncoding.EncodeToString([]byte(url)) + content := "bytes=" + val + in2 := &complex.Complex{} + if err := encoding.GetCodec(contentType).Unmarshal([]byte(content), in2); err != nil { + t.Errorf("expect %v, got %v", nil, err) + } + if !reflect.DeepEqual(url, string(in2.Bytes.Value)) { + t.Errorf("except %v, got %v", val, string(in2.Bytes.Value)) + } +} diff --git a/encoding/form/proto_decode.go b/encoding/form/proto_decode.go index 8b4b31590..9e31e3ba7 100644 --- a/encoding/form/proto_decode.go +++ b/encoding/form/proto_decode.go @@ -283,7 +283,9 @@ func parseMessage(md protoreflect.MessageDescriptor, value string) (protoreflect case "google.protobuf.BytesValue": v, err := base64.StdEncoding.DecodeString(value) if err != nil { - return protoreflect.Value{}, err + if v, err = base64.URLEncoding.DecodeString(value); err != nil { + return protoreflect.Value{}, err + } } msg = wrapperspb.Bytes(v) case "google.protobuf.FieldMask":