diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index dbd844b81..8d7839869 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -610,19 +610,22 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re case valueKind == reflect.String && typeKind == reflect.Map: return u.fillMapFromString(value, mapValue) case valueKind == reflect.String && typeKind == reflect.Slice: - // try to find out if it's a byte slice, golang use []byte Marshal to base64 but there only SliceOf uint8/bytes can convert to []byte + // try to find out if it's a byte slice, // more details https://pkg.go.dev/encoding/json#Marshal - //> Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value. - //and also u can find this https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string + // array and slice values encode as JSON arrays, + // except that []byte encodes as a base64-encoded string, + // and a nil slice encoded as the null JSON value. + // https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string if fieldType.Elem().Kind() == reflect.Uint8 { - strVal := mapValue.(string) - decodedBytes, err := base64.StdEncoding.DecodeString(strVal) - // if err !=nil do next - if err == nil { - value.Set(reflect.ValueOf(decodedBytes)) - return nil + // check whether string type, because the kind of some other types can be string + if strVal, ok := mapValue.(string); ok { + if decodedBytes, err := base64.StdEncoding.DecodeString(strVal); err == nil { + value.Set(reflect.ValueOf(decodedBytes)) + return nil + } } } + return u.fillSliceFromString(fieldType, value, mapValue, fullName) case valueKind == reflect.String && derefedFieldType == durationType: return fillDurationValue(fieldType, value, mapValue.(string)) @@ -748,15 +751,7 @@ func (u *Unmarshaler) processFieldTextUnmarshaler(fieldType reflect.Type, value return true, tval.UnmarshalText(mv) } } - //[]byte - if fieldType.Kind() == reflect.Slice && fieldType.Elem().Kind() == reflect.Uint8 { - b, err := base64.StdEncoding.DecodeString(mapValue.(string)) - if err != nil { - return false, err - } - value.SetBytes(b) - return true, nil - } + return false, nil } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index edecf9068..3ea666c1b 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/zeromicro/go-zero/core/jsonx" "reflect" "strconv" "strings" @@ -14,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/jsonx" "github.com/zeromicro/go-zero/core/stringx" ) @@ -4874,6 +4874,7 @@ func TestUnmarshalJsonReaderMultiArray(t *testing.T) { B [][]string `json:"b"` C []byte `json:"c"` } + var res testRes marshal := testRes{ A: "133", diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index 93d9c84d3..a544c2d94 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -516,6 +516,7 @@ func TestParseJsonBody(t *testing.T) { assert.Equal(t, "apple", v[0].Name) assert.Equal(t, 18, v[0].Age) }) + t.Run("bytes field", func(t *testing.T) { type v struct { Signature []byte `json:"signature,optional"`