From d27db119a0adc8a2d96c1522e84007bc15f027c0 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 24 Feb 2022 21:24:42 +0800 Subject: [PATCH] fix issue #1626 --- .../ghttp_middleware_handler_response.go | 5 +-- net/ghttp/ghttp_request.go | 6 +-- net/ghttp/ghttp_request_middleware.go | 12 +++--- ...it_feature_router_handler_extended_test.go | 40 +++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/net/ghttp/ghttp_middleware_handler_response.go b/net/ghttp/ghttp_middleware_handler_response.go index 72193f40d..5887af54e 100644 --- a/net/ghttp/ghttp_middleware_handler_response.go +++ b/net/ghttp/ghttp_middleware_handler_response.go @@ -28,12 +28,11 @@ func MiddlewareHandlerResponse(r *Request) { } var ( - err error - res interface{} ctx = r.Context() + err = r.GetError() + res = r.GetHandlerResponse() internalErr error ) - res, err = r.GetHandlerResponse() if err != nil { code := gerror.Code(err) if code == gcode.CodeNil { diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index a704d6b16..be5935f41 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -41,7 +41,7 @@ type Request struct { context context.Context // Custom context for internal usage purpose. handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request. - handlerResponse handlerResponse // Handler response object and its error value for Request/Response handler. + handlerResponse interface{} // Handler response object for Request/Response handler. hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose. hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose. parsedQuery bool // A bool marking whether the GET parameters parsed. @@ -267,6 +267,6 @@ func (r *Request) ReloadParam() { } // GetHandlerResponse retrieves and returns the handler response object and its error. -func (r *Request) GetHandlerResponse() (res interface{}, err error) { - return r.handlerResponse.Object, r.handlerResponse.Error +func (r *Request) GetHandlerResponse() interface{} { + return r.handlerResponse } diff --git a/net/ghttp/ghttp_request_middleware.go b/net/ghttp/ghttp_request_middleware.go index 3572d8d40..ddf4f7313 100644 --- a/net/ghttp/ghttp_request_middleware.go +++ b/net/ghttp/ghttp_request_middleware.go @@ -138,12 +138,12 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) { ) if funcInfo.Type.In(1).Kind() == reflect.Ptr { inputObject = reflect.New(funcInfo.Type.In(1).Elem()) - m.request.handlerResponse.Error = m.request.Parse(inputObject.Interface()) + m.request.error = m.request.Parse(inputObject.Interface()) } else { inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem() - m.request.handlerResponse.Error = m.request.Parse(inputObject.Addr().Interface()) + m.request.error = m.request.Parse(inputObject.Addr().Interface()) } - if m.request.handlerResponse.Error != nil { + if m.request.error != nil { return } inputValues = append(inputValues, inputObject) @@ -155,15 +155,15 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) { case 1: if !results[0].IsNil() { if err, ok := results[0].Interface().(error); ok { - m.request.handlerResponse.Error = err + m.request.error = err } } case 2: - m.request.handlerResponse.Object = results[0].Interface() + m.request.handlerResponse = results[0].Interface() if !results[1].IsNil() { if err, ok := results[1].Interface().(error); ok { - m.request.handlerResponse.Error = err + m.request.error = err } } } diff --git a/net/ghttp/ghttp_z_unit_feature_router_handler_extended_test.go b/net/ghttp/ghttp_z_unit_feature_router_handler_extended_test.go index 5a8aaf6a3..8f792d273 100644 --- a/net/ghttp/ghttp_z_unit_feature_router_handler_extended_test.go +++ b/net/ghttp/ghttp_z_unit_feature_router_handler_extended_test.go @@ -16,6 +16,7 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/guid" ) @@ -190,3 +191,42 @@ func Test_Router_Handler_Extended_Handler_Group_Bind(t *testing.T) { t.Assert(client.GetContent(ctx, "/api/v2/custom-test4?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`) }) } + +// https://github.com/gogf/gf/issues/1626 +func Test_Issue1626(t *testing.T) { + type TestReq struct { + Name string `v:"required"` + } + type TestRes struct { + Name string + } + s := g.Server(guid.S()) + s.Use( + ghttp.MiddlewareHandlerResponse, + func(r *ghttp.Request) { + r.Middleware.Next() + if err := r.GetError(); err != nil { + r.Response.ClearBuffer() + r.Response.Write(err.Error()) + } + }, + ) + s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) { + return &TestRes{Name: req.Name}, nil + }) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + c := g.Client() + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) + + t.Assert(c.GetContent(ctx, "/test"), `The Name field is required`) + t.Assert( + gstr.Contains(c.GetContent(ctx, "/test?name=john"), `{"Name":"john"}`), + true, + ) + }) +}