Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: give more priority to longer routes #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading