Skip to content
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

fix: check array values #2738

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
Loading