diff --git a/net/ghttp/ghttp_server_service_handler.go b/net/ghttp/ghttp_server_service_handler.go index e0e97270390..a1d56f2f80e 100644 --- a/net/ghttp/ghttp_server_service_handler.go +++ b/net/ghttp/ghttp_server_service_handler.go @@ -211,14 +211,14 @@ func (s *Server) checkAndCreateFuncInfo(f interface{}, pkgPath, structName, meth return } +// trimGeneric removes type definitions string from response type name if generic func trimGeneric(structName string) string { var ( - leftBraceIndex = strings.LastIndex(structName, "[") // for generic, it is faster to start at the end than at the beginning + leftBraceIndex = strings.LastIndex(structName, "[") rightBraceIndex = strings.LastIndex(structName, "]") ) - if leftBraceIndex == -1 || rightBraceIndex == -1 { // not found '[' or ']' - return structName - } else if leftBraceIndex+1 == rightBraceIndex { // may be a slice, because generic is '[X]', not '[]' + if leftBraceIndex == -1 && rightBraceIndex == -1 { + // no generic type usage. return structName } return structName[:leftBraceIndex] diff --git a/net/ghttp/ghttp_z_unit_feature_router_strict_test.go b/net/ghttp/ghttp_z_unit_feature_router_strict_test.go index 664e1b49dd4..ac041454b79 100644 --- a/net/ghttp/ghttp_z_unit_feature_router_strict_test.go +++ b/net/ghttp/ghttp_z_unit_feature_router_strict_test.go @@ -323,40 +323,17 @@ func Test_Router_Handler_Strict_WithGeneric(t *testing.T) { }, }, nil }) - s.BindHandler("/test1_slice", func(ctx context.Context, req *TestReq) (res []Test1Res, err error) { - return []Test1Res{ - Test1Res{ - Age: TestGeneric[int]{ - Test: req.Age, - }, - }, - }, nil - }) s.BindHandler("/test2", func(ctx context.Context, req *TestReq) (res *Test2Res, err error) { return &Test2Res{ Test: req.Age, }, nil }) - s.BindHandler("/test2_slice", func(ctx context.Context, req *TestReq) (res []Test2Res, err error) { - return []Test2Res{ - Test2Res{ - Test: req.Age, - }, - }, nil - }) s.BindHandler("/test3", func(ctx context.Context, req *TestReq) (res *TestGenericRes[int], err error) { return &TestGenericRes[int]{ Test: req.Age, }, nil }) - s.BindHandler("/test3_slice", func(ctx context.Context, req *TestReq) (res []TestGenericRes[int], err error) { - return []TestGenericRes[int]{ - TestGenericRes[int]{ - Test: req.Age, - }, - }, nil - }) s.SetDumpRouterMap(false) s.Start() @@ -368,10 +345,7 @@ func Test_Router_Handler_Strict_WithGeneric(t *testing.T) { client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) t.Assert(client.GetContent(ctx, "/test1?age=1"), `{"code":0,"message":"","data":{"Age":{"Test":1}}}`) - t.Assert(client.GetContent(ctx, "/test1_slice?age=1"), `{"code":0,"message":"","data":[{"Age":{"Test":1}}]}`) t.Assert(client.GetContent(ctx, "/test2?age=2"), `{"code":0,"message":"","data":{"Test":2}}`) - t.Assert(client.GetContent(ctx, "/test2_slice?age=2"), `{"code":0,"message":"","data":[{"Test":2}]}`) t.Assert(client.GetContent(ctx, "/test3?age=3"), `{"code":0,"message":"","data":{"Test":3}}`) - t.Assert(client.GetContent(ctx, "/test3_slice?age=3"), `{"code":0,"message":"","data":[{"Test":3}]}`) }) }