Skip to content

Commit

Permalink
fix(binding): When there is a body parameter that is required, if the…
Browse files Browse the repository at this point in the history
…re is no body, an error is returned

Change-Id: Iacc4e379d1a1aa25b23f972d4abbea8f95a3d463
  • Loading branch information
andeya committed Aug 16, 2019
1 parent 844bc71 commit ab250cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions binding/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (b *Binding) bind(structPointer interface{}, req *http.Request, pathParams
case form, json, protobuf:
if info.paramIn == in(bodyCodec) {
found, err = param.bindOrRequireBody(info, expr, bodyCodec, bodyString, postForm)
} else if info.required {
found = false
err = info.requiredError
}
case auto:
// Try bind parameters from the body when the request has body,
Expand Down
13 changes: 13 additions & 0 deletions binding/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ func TestQueryString(t *testing.T) {
assert.Equal(t, (*string)(nil), recv.Z)
}

func TestGetBody(t *testing.T) {
type Recv struct {
X **struct {
E string `json:"e,required"`
}
}
req := newRequest("http://localhost:8080/", nil, nil, nil)
recv := new(Recv)
binder := binding.New(nil)
err := binder.BindAndValidate(recv, req, nil)
assert.Error(t, &binding.Error{ErrType: "binding", FailField: "X.e", Msg: "missing required parameter"}, err)
}

func TestQueryNum(t *testing.T) {
type Recv struct {
X **struct {
Expand Down

0 comments on commit ab250cd

Please sign in to comment.