1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

case sensitive matching prioritized

This commit is contained in:
Brandon Hansen 2018-05-30 15:09:39 -07:00 committed by Vishal Rana
parent 01cfe83efe
commit 6007218835
2 changed files with 26 additions and 12 deletions

25
bind.go
View File

@ -104,18 +104,19 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
}
}
var inputValue []string
var exists bool
// 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 case-insensitive search.
inputFieldName = strings.ToLower(inputFieldName)
for k, v := range data {
if strings.ToLower(k) == inputFieldName {
inputValue = v
exists = true
break
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
// fix this we must check all of the map values in a
// case-insensitive search.
inputFieldName = strings.ToLower(inputFieldName)
for k, v := range data {
if strings.ToLower(k) == inputFieldName {
inputValue = v
exists = true
break
}
}
}

View File

@ -165,6 +165,19 @@ func TestBindQueryParamsCaseInsensitive(t *testing.T) {
}
}
func TestBindQueryParamsCaseSensitivePrioritized(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?id=1&ID=2&NAME=Jon+Snow&name=Jon+Doe", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, 1, u.ID)
assert.Equal(t, "Jon Doe", u.Name)
}
}
func TestBindUnmarshalParam(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil)