Skip to content

fix bind with map[string]interface{} for json post but with no params #989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error {
for _, key := range c.ParamNames() {
m[key] = []string{c.Param(key)}
}
if len(m) >= 0 {
if len(m) > 0 {
if err := b.bindData(ptr, m, "param"); err != nil {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ func TestBindRouteParam(t *testing.T) {
}
}

func TestBindMapString(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)
u := map[string]interface{}{}
err := c.Bind(&u)
if assert.NoError(t, err) {
assert.Equal(t, "Jon Snow", u["name"])
}
}

func TestBindQueryParams(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
Expand Down