mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-22 19:03:38 +08:00
Merge 82a652585856fcb73b299f3ae3f6f48bc1375bd1 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
027ce6c725
32
tree.go
32
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:
|
||||
|
||||
43
tree_test.go
43
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user