diff --git a/binding/json.go b/binding/json.go index e21c2ee346..ca36aabd2b 100644 --- a/binding/json.go +++ b/binding/json.go @@ -50,6 +50,9 @@ func decodeJSON(r io.Reader, obj any) error { decoder.DisallowUnknownFields() } if err := decoder.Decode(obj); err != nil { + if err == io.EOF { + return errors.New("empty body") + } return err } return validate(obj) diff --git a/binding/json_test.go b/binding/json_test.go index fbd5c52743..1d316bb23f 100644 --- a/binding/json_test.go +++ b/binding/json_test.go @@ -5,6 +5,7 @@ package binding import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -28,3 +29,11 @@ func TestJSONBindingBindBodyMap(t *testing.T) { assert.Equal(t, "FOO", s["foo"]) assert.Equal(t, "world", s["hello"]) } + +func TestJSONBindingBindEmpty(t *testing.T) { + var s struct { + Foo string `binding:"required"` + } + err := jsonBinding{}.BindBody([]byte(""), &s) + require.True(t, strings.Contains(err.Error(), "empty body")) +}