fix: panic on convert to string on fillSliceFromString() (#1951)

* Update unmarshaler.go

 fix: 修复fillSliceFromString()方法中mapValue 强转string后的panic 错误

* test: 增加单元测试

增加单元测试

* Update unmarshaler_test.go
master
家福 3 years ago committed by GitHub
parent 321a20add6
commit 07145b210e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 { func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
var slice []interface{} var slice []interface{}
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil { switch v := mapValue.(type) {
return err 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()) 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))
}

Loading…
Cancel
Save