From 682dfde74c313efdb41c843a889eae6b55c7f204 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 24 Jan 2025 12:27:09 +0100 Subject: [PATCH 1/2] fix: check array values --- bind.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 From 64819a9f2b8d3e58391a67b0d6a6042e1c75e43a Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 24 Jan 2025 13:18:50 +0100 Subject: [PATCH 2/2] feat: add tests --- bind_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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)