Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else if isElemInterface {
// To maintain backward compatibility, we always bind to the first string value
// and not the slice of strings when dealing with map[string]interface{}{}
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
if len(v) == 1 {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
} else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
Expand Down
22 changes: 20 additions & 2 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param", nil))
assert.Equal(t,
map[string]interface{}{
"multiple": "1",
"multiple": []string{"1", "2"},
"single": "3",
},
dest,
Expand All @@ -510,7 +510,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param", nil))
assert.Equal(t,
map[string]interface{}{
"multiple": "1",
"multiple": []string{"1", "2"},
"single": "3",
},
dest,
Expand Down Expand Up @@ -542,6 +542,24 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
})
}

func TestBindMultipartFormToMapInterface(t *testing.T) {
bodyBuffer := new(bytes.Buffer)
mw := multipart.NewWriter(bodyBuffer)
assert.NoError(t, mw.WriteField("ima_slice", "WEBHOOK"))
assert.NoError(t, mw.WriteField("ima_slice", "OTHER"))
assert.NoError(t, mw.Close())

e := New()
req := httptest.NewRequest(http.MethodPost, "/", bodyBuffer)
req.Header.Set(HeaderContentType, mw.FormDataContentType())
c := e.NewContext(req, nil)

data := map[string]interface{}{}
err := c.Bind(&data)
assert.NoError(t, err)
assert.Equal(t, []string{"WEBHOOK", "OTHER"}, data["ima_slice"])
}

func TestBindbindData(t *testing.T) {
ts := new(bindTestStruct)
b := new(DefaultBinder)
Expand Down