Skip to content

Commit b42edd7

Browse files
committed
Closes #980
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent f8c3008 commit b42edd7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

bind.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
4242
if err = b.bindPathData(i, c); err != nil {
4343
return NewHTTPError(http.StatusBadRequest, err.Error())
4444
}
45+
if err = b.bindPathData(i, c); err != nil {
46+
return NewHTTPError(http.StatusBadRequest, err.Error())
47+
}
4548
ctype := req.Header.Get(HeaderContentType)
4649
switch {
4750
case strings.HasPrefix(ctype, MIMEApplicationJSON):
@@ -79,7 +82,10 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
7982
}
8083

8184
func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error {
82-
m := make(map[string][]string)
85+
if reflect.TypeOf(ptr).Elem().Kind() == reflect.Slice {
86+
return nil
87+
}
88+
m := make(map[string][]string, len(c.ParamNames()))
8389
for _, key := range c.ParamNames() {
8490
m[key] = []string{c.Param(key)}
8591
}

bind_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var values = map[string][]string{
118118
func TestBindJSON(t *testing.T) {
119119
testBindOkay(t, strings.NewReader(userJSON), MIMEApplicationJSON)
120120
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
121+
testBindSlice(t, strings.NewReader(userJSONArray), MIMEApplicationJSON)
121122
}
122123

123124
func TestBindXML(t *testing.T) {
@@ -349,3 +350,19 @@ func testBindError(t *testing.T, r io.Reader, ctype string) {
349350
}
350351
}
351352
}
353+
354+
func testBindSlice(t *testing.T, r io.Reader, ctype string) {
355+
e := New()
356+
req := httptest.NewRequest(POST, "/", r)
357+
rec := httptest.NewRecorder()
358+
c := e.NewContext(req, rec)
359+
req.Header.Set(HeaderContentType, ctype)
360+
us := []user{}
361+
err := c.Bind(&us)
362+
if assert.NoError(t, err) {
363+
assert.Equal(t, 1, us[0].ID)
364+
assert.Equal(t, "Jon Snow", us[0].Name)
365+
assert.Equal(t, 2, us[1].ID)
366+
assert.Equal(t, "Arya Stark", us[1].Name)
367+
}
368+
}

echo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type (
2525

2626
const (
2727
userJSON = `{"id":1,"name":"Jon Snow"}`
28+
userJSONArray = `[{"id":1,"name":"Jon Snow"},{"id":2,"name":"Arya Stark"}]`
2829
userJSONOnlyName = `{"name":"Jon Snow"}`
2930
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
3031
userForm = `id=1&name=Jon Snow`

0 commit comments

Comments
 (0)