fix(tree): fall back to skipped wildcard when a param branch dead-ends

getValue records a skippedNode whenever it prefers a static child over a
wildcard sibling, and rolls back to it when the static branch fails. Two
dead ends inside the param case bypassed that rollback and returned
not-found directly:

- more path remains but the param node has no child to descend into
- the param node consumed the whole remaining path but has no handler

For the routes "/:p0/:p1" and "/bc/:p1/a", GET /bc/bc preferred the
static bc branch, dead-ended on the handler-less :p1 node, and gave up
with 404 instead of retrying the skipped /:p0 branch and matching
/:p0/:p1 with p0=bc p1=bc.

Both dead ends now roll back to the last valid skippedNode, consistent
with the existing fallback at the other dead ends of getValue.

TestTreeParamFallbackAfterStaticDeadEnd covers each dead end; each
subtest fails without the corresponding fix.

Signed-off-by: simpleqt <2015900818@qq.com>
This commit is contained in:
simpleqt 2026-11-02 11:45:11 +08:00
parent 0f09c3a9b4
commit 82a6525858
2 changed files with 75 additions and 0 deletions

32
tree.go
View File

@ -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:

View File

@ -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)
})
}
}