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: 修复标准路由结构体参数执行失败的问题 #2931

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_request_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) {
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
m.request.error = m.request.Parse(inputObject.Interface())
} else {
inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem()
inputObject = reflect.New(funcInfo.Type.In(1)).Elem()
m.request.error = m.request.Parse(inputObject.Addr().Interface())
}
if m.request.error != nil {
Expand Down
39 changes: 36 additions & 3 deletions net/ghttp/ghttp_z_unit_feature_request_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type UserRes struct {
}

var (
User = cUser{}
User = cUser{}
UserStruct = cUserStruct{}
)

type cUser struct{}
Expand All @@ -36,6 +37,13 @@ func (c *cUser) User(ctx context.Context, req *UserReq) (res *UserRes, err error
return
}

type cUserStruct struct{}

func (c *cUserStruct) User(ctx context.Context, req UserReq) (res UserRes, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

其实规范路由下,输入的Req对象应该是指针。建议之类不支持非指针的Req对象,而是在路由注册的时候增加输入参数的类型校验。

g.RequestFromCtx(ctx).Response.WriteJson(req)
return
}

func Test_Params_Tag(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
Expand All @@ -60,6 +68,30 @@ func Test_Params_Tag(t *testing.T) {
})
}

func Test_Params_StructReq(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(UserStruct)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
client := g.Client()
client.SetPrefix(prefix)
client.SetCookie("name", "john")
client.SetHeader("age", "18")

t.Assert(client.PostContent(ctx, "/user"), `{"Id":1,"Name":"john","Age":"18"}`)
t.Assert(client.PostContent(ctx, "/user", "name=&age=&id="), `{"Id":1,"Name":"john","Age":"18"}`)
})
}

func Benchmark_ParamTag(b *testing.B) {
b.StopTimer()

Expand All @@ -77,9 +109,10 @@ func Benchmark_ParamTag(b *testing.B) {
client := g.Client()
client.SetPrefix(prefix)
client.SetCookie("name", "john")
client.SetHeader("age", "18")

b.StartTimer()
for i := 0; i < b.N; i++ {
client.PostContent(ctx, "/user", "key="+strconv.Itoa(i))
for i := 1; i < b.N; i++ {
client.PostContent(ctx, "/user", "id="+strconv.Itoa(i))
}
}
5 changes: 5 additions & 0 deletions net/ghttp/ghttp_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"net/http"
"net/url"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -170,6 +171,10 @@ func Test_BuildParams(t *testing.T) {
}

func Test_ServerSignal(t *testing.T) {
if runtime.GOOS == "windows" {
t.Log("skip windows")
return
}
s := g.Server(guid.S())
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("hello world")
Expand Down
Loading