Skip to content

Commit 047275a

Browse files
fix: give more priority to longer routes (#145)
1 parent 8ebc675 commit 047275a

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/resty/radixtree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ local mt = { __index = _M, __gc = gc_free }
204204

205205

206206
local function sort_route(route_a, route_b)
207+
if route_a.priority == route_b.priority then
208+
return #route_a.path_org > #route_b.path_org
209+
end
207210
return (route_a.priority or 0) > (route_b.priority or 0)
208211
end
209212

t/parameter.t

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,48 @@ match meta: metadata /name
428428
matched: {"_path":"/name/:name/","name":"json"}
429429
match meta: nil
430430
matched: []
431+
432+
433+
434+
=== TEST 13: route matching for routes with params and common prefix should not be dependent on registration order
435+
--- config
436+
location /t {
437+
content_by_lua_block {
438+
local json = require("toolkit.json")
439+
local radix = require("resty.radixtree")
440+
local rx = radix.new({
441+
{
442+
paths = {"/api/:version/test/api/projects/:project_id/clusters/:cluster_id/nodes/?"},
443+
metadata = "long",
444+
},
445+
{
446+
paths = {"/api/:version/test/api/projects/:project_id"},
447+
metadata = "medium",
448+
},
449+
{
450+
paths = {"/api/:version/test/*subpath"},
451+
metadata = "short",
452+
},
453+
})
454+
455+
-- should match long
456+
local meta = rx:match("/api/v4/test/api/projects/saas/clusters/123/nodes/")
457+
ngx.say("match meta: ", meta)
458+
459+
-- should match short
460+
local meta = rx:match("/api/v4/test/api")
461+
ngx.say("match meta: ", meta)
462+
463+
-- should match medium
464+
local meta = rx:match("/api/v4/test/api/projects/saas")
465+
ngx.say("match meta: ", meta)
466+
}
467+
}
468+
--- request
469+
GET /t
470+
--- no_error_log
471+
[error]
472+
--- response_body
473+
match meta: long
474+
match meta: short
475+
match meta: medium

0 commit comments

Comments
 (0)