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 #1423: Use case sensitive only for bind query params #1491

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
}

inputValue, exists := data[inputFieldName]
if !exists {
if !exists && tag == "query" {
// Go json.Unmarshal supports case insensitive binding. However the
// url params are bound case sensitive which is inconsistent. To
// fix this we must check all of the map values in a
Expand Down
26 changes: 26 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,32 @@ func TestBindParam(t *testing.T) {
}
}

func TestBindParamIgnoreCaseSensitive(t *testing.T) {
e := New()
*e.maxParam = 1

body := bytes.NewBufferString(`{ "id": 1, "name": "Jon Snow" }`)
req := httptest.NewRequest(POST, "/", body)
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
rec := httptest.NewRecorder()

ctx := e.NewContext(req, rec)
ctx.SetPath("/users/:id")
ctx.SetParamNames("id")
ctx.SetParamValues("a")

u2 := new(struct {
ID int `json:"id"`
Name string `json:"name"`
})

err := ctx.Bind(u2)
if assert.NoError(t, err) {
assert.Equal(t, 1, u2.ID)
assert.Equal(t, "Jon Snow", u2.Name)
}
}

func TestBindUnmarshalTypeError(t *testing.T) {
body := bytes.NewBufferString(`{ "id": "text" }`)
e := New()
Expand Down