diff --git a/mapstructure.go b/mapstructure.go index 6b81b006..d6b01f74 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -1088,7 +1088,7 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) } // If the input value is nil, then don't allocate since empty != nil - if dataVal.IsNil() { + if dataValKind != reflect.Array && dataVal.IsNil() { return nil } diff --git a/mapstructure_test.go b/mapstructure_test.go index a78e77a9..e4ba4286 100644 --- a/mapstructure_test.go +++ b/mapstructure_test.go @@ -556,6 +556,38 @@ func TestDecode_EmbeddedArray(t *testing.T) { } } +func TestDecode_decodeSliceWithArray(t *testing.T) { + t.Parallel() + + data := []struct { + title string + input interface{} + result interface{} + exp interface{} + }{ + { + title: "input is array and result is slice", + input: [1]int{1}, + result: []int{}, + exp: []int{1}, + }, + } + + for _, d := range data { + d := d + t.Run(d.title, func(t *testing.T) { + t.Parallel() + if err := Decode(d.input, &d.result); err != nil { + t.Fatalf("got an err: %s", err.Error()) + } + + if !reflect.DeepEqual(d.exp, d.result) { + t.Errorf("wanted %+v, got %+v", d.exp, d.result) + } + }) + } +} + func TestDecode_EmbeddedNoSquash(t *testing.T) { t.Parallel()