Skip to content

Commit ab250cd

Browse files
committed
fix(binding): When there is a body parameter that is required, if there is no body, an error is returned
Change-Id: Iacc4e379d1a1aa25b23f972d4abbea8f95a3d463
1 parent 844bc71 commit ab250cd

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

binding/bind.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func (b *Binding) bind(structPointer interface{}, req *http.Request, pathParams
143143
case form, json, protobuf:
144144
if info.paramIn == in(bodyCodec) {
145145
found, err = param.bindOrRequireBody(info, expr, bodyCodec, bodyString, postForm)
146+
} else if info.required {
147+
found = false
148+
err = info.requiredError
146149
}
147150
case auto:
148151
// Try bind parameters from the body when the request has body,

binding/bind_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ func TestQueryString(t *testing.T) {
7171
assert.Equal(t, (*string)(nil), recv.Z)
7272
}
7373

74+
func TestGetBody(t *testing.T) {
75+
type Recv struct {
76+
X **struct {
77+
E string `json:"e,required"`
78+
}
79+
}
80+
req := newRequest("http://localhost:8080/", nil, nil, nil)
81+
recv := new(Recv)
82+
binder := binding.New(nil)
83+
err := binder.BindAndValidate(recv, req, nil)
84+
assert.Error(t, &binding.Error{ErrType: "binding", FailField: "X.e", Msg: "missing required parameter"}, err)
85+
}
86+
7487
func TestQueryNum(t *testing.T) {
7588
type Recv struct {
7689
X **struct {

0 commit comments

Comments
 (0)