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

optimize: optimize the error message of the required constraint for binding #1119

Merged
merged 2 commits into from
May 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,14 @@ func TestBind_RequiredBind(t *testing.T) {
A int `query:"a,required"`
}
req := newMockRequest().
SetRequestURI("http://foobar.com")
err := DefaultBinder().Bind(req.Req, &s, nil)
assert.DeepEqual(t, "'a' field is a 'required' parameter, but the request does not have this parameter", err.Error())

req = newMockRequest().
SetRequestURI("http://foobar.com").
SetHeader("A", "1")

err := DefaultBinder().Bind(req.Req, &s, nil)
err = DefaultBinder().Bind(req.Req, &s, nil)
if err == nil {
t.Fatal("expected error")
}
Expand Down Expand Up @@ -904,6 +908,7 @@ func TestBind_JSONRequiredField(t *testing.T) {
if err == nil {
t.Errorf("expected an error, but get nil")
}
assert.DeepEqual(t, "'c' field is a 'required' parameter, but the request body does not have this parameter 'n.n2.c'", err.Error())
assert.DeepEqual(t, 1, result.N.A)
assert.DeepEqual(t, 2, result.N.B)
assert.DeepEqual(t, 0, result.N.N2.C)
Expand Down Expand Up @@ -1492,6 +1497,7 @@ func Test_ValidatorErrorFactory(t *testing.T) {
if err == nil {
t.Fatalf("unexpected nil, expected an error")
}
assert.DeepEqual(t, "'a' field is a 'required' parameter, but the request does not have this parameter", err.Error())

type TestValidate struct {
B int `query:"b" vd:"$>100"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (d *baseTypeFieldTextDecoder) Decode(req *protocol.Request, params param.Pa
if found {
err = nil
} else {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request body does not have this parameter '%s'", d.fieldName, tagInfo.JSONName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request body does not have this parameter '%s'", tagInfo.Value, tagInfo.JSONName)
}
if len(tagInfo.Default) != 0 && keyExist(req, tagInfo) {
defaultValue = ""
Expand All @@ -90,7 +90,7 @@ func (d *baseTypeFieldTextDecoder) Decode(req *protocol.Request, params param.Pa
break
}
if tagInfo.Required {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/server/binding/internal/decoder/map_type_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (d *mapTypeFieldTextDecoder) Decode(req *protocol.Request, params param.Par
if found {
err = nil
} else {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
if len(tagInfo.Default) != 0 && keyExist(req, tagInfo) {
defaultValue = ""
Expand All @@ -82,7 +82,7 @@ func (d *mapTypeFieldTextDecoder) Decode(req *protocol.Request, params param.Par
break
}
if tagInfo.Required {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
}
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *sliceTypeFieldTextDecoder) Decode(req *protocol.Request, params param.P
if found {
err = nil
} else {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
if len(tagInfo.Default) != 0 && keyExist(req, tagInfo) { //
defaultValue = ""
Expand All @@ -88,7 +88,7 @@ func (d *sliceTypeFieldTextDecoder) Decode(req *protocol.Request, params param.P
break
}
if tagInfo.Required {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
}
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *structTypeFieldTextDecoder) Decode(req *protocol.Request, params param.
if found {
err = nil
} else {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
if len(tagInfo.Default) != 0 && keyExist(req, tagInfo) {
defaultValue = ""
Expand All @@ -59,7 +59,7 @@ func (d *structTypeFieldTextDecoder) Decode(req *protocol.Request, params param.
break
}
if tagInfo.Required {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", tagInfo.Value)
}
}
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/server/binding/tagexpr_bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestGetBody(t *testing.T) {
if err == nil {
t.Fatalf("expected an error, but get nil")
}
assert.DeepEqual(t, err.Error(), "'E' field is a 'required' parameter, but the request body does not have this parameter 'X.e'")
assert.DeepEqual(t, err.Error(), "'e' field is a 'required' parameter, but the request body does not have this parameter 'X.e'")
}

func TestQueryNum(t *testing.T) {
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestJSON(t *testing.T) {
if err == nil {
t.Error("expected an error, but get nil")
}
assert.DeepEqual(t, err.Error(), "'Y' field is a 'required' parameter, but the request body does not have this parameter 'y'")
assert.DeepEqual(t, err.Error(), "'y' field is a 'required' parameter, but the request body does not have this parameter 'y'")
assert.DeepEqual(t, []string{"a1", "a2"}, (**recv.X).A)
assert.DeepEqual(t, int32(21), (**recv.X).B)
assert.DeepEqual(t, &[]uint16{31, 32}, (**recv.X).C)
Expand Down Expand Up @@ -753,7 +753,7 @@ func TestOption(t *testing.T) {
req = newRequest("", header, nil, bodyReader)
recv = new(Recv)
err = DefaultBinder().Bind(req.Req, recv, nil)
assert.DeepEqual(t, err.Error(), "'C' field is a 'required' parameter, but the request body does not have this parameter 'X.c'")
assert.DeepEqual(t, err.Error(), "'c' field is a 'required' parameter, but the request body does not have this parameter 'X.c'")
assert.DeepEqual(t, 0, recv.X.C)
assert.DeepEqual(t, 0, recv.X.D)
assert.DeepEqual(t, "y1", recv.Y)
Expand Down
Loading