From 70c18060bde41901b2675da5629611f6afc580bc Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 14 Aug 2017 21:48:19 -0700 Subject: [PATCH] Fixed #988, Closed #989 Signed-off-by: Vishal Rana --- bind.go | 19 ------------------- bind_test.go | 34 ---------------------------------- echo_test.go | 12 +++++------- 3 files changed, 5 insertions(+), 60 deletions(-) diff --git a/bind.go b/bind.go index 72b7e661..186bd83d 100644 --- a/bind.go +++ b/bind.go @@ -39,9 +39,6 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) { } return NewHTTPError(http.StatusBadRequest, "Request body can't be empty") } - if err = b.bindPathData(i, c); err != nil { - return NewHTTPError(http.StatusBadRequest, err.Error()) - } ctype := req.Header.Get(HeaderContentType) switch { case strings.HasPrefix(ctype, MIMEApplicationJSON): @@ -78,22 +75,6 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) { return } -func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error { - if reflect.TypeOf(ptr).Elem().Kind() == reflect.Slice { - return nil - } - m := make(map[string][]string, len(c.ParamNames())) - for _, key := range c.ParamNames() { - m[key] = []string{c.Param(key)} - } - if len(m) >= 0 { - if err := b.bindData(ptr, m, "param"); err != nil { - return err - } - } - return nil -} - func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error { typ := reflect.TypeOf(ptr).Elem() val := reflect.ValueOf(ptr).Elem() diff --git a/bind_test.go b/bind_test.go index 69ccc5d0..1f1fa486 100644 --- a/bind_test.go +++ b/bind_test.go @@ -118,7 +118,6 @@ var values = map[string][]string{ func TestBindJSON(t *testing.T) { testBindOkay(t, strings.NewReader(userJSON), MIMEApplicationJSON) testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON) - testBindSlice(t, strings.NewReader(userJSONArray), MIMEApplicationJSON) } func TestBindXML(t *testing.T) { @@ -141,23 +140,6 @@ func TestBindForm(t *testing.T) { assert.Error(t, err) } -func TestBindRouteParam(t *testing.T) { - e := New() - r := strings.NewReader(userJSONOnlyName) - req := httptest.NewRequest(POST, "/", r) - req.Header.Set(HeaderContentType, MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - c.SetParamNames("id") - c.SetParamValues("5") - u := new(user) - err := c.Bind(u) - if assert.NoError(t, err) { - assert.Equal(t, 5, u.ID) - assert.Equal(t, "Jon Snow", u.Name) - } -} - func TestBindQueryParams(t *testing.T) { e := New() req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil) @@ -350,19 +332,3 @@ func testBindError(t *testing.T, r io.Reader, ctype string) { } } } - -func testBindSlice(t *testing.T, r io.Reader, ctype string) { - e := New() - req := httptest.NewRequest(POST, "/", r) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - req.Header.Set(HeaderContentType, ctype) - us := []user{} - err := c.Bind(&us) - if assert.NoError(t, err) { - assert.Equal(t, 1, us[0].ID) - assert.Equal(t, "Jon Snow", us[0].Name) - assert.Equal(t, 2, us[1].ID) - assert.Equal(t, "Arya Stark", us[1].Name) - } -} diff --git a/echo_test.go b/echo_test.go index 96e62720..a84d310d 100644 --- a/echo_test.go +++ b/echo_test.go @@ -18,18 +18,16 @@ import ( type ( user struct { - ID int `json:"id" xml:"id" form:"id" query:"id" param:"id"` + ID int `json:"id" xml:"id" form:"id" query:"id"` Name string `json:"name" xml:"name" form:"name" query:"name"` } ) const ( - userJSON = `{"id":1,"name":"Jon Snow"}` - userJSONArray = `[{"id":1,"name":"Jon Snow"},{"id":2,"name":"Arya Stark"}]` - userJSONOnlyName = `{"name":"Jon Snow"}` - userXML = `1Jon Snow` - userForm = `id=1&name=Jon Snow` - invalidContent = "invalid content" + userJSON = `{"id":1,"name":"Jon Snow"}` + userXML = `1Jon Snow` + userForm = `id=1&name=Jon Snow` + invalidContent = "invalid content" ) const userJSONPretty = `{