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 isue #2976, to be compatible with bad response type definition for strict route function #2977

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
10 changes: 7 additions & 3 deletions net/ghttp/ghttp_server_service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,15 @@ func (s *Server) checkAndCreateFuncInfo(f interface{}, pkgPath, structName, meth
// trimGeneric removes type definitions string from response type name if generic
func trimGeneric(structName string) string {
var (
leftBraceIndex = strings.LastIndex(structName, "[")
leftBraceIndex = strings.LastIndex(structName, "[") // for generic, it is faster to start at the end than at the beginning
rightBraceIndex = strings.LastIndex(structName, "]")
)
if leftBraceIndex == -1 && rightBraceIndex == -1 {
// no generic type usage.
if leftBraceIndex == -1 || rightBraceIndex == -1 {
// not found '[' or ']'
return structName
} else if leftBraceIndex+1 == rightBraceIndex {
// may be a slice, because generic is '[X]', not '[]'
// to be compatible with bad return parameter type: []XxxRes
return structName
}
return structName[:leftBraceIndex]
Expand Down
28 changes: 28 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_router_strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,29 @@ 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,
Expand All @@ -339,13 +356,24 @@ func Test_Router_Handler_Strict_WithGeneric(t *testing.T) {
s.Start()
defer s.Shutdown()

s.BindHandler("/test3_slice", func(ctx context.Context, req *TestReq) (res []TestGenericRes[int], err error) {
return []TestGenericRes[int]{
TestGenericRes[int]{
Test: req.Age,
},
}, nil
})

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
client := g.Client()
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}]}`)
})
}