Skip to content

Commit

Permalink
Closes labstack#1353
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jun 27, 2019
1 parent 858270f commit 8fb7b5b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
13 changes: 8 additions & 5 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ type (
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
req := c.Request()

paramNames := c.ParamNames()
paramValues := c.ParamValues()
params := make(map[string][]string)
for i, name := range paramNames {
params[name] = []string{paramValues[i]}
names := c.ParamNames()
values := c.ParamValues()
params := map[string][]string{}
for i, name := range names {
params[name] = []string{values[i]}
}
if err := b.bindData(i, params, "param"); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
Expand Down Expand Up @@ -88,6 +88,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
}

func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
if len(data) == 0 {
return nil
}
typ := reflect.TypeOf(ptr).Elem()
val := reflect.ValueOf(ptr).Elem()

Expand Down
9 changes: 2 additions & 7 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,15 +788,10 @@ func TestContext_Bind(t *testing.T) {
e := New()
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
c := e.NewContext(req, nil)

var u *user

err := c.Bind(u)
testify.Error(t, err)
testify.Nil(t, u)
u := new(user)

req.Header.Add(HeaderContentType, MIMEApplicationJSON)
err = c.Bind(&u)
err := c.Bind(u)
testify.NoError(t, err)
testify.Equal(t, &user{1, "Jon Snow"}, u)
}
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down

0 comments on commit 8fb7b5b

Please sign in to comment.