Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,24 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][

switch value.Kind() {
case reflect.Slice:
if len(vs) == 0 {
if vs == nil {
if !opt.isDefaultExists {
return false, nil
}

vs = []string{opt.defaultValue}
// pre-process the default value for multi if present
cfTag := field.Tag.Get("collection_format")
if cfTag == "" || cfTag == "multi" {
vs = strings.Split(opt.defaultValue, ",")
} else {
vs = []string{opt.defaultValue}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't part of the fix (nor is it in the array part below), but it just means vs only needs to get written to once instead of twice in the case of cfTag == "" | cfTag == "multi"

}
}

if len(vs) == 0 {
return true, setSlice(vs, value, field, opt)
}

if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
return ok, err
} else if ok, err = trySetCustom(vs[0], value); ok {
Expand All @@ -280,11 +285,12 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
return false, nil
}

vs = []string{opt.defaultValue}
// pre-process the default value for multi if present
cfTag := field.Tag.Get("collection_format")
if cfTag == "" || cfTag == "multi" {
vs = strings.Split(opt.defaultValue, ",")
} else {
vs = []string{opt.defaultValue}
}
}

Expand Down
19 changes: 12 additions & 7 deletions binding/form_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ func TestMappingCustomSliceOfSliceUnmarshalTextDefault(t *testing.T) {
var s struct {
FileData []customPathUnmarshalText `form:"path,default=bar/foo;bar/foo/spam,parser=encoding.TextUnmarshaler" collection_format:"csv"`
}
err := mappingByPtr(&s, formSource{"path": {}}, "form")
err := mappingByPtr(&s, formSource{"path": nil}, "form")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this was relying on an empty (but not nil!) value slice to rely on default, but an empty slice isn't the zero value. a nil slice is.

require.NoError(t, err)
assert.Equal(t, []customPathUnmarshalText{{"bar", "foo"}, {"bar", "foo", "spam"}}, s.FileData)
}
Expand Down Expand Up @@ -994,7 +994,7 @@ func TestMappingCustomArrayOfArrayUnmarshalTextDefault(t *testing.T) {
var s struct {
FileData []objectIDUnmarshalText `form:"ids,default=664a062ac74a8ad104e0e80e;664a062ac74a8ad104e0e80f,parser=encoding.TextUnmarshaler" collection_format:"csv"`
}
err := mappingByPtr(&s, formSource{"ids": {}}, "form")
err := mappingByPtr(&s, formSource{"ids": nil}, "form")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this was relying on an empty (but not nil!) value slice to rely on default, but an empty slice isn't the zero value. a nil slice is.

require.NoError(t, err)
assert.Equal(t, []objectIDUnmarshalText{id1, id2}, s.FileData)
}
Expand Down Expand Up @@ -1079,7 +1079,7 @@ func TestMappingEmptyValues(t *testing.T) {
// field present but empty
err = mappingByPtr(&s, formSource{"slice": {}}, "form")
require.NoError(t, err)
assert.Equal(t, []int{5}, s.Slice)
assert.Equal(t, []int{}, s.Slice)

// field present with values
err = mappingByPtr(&s, formSource{"slice": {"1", "2", "3"}}, "form")
Expand Down Expand Up @@ -1108,10 +1108,15 @@ func TestMappingEmptyValues(t *testing.T) {
Slice []int `form:"slice"`
}

// field present but empty
err := mappingByPtr(&s, formSource{"slice": {}}, "form")
// field not present
err := mappingByPtr(&s, formSource{}, "form")
require.NoError(t, err)
assert.Equal(t, []int(nil), s.Slice)

// field present but empty
err = mappingByPtr(&s, formSource{"slice": {}}, "form")
require.NoError(t, err)
assert.Equal(t, []int{}, s.Slice)
})

t.Run("array without default", func(t *testing.T) {
Expand Down Expand Up @@ -1140,7 +1145,7 @@ func TestMappingEmptyValues(t *testing.T) {
// field present but empty
err = mappingByPtr(&s, formSource{"slice_multi": {}, "slice_csv": {}}, "form")
require.NoError(t, err)
assert.Equal(t, []int{1, 2, 3}, s.SliceMulti)
assert.Equal(t, []int{1, 2, 3}, s.SliceCsv)
assert.Equal(t, []int{}, s.SliceMulti)
assert.Equal(t, []int{}, s.SliceCsv)
})
}