-
Notifications
You must be signed in to change notification settings - Fork 8.6k
fix(form): correctly differentiate between nil / present-but-empty slices #4498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
7b7f800
47a2602
cafe8d9
e7abd0b
c406e6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
|
|
@@ -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") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
|
|
@@ -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") | ||
|
|
@@ -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) { | ||
|
|
@@ -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) | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
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
vsonly needs to get written to once instead of twice in the case ofcfTag == "" | cfTag == "multi"