correctly differentiate between nil / present-but-empty slices

This commit is contained in:
kim 2026-01-12 13:35:35 +00:00
parent d7776de7d4
commit 7b7f800b2c
2 changed files with 19 additions and 8 deletions

View File

@ -250,19 +250,24 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
switch value.Kind() { switch value.Kind() {
case reflect.Slice: case reflect.Slice:
if len(vs) == 0 { if vs == nil {
if !opt.isDefaultExists { if !opt.isDefaultExists {
return false, nil return false, nil
} }
vs = []string{opt.defaultValue}
// pre-process the default value for multi if present // pre-process the default value for multi if present
cfTag := field.Tag.Get("collection_format") cfTag := field.Tag.Get("collection_format")
if cfTag == "" || cfTag == "multi" { if cfTag == "" || cfTag == "multi" {
vs = strings.Split(opt.defaultValue, ",") vs = strings.Split(opt.defaultValue, ",")
} else {
vs = []string{opt.defaultValue}
} }
} }
if len(vs) == 0 {
return true, setSlice(vs, value, field, opt)
}
if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok { if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
return ok, err return ok, err
} else if ok, err = trySetCustom(vs[0], value); ok { } else if ok, err = trySetCustom(vs[0], value); ok {
@ -280,11 +285,12 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
return false, nil return false, nil
} }
vs = []string{opt.defaultValue}
// pre-process the default value for multi if present // pre-process the default value for multi if present
cfTag := field.Tag.Get("collection_format") cfTag := field.Tag.Get("collection_format")
if cfTag == "" || cfTag == "multi" { if cfTag == "" || cfTag == "multi" {
vs = strings.Split(opt.defaultValue, ",") vs = strings.Split(opt.defaultValue, ",")
} else {
vs = []string{opt.defaultValue}
} }
} }

View File

@ -1079,7 +1079,7 @@ func TestMappingEmptyValues(t *testing.T) {
// field present but empty // field present but empty
err = mappingByPtr(&s, formSource{"slice": {}}, "form") err = mappingByPtr(&s, formSource{"slice": {}}, "form")
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []int{5}, s.Slice) assert.Equal(t, []int{}, s.Slice)
// field present with values // field present with values
err = mappingByPtr(&s, formSource{"slice": {"1", "2", "3"}}, "form") err = mappingByPtr(&s, formSource{"slice": {"1", "2", "3"}}, "form")
@ -1108,10 +1108,15 @@ func TestMappingEmptyValues(t *testing.T) {
Slice []int `form:"slice"` Slice []int `form:"slice"`
} }
// field present but empty // field not present
err := mappingByPtr(&s, formSource{"slice": {}}, "form") err := mappingByPtr(&s, formSource{}, "form")
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []int(nil), s.Slice) 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) { t.Run("array without default", func(t *testing.T) {
@ -1140,7 +1145,7 @@ func TestMappingEmptyValues(t *testing.T) {
// field present but empty // field present but empty
err = mappingByPtr(&s, formSource{"slice_multi": {}, "slice_csv": {}}, "form") err = mappingByPtr(&s, formSource{"slice_multi": {}, "slice_csv": {}}, "form")
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []int{1, 2, 3}, s.SliceMulti) assert.Equal(t, []int{}, s.SliceMulti)
assert.Equal(t, []int{1, 2, 3}, s.SliceCsv) assert.Equal(t, []int{}, s.SliceCsv)
}) })
} }