1
0
mirror of https://github.com/labstack/echo.git synced 2025-02-03 13:11:39 +02:00

Enable case insensitive query param matching

Unmarshalling body params with json.Unmarshal supports case-insensitive
matching against struct tags.  Matching query params case insensitive
provides a more sane and consistent experience for API consumers.

The original url.Values keys remain case sensitive.
This commit is contained in:
Brandon Hansen 2018-05-30 14:50:24 -07:00 committed by Vishal Rana
parent ec7b497940
commit 01cfe83efe
2 changed files with 29 additions and 1 deletions

17
bind.go
View File

@ -103,7 +103,22 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
continue
}
}
inputValue, exists := data[inputFieldName]
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
}
}
if !exists {
continue
}

View File

@ -152,6 +152,19 @@ func TestBindQueryParams(t *testing.T) {
}
}
func TestBindQueryParamsCaseInsensitive(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?ID=1&NAME=Jon+Snow", 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 Snow", 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)