diff --git a/pkg/route/tree.go b/pkg/route/tree.go index 64f3affc6..8b63c147c 100644 --- a/pkg/route/tree.go +++ b/pkg/route/tree.go @@ -303,9 +303,7 @@ func (r *router) insert(path string, h app.HandlersChain, t kind, ppath string, if h != nil { currentNode.handlers = h currentNode.ppath = ppath - if len(currentNode.pnames) == 0 { - currentNode.pnames = pnames - } + currentNode.pnames = pnames } } return diff --git a/pkg/route/tree_test.go b/pkg/route/tree_test.go index 946bfd626..084400549 100644 --- a/pkg/route/tree_test.go +++ b/pkg/route/tree_test.go @@ -706,3 +706,32 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) { } } } + +func TestTreeParamNotOptimze(t *testing.T) { + tree := &router{method: "GET", root: &node{}, hasTsrHandler: make(map[string]bool)} + routes := [...]string{ + "/:parama/start", + "/:paramb", + } + for _, route := range routes { + tree.addRoute(route, fakeHandler(route)) + } + checkRequests(t, tree, testRequests{ + {"/1", false, "/:paramb", param.Params{param.Param{Key: "paramb", Value: "1"}}}, + {"/1/start", false, "/:parama/start", param.Params{param.Param{Key: "parama", Value: "1"}}}, + }) + + // other sequence + tree = &router{method: "GET", root: &node{}, hasTsrHandler: make(map[string]bool)} + routes = [...]string{ + "/:paramb", + "/:parama/start", + } + for _, route := range routes { + tree.addRoute(route, fakeHandler(route)) + } + checkRequests(t, tree, testRequests{ + {"/1/start", false, "/:parama/start", param.Params{param.Param{Key: "parama", Value: "1"}}}, + {"/1", false, "/:paramb", param.Params{param.Param{Key: "paramb", Value: "1"}}}, + }) +}