Skip to content

Commit

Permalink
handle nil body for JSON binding (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
mllu authored and thinkerou committed Nov 22, 2018
1 parent 521d06c commit 64457fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ func TestBindingDefault(t *testing.T) {
assert.Equal(t, YAML, Default("PUT", MIMEYAML))
}

func TestBindingJSONNilBody(t *testing.T) {
var obj FooStruct
req, _ := http.NewRequest(http.MethodPost, "/", nil)
err := JSON.Bind(req, &obj)
assert.Error(t, err)
}

func TestBindingJSON(t *testing.T) {
testBodyBinding(t,
JSON, "json",
Expand Down
4 changes: 4 additions & 0 deletions binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package binding

import (
"bytes"
"fmt"
"io"
"net/http"

Expand All @@ -24,6 +25,9 @@ func (jsonBinding) Name() string {
}

func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
if req == nil || req.Body == nil {
return fmt.Errorf("invalid request")
}
return decodeJSON(req.Body, obj)
}

Expand Down

0 comments on commit 64457fb

Please sign in to comment.