diff --git a/bind.go b/bind.go index 5940e15da..8dad2a6f1 100644 --- a/bind.go +++ b/bind.go @@ -232,7 +232,12 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri } } - inputValue, exists := data[inputFieldName] + var inputValue []string + var exists bool + inputValue, exists = data[inputFieldName] + if !exists { + inputValue, exists = data[inputFieldName+"[]"] + } if !exists { // Go json.Unmarshal supports case-insensitive binding. However the // url params are bound case-sensitive which is inconsistent. To diff --git a/bind_test.go b/bind_test.go index 303c8854a..90adabf2c 100644 --- a/bind_test.go +++ b/bind_test.go @@ -62,6 +62,10 @@ type bindTestStruct struct { SA StringArray } +type bindQueryArrayTestStruct struct { + Foo []string `query:"foo"` +} + type bindTestStructWithTags struct { I int `json:"I" form:"I"` PtrI *int `json:"PtrI" form:"PtrI"` @@ -275,6 +279,30 @@ func TestBindQueryParamsCaseSensitivePrioritized(t *testing.T) { } } +func TestBindQueryArrayParams(t *testing.T) { + e := New() + req := httptest.NewRequest(http.MethodGet, "/?foo=one&foo=two", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + q := new(bindQueryArrayTestStruct) + err := c.Bind(q) + if assert.NoError(t, err) { + assert.Equal(t, []string{"one", "two"}, q.Foo) + } +} + +func TestBindQueryArrayParamsWithSuffix(t *testing.T) { + e := New() + req := httptest.NewRequest(http.MethodGet, "/?foo[]=one&foo[]=two", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + q := new(bindQueryArrayTestStruct) + err := c.Bind(q) + if assert.NoError(t, err) { + assert.Equal(t, []string{"one", "two"}, q.Foo) + } +} + func TestBindHeaderParam(t *testing.T) { e := New() req := httptest.NewRequest(http.MethodGet, "/", nil)