From 82a652585856fcb73b299f3ae3f6f48bc1375bd1 Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Mon, 2 Nov 2026 11:45:11 +0800 Subject: [PATCH] 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> --- tree.go | 32 ++++++++++++++++++++++++++++++++ tree_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) 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) + }) + } +}