From 7c9e3b0e29efcced26c3e38ec25eb2d48f56247c Mon Sep 17 00:00:00 2001 From: Yue Yang Date: Wed, 28 Apr 2021 14:56:07 +0800 Subject: [PATCH] Fix prefix conflict in exact paths Signed-off-by: Yue Yang --- tree.go | 16 +++++++++++++--- tree_test.go | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tree.go b/tree.go index a0df321f..7cda7e7f 100644 --- a/tree.go +++ b/tree.go @@ -414,11 +414,21 @@ walk: // Outer loop for walking the tree childWillWalkTo := n.children[i] if strings.Split(childWillWalkTo.path, "/")[0] != strings.Split(path, "/")[0] && strings.HasPrefix(n.children[len(n.children)-1].path, ":") { + for _, child := range childWillWalkTo.children { + cPath := string(c) + child.path + + if cPath == path { + child.path = cPath + n = child + continue walk + } + } + continue - } else { - n = childWillWalkTo - continue walk } + + n = childWillWalkTo + continue walk } } diff --git a/tree_test.go b/tree_test.go index 2f372bc0..77340bd3 100644 --- a/tree_test.go +++ b/tree_test.go @@ -143,6 +143,7 @@ func TestTreeWildcard(t *testing.T) { "/search/", "/search/:query", "/search/gin-gonic", + "/search/google", "/user_:name", "/user_:name/about", "/files/:dir/*filepath", @@ -171,6 +172,8 @@ func TestTreeWildcard(t *testing.T) { {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}}, {"/search/someth!ng+in+ünìcodé/", true, "", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}}, {"/search/gin", false, "/search/:query", Params{Param{"query", "gin"}}}, + {"/search/gin-gonic", false, "/search/gin-gonic", nil}, + {"/search/google", false, "/search/google", nil}, {"/user_gopher", false, "/user_:name", Params{Param{Key: "name", Value: "gopher"}}}, {"/user_gopher/about", false, "/user_:name/about", Params{Param{Key: "name", Value: "gopher"}}}, {"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{Key: "dir", Value: "js"}, Param{Key: "filepath", Value: "/inc/framework.js"}}},