Skip to content

Commit

Permalink
fix: give more priority to longer routes (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreemaan-abhishek authored Jan 19, 2024
1 parent 8ebc675 commit 047275a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ local mt = { __index = _M, __gc = gc_free }


local function sort_route(route_a, route_b)
if route_a.priority == route_b.priority then
return #route_a.path_org > #route_b.path_org
end
return (route_a.priority or 0) > (route_b.priority or 0)
end

Expand Down
45 changes: 45 additions & 0 deletions t/parameter.t
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,48 @@ match meta: metadata /name
matched: {"_path":"/name/:name/","name":"json"}
match meta: nil
matched: []
=== TEST 13: route matching for routes with params and common prefix should not be dependent on registration order
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/api/:version/test/api/projects/:project_id/clusters/:cluster_id/nodes/?"},
metadata = "long",
},
{
paths = {"/api/:version/test/api/projects/:project_id"},
metadata = "medium",
},
{
paths = {"/api/:version/test/*subpath"},
metadata = "short",
},
})
-- should match long
local meta = rx:match("/api/v4/test/api/projects/saas/clusters/123/nodes/")
ngx.say("match meta: ", meta)
-- should match short
local meta = rx:match("/api/v4/test/api")
ngx.say("match meta: ", meta)
-- should match medium
local meta = rx:match("/api/v4/test/api/projects/saas")
ngx.say("match meta: ", meta)
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
match meta: long
match meta: short
match meta: medium

0 comments on commit 047275a

Please sign in to comment.