diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 56e58839278d..a633453a1990 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -496,17 +496,20 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map return nil } -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{} switch v := mapValue.(type) { - case json.Number: + case fmt.Stringer: if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil { return err } - default: - if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil { + case string: + if err := jsonx.UnmarshalFromString(v, &slice); err != nil { return err } + default: + return errUnsupportedType } baseFieldType := Deref(fieldType.Elem()) diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index f3b037f0ea9f..da0424901ffe 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -2777,6 +2777,36 @@ func TestUnmarshalJsonReaderComplex(t *testing.T) { assert.Equal(t, "txt", req.Txt) } +func TestUnmarshalJsonReaderArrayBool(t *testing.T) { + payload := `{"id": false}` + var res struct { + ID []string `json:"id"` + } + reader := strings.NewReader(payload) + err := UnmarshalJsonReader(reader, &res) + assert.NotNil(t, err) +} + +func TestUnmarshalJsonReaderArrayInt(t *testing.T) { + payload := `{"id": 123}` + var res struct { + ID []string `json:"id"` + } + reader := strings.NewReader(payload) + err := UnmarshalJsonReader(reader, &res) + assert.NotNil(t, err) +} + +func TestUnmarshalJsonReaderArrayString(t *testing.T) { + payload := `{"id": "123"}` + var res struct { + ID []string `json:"id"` + } + reader := strings.NewReader(payload) + err := UnmarshalJsonReader(reader, &res) + assert.NotNil(t, err) +} + func BenchmarkDefaultValue(b *testing.B) { for i := 0; i < b.N; i++ { var a struct { @@ -2789,14 +2819,3 @@ 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)) -}