mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
fix: wrong way of Unmarshal (#4397)
This commit is contained in:
parent
520d2a2075
commit
238c92aaa9
@ -2,6 +2,7 @@ package mapping
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
"encoding"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -609,6 +610,19 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
|
|||||||
case valueKind == reflect.String && typeKind == reflect.Map:
|
case valueKind == reflect.String && typeKind == reflect.Map:
|
||||||
return u.fillMapFromString(value, mapValue)
|
return u.fillMapFromString(value, mapValue)
|
||||||
case valueKind == reflect.String && typeKind == reflect.Slice:
|
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
|
||||||
|
// 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
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
|
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
|
||||||
case valueKind == reflect.String && derefedFieldType == durationType:
|
case valueKind == reflect.String && derefedFieldType == durationType:
|
||||||
return fillDurationValue(fieldType, value, mapValue.(string))
|
return fillDurationValue(fieldType, value, mapValue.(string))
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/zeromicro/go-zero/core/jsonx"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -4868,14 +4869,27 @@ func TestUnmarshal_EnvWithOptionsWrongValueString(t *testing.T) {
|
|||||||
|
|
||||||
func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
|
func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
|
||||||
t.Run("reader multi array", func(t *testing.T) {
|
t.Run("reader multi array", func(t *testing.T) {
|
||||||
var res struct {
|
type testRes struct {
|
||||||
A string `json:"a"`
|
A string `json:"a"`
|
||||||
B [][]string `json:"b"`
|
B [][]string `json:"b"`
|
||||||
|
C []byte `json:"c"`
|
||||||
}
|
}
|
||||||
payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
|
var res testRes
|
||||||
|
marshal := testRes{
|
||||||
|
A: "133",
|
||||||
|
B: [][]string{
|
||||||
|
{"add", "cccd"},
|
||||||
|
{"eeee"},
|
||||||
|
},
|
||||||
|
C: []byte("11122344wsss"),
|
||||||
|
}
|
||||||
|
bytes, err := jsonx.Marshal(marshal)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
payload := string(bytes)
|
||||||
reader := strings.NewReader(payload)
|
reader := strings.NewReader(payload)
|
||||||
if assert.NoError(t, UnmarshalJsonReader(reader, &res)) {
|
if assert.NoError(t, UnmarshalJsonReader(reader, &res)) {
|
||||||
assert.Equal(t, 2, len(res.B))
|
assert.Equal(t, 2, len(res.B))
|
||||||
|
assert.Equal(t, string(marshal.C), string(res.C))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user