Skip to content

Commit d279b25

Browse files
bbiaoThomasObenaus
authored andcommitted
Bugfix for the FullPath feature (gin-gonic#1919)
* worked with more complex situations * the original pr not work when and a short route with the same prefix to some already added routes
1 parent 2c000c1 commit d279b25

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

routes_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,15 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
560560

561561
// Test routes
562562
routes := []string{
563-
"/",
564563
"/simple",
565564
"/project/:name",
565+
"/",
566+
"/news/home",
567+
"/news",
568+
"/simple-two/one",
569+
"/simple-two/one-two",
566570
"/project/:name/build/*params",
571+
"/project/:name/bui",
567572
}
568573

569574
for _, route := range routes {

tree.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
128128
n.priority++
129129
numParams := countParams(path)
130130

131+
parentFullPathIndex := 0
132+
131133
// non-empty tree
132134
if len(n.path) > 0 || len(n.children) > 0 {
133135
walk:
@@ -155,7 +157,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
155157
children: n.children,
156158
handlers: n.handlers,
157159
priority: n.priority - 1,
158-
fullPath: fullPath,
160+
fullPath: n.fullPath,
159161
}
160162

161163
// Update maxParams (max of all children)
@@ -171,13 +173,15 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
171173
n.path = path[:i]
172174
n.handlers = nil
173175
n.wildChild = false
176+
n.fullPath = fullPath[:parentFullPathIndex+i]
174177
}
175178

176179
// Make new node a child of this node
177180
if i < len(path) {
178181
path = path[i:]
179182

180183
if n.wildChild {
184+
parentFullPathIndex += len(n.path)
181185
n = n.children[0]
182186
n.priority++
183187

@@ -211,6 +215,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
211215

212216
// slash after param
213217
if n.nType == param && c == '/' && len(n.children) == 1 {
218+
parentFullPathIndex += len(n.path)
214219
n = n.children[0]
215220
n.priority++
216221
continue walk
@@ -219,6 +224,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
219224
// Check if a child with the next path byte exists
220225
for i := 0; i < len(n.indices); i++ {
221226
if c == n.indices[i] {
227+
parentFullPathIndex += len(n.path)
222228
i = n.incrementChildPrio(i)
223229
n = n.children[i]
224230
continue walk
@@ -369,6 +375,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
369375
// insert remaining path part and handle to the leaf
370376
n.path = path[offset:]
371377
n.handlers = handlers
378+
n.fullPath = fullPath
372379
}
373380

374381
// nodeValue holds return values of (*Node).getValue method

0 commit comments

Comments
 (0)