diff --git a/tree.go b/tree.go index 6842d225..ee9dd5cb 100644 --- a/tree.go +++ b/tree.go @@ -538,6 +538,22 @@ walk: // Outer loop for walking the tree // ... but we can't value.tsr = len(path) == end+1 + if !value.tsr && path != "/" { + // roll back to last valid skippedNode + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] + } + globalParamsCount = skippedNode.paramsCount + continue walk + } + } + } return value } @@ -551,6 +567,22 @@ walk: // Outer loop for walking the tree n = n.children[0] value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") } + if !value.tsr && path != "/" { + // roll back to last valid skippedNode + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] + } + globalParamsCount = skippedNode.paramsCount + continue walk + } + } + } return value case catchAll: diff --git a/tree_test.go b/tree_test.go index 23339af4..475689d9 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1111,3 +1111,46 @@ func TestTreeFindCaseInsensitivePathWildcardParamAndStaticChild(t *testing.T) { t.Errorf("Wrong result for '/prefix/something': %s", string(out)) } } + +// TestTreeParamFallbackAfterStaticDeadEnd ensures the walk falls back to a +// previously skipped wildcard node when the preferred static branch dead-ends +// inside the param case of getValue: either more path remains but the param +// node has no child to descend into, or the param node consumed the whole +// remaining path but has no handler registered. Both dead ends must retry the +// skipped wildcard instead of returning not-found. +func TestTreeParamFallbackAfterStaticDeadEnd(t *testing.T) { + tests := []struct { + name string + routes []string + requests testRequests + }{ + { + name: "param consumed the whole path but has no handler", + routes: []string{"/:p0/:p1", "/bc/:p1/a"}, + requests: testRequests{ + {"/bc/bc/a", false, "/bc/:p1/a", Params{Param{Key: "p1", Value: "bc"}}}, + {"/bc/bc", false, "/:p0/:p1", Params{Param{Key: "p0", Value: "bc"}, Param{Key: "p1", Value: "bc"}}}, + {"/x/y", false, "/:p0/:p1", Params{Param{Key: "p0", Value: "x"}, Param{Key: "p1", Value: "y"}}}, + }, + }, + { + name: "more path remains but the param node has no child", + routes: []string{"/:p0/:p1/:p2", "/bc/:p1"}, + requests: testRequests{ + {"/bc/bc", false, "/bc/:p1", Params{Param{Key: "p1", Value: "bc"}}}, + {"/bc/bc/2", false, "/:p0/:p1/:p2", Params{Param{Key: "p0", Value: "bc"}, Param{Key: "p1", Value: "bc"}, Param{Key: "p2", Value: "2"}}}, + {"/x/y/2", false, "/:p0/:p1/:p2", Params{Param{Key: "p0", Value: "x"}, Param{Key: "p1", Value: "y"}, Param{Key: "p2", Value: "2"}}}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tree := &node{} + for _, route := range tt.routes { + tree.addRoute(route, fakeHandler(route)) + } + checkRequests(t, tree, tt.requests) + }) + } +}