diff --git a/context.go b/context.go index df001a40..80e94ef3 100644 --- a/context.go +++ b/context.go @@ -329,11 +329,7 @@ func (c *Context) BindJSON(obj interface{}) error { // BindWith binds the passed struct pointer using the specified binding engine. // See the binding package. func (c *Context) BindWith(obj interface{}, b binding.Binding) error { - if err := b.Bind(c.Request, obj); err != nil { - c.AbortWithError(400, err).SetType(ErrorTypeBind) - return err - } - return nil + return b.Bind(c.Request, obj) } // ClientIP implements a best effort algorithm to return the real client IP, it parses diff --git a/context_test.go b/context_test.go index 01ee6b83..01f8c16c 100644 --- a/context_test.go +++ b/context_test.go @@ -703,7 +703,7 @@ func TestContextBindWithJSON(t *testing.T) { } func TestContextBadAutoBind(t *testing.T) { - c, w, _ := CreateTestContext() + c, _, _ := CreateTestContext() c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}")) c.Request.Header.Add("Content-Type", MIMEJSON) var obj struct { @@ -711,14 +711,9 @@ func TestContextBadAutoBind(t *testing.T) { Bar string `json:"bar"` } - assert.False(t, c.IsAborted()) assert.Error(t, c.Bind(&obj)) - c.Writer.WriteHeaderNow() - assert.Empty(t, obj.Bar) assert.Empty(t, obj.Foo) - assert.Equal(t, w.Code, 400) - assert.True(t, c.IsAborted()) } func TestContextGolangContext(t *testing.T) { diff --git a/utils.go b/utils.go index 18064fb5..8b908265 100644 --- a/utils.go +++ b/utils.go @@ -29,6 +29,8 @@ func Bind(val interface{}) HandlerFunc { obj := reflect.New(typ).Interface() if c.Bind(obj) == nil { c.Set(BindKey, obj) + } else { + c.Abort() } } }