Skip to content
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

handle nil body for JSON binding #1638

Merged
merged 2 commits into from Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions binding/binding_test.go
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
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