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

fix:Add required check for customed type binding #1058

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,76 @@ func TestBind_CustomizedTypeDecodeForPanic(t *testing.T) {
})
}

func TestBind_RequiredCustomizedTypeDecode1(t *testing.T) {
type Foo struct {
F ***CustomizedDecode `query:"a,required"`
}

bindConfig := &BindConfig{}
err := bindConfig.RegTypeUnmarshal(reflect.TypeOf(CustomizedDecode{}), func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) {
q1 := req.URI().QueryArgs().Peek("a")
if len(q1) == 0 {
return reflect.Value{}, fmt.Errorf("can be nil")
}
val := CustomizedDecode{
A: string(q1),
}
return reflect.ValueOf(val), nil
})
if err != nil {
t.Fatal(err)
}
binder := NewDefaultBinder(bindConfig)

req := newMockRequest().
SetRequestURI("http://foobar.com?b=2")
result := Foo{}
err = binder.Bind(req.Req, &result, nil)
if err == nil {
t.Fatal("expect an required error, but get nil")
}
}

/*
* TestBind_Required_CustomizedTypeDecode2 is a test case for customized type decoder by json.
* this case tells us that never only use json tag for customized type. There are two reasons:
* 1. your customized decoder function will never be called.
* 2. your customized type must meet json unmarshaler format or you will get an unmarshal error. Since it meet json unmarshaler format, you can use json tag directly.
*/
func TestBind_RequiredCustomizedTypeDecode2(t *testing.T) {
type Foo struct {
S int `json:"s"`
F CustomizedDecode `json:"f,required"`
D string `json:"d"`
Q string `query:"q"`
}

bindConfig := &BindConfig{}
err := bindConfig.RegTypeUnmarshal(reflect.TypeOf(CustomizedDecode{}), func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) {
t.Log("CustomizedDecode")
q1 := req.URI().QueryArgs().Peek("a")
if len(q1) == 0 {
return reflect.Value{}, fmt.Errorf("can be nil")
}
val := CustomizedDecode{
A: string(q1),
}
return reflect.ValueOf(val), nil
})
if err != nil {
t.Fatal(err)
}
binder := NewDefaultBinder(bindConfig)

req := newMockRequest().
SetRequestURI("http://foobar.com?q=dummy").SetJSONContentType().SetBody([]byte(`{"f":54, "d":"d", "s":1}`))
result := Foo{}
err = binder.Bind(req.Req, &result, nil)
if err == nil {
t.Fatal("expect an unUnmarshal error, but get nil")
}
}

func TestBind_JSON(t *testing.T) {
type Req struct {
J1 string `json:"j1"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package decoder

import (
"fmt"
"reflect"

"github.com/cloudwego/hertz/pkg/protocol"
Expand All @@ -55,6 +56,7 @@ type customizedFieldTextDecoder struct {
}

func (d *customizedFieldTextDecoder) Decode(req *protocol.Request, params param.Params, reqValue reflect.Value) error {
var err error
var text string
var exist bool
var defaultValue string
Expand All @@ -66,8 +68,15 @@ func (d *customizedFieldTextDecoder) Decode(req *protocol.Request, params param.
text, exist = tagInfo.Getter(req, params, tagInfo.Value)
defaultValue = tagInfo.Default
if exist {
err = nil
break
}
if tagInfo.Required {
err = fmt.Errorf("'%s' field is a 'required' parameter, but the request does not have this parameter", d.fieldName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do not return the err here directly?

}
}
if err != nil {
return err
}
if !exist {
return nil
Expand Down
2 changes: 0 additions & 2 deletions pkg/protocol/http1/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ func SetupProxy(conn network.Conn, addr string, proxyURI *protocol.URI, tlsConfi
defer close(didReadResponse)

err = reqI.Write(connectReq, conn)

if err != nil {
return
}

err = conn.Flush()

if err != nil {
return
}
Expand Down
Loading