fix: panic on convert to string on fillSliceFromString() (#1951)
* Update unmarshaler.go fix: 修复fillSliceFromString()方法中mapValue 强转string后的panic 错误 * test: 增加单元测试 增加单元测试 * Update unmarshaler_test.go
This commit is contained in:
@@ -498,8 +498,15 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
|
||||
|
||||
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
|
||||
var slice []interface{}
|
||||
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
|
||||
return err
|
||||
switch v := mapValue.(type) {
|
||||
case json.Number:
|
||||
if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
baseFieldType := Deref(fieldType.Elem())
|
||||
|
||||
@@ -2789,3 +2789,14 @@ func BenchmarkDefaultValue(b *testing.B) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderArray(t *testing.T) {
|
||||
payload := "{\"id\": 123}"
|
||||
var res struct {
|
||||
ID []string `json:"id"`
|
||||
}
|
||||
reader := strings.NewReader(payload)
|
||||
err := UnmarshalJsonReader(reader, &res)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(res.ID))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user