From 7f1ce2aff34aea6e67bf5bc1b1607f5d54208f86 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 11 Oct 2023 21:33:51 +0800 Subject: [PATCH] fix issue #2904 (#3004) --- net/ghttp/ghttp_server_service_handler.go | 24 +++++++++++++++++++++++ net/ghttp/ghttp_z_unit_issue_test.go | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/net/ghttp/ghttp_server_service_handler.go b/net/ghttp/ghttp_server_service_handler.go index cb69744cb..f943fb0a3 100644 --- a/net/ghttp/ghttp_server_service_handler.go +++ b/net/ghttp/ghttp_server_service_handler.go @@ -197,6 +197,30 @@ func (s *Server) checkAndCreateFuncInfo( return } + if reflectType.In(1).Kind() != reflect.Ptr || + (reflectType.In(1).Kind() == reflect.Ptr && reflectType.In(1).Elem().Kind() != reflect.Struct) { + err = gerror.NewCodef( + gcode.CodeInvalidParameter, + `invalid handler: defined as "%s", but the second input parameter should be type of pointer to struct like "*BizReq"`, + reflectType.String(), + ) + return + } + + // Do not enable this logic, as many users are already using none struct pointer type + // as the first output parameter. + /* + if reflectType.Out(0).Kind() != reflect.Ptr || + (reflectType.Out(0).Kind() == reflect.Ptr && reflectType.Out(0).Elem().Kind() != reflect.Struct) { + err = gerror.NewCodef( + gcode.CodeInvalidParameter, + `invalid handler: defined as "%s", but the first output parameter should be type of pointer to struct like "*BizRes"`, + reflectType.String(), + ) + return + } + */ + // The request struct should be named as `xxxReq`. reqStructName := trimGeneric(reflectType.In(1).String()) if !gstr.HasSuffix(reqStructName, `Req`) { diff --git a/net/ghttp/ghttp_z_unit_issue_test.go b/net/ghttp/ghttp_z_unit_issue_test.go index ff79ab07e..02951453e 100644 --- a/net/ghttp/ghttp_z_unit_issue_test.go +++ b/net/ghttp/ghttp_z_unit_issue_test.go @@ -391,7 +391,6 @@ func (c *Issue2890Controller) Post(ctx context.Context, req *Issue2890Req) (res // https://github.com/gogf/gf/issues/2890 func Test_Issue2890(t *testing.T) { gtest.C(t, func(t *gtest.T) { - oldEnumsJson, err := gtag.GetGlobalEnums() t.AssertNil(err) defer t.AssertNil(gtag.SetGlobalEnums(oldEnumsJson)) @@ -417,6 +416,7 @@ func Test_Issue2890(t *testing.T) { ) t.Assert( c.PostContent(ctx, "/api/v2/issue2890", `{"Enums":"c"}`), - "{\"code\":51,\"message\":\"The Enums value `c` should be in enums of: [\\\"a\\\",\\\"b\\\"]\",\"data\":null}") + "{\"code\":51,\"message\":\"The Enums value `c` should be in enums of: [\\\"a\\\",\\\"b\\\"]\",\"data\":null}", + ) }) }