From 0ee4d2f81a6e2d1e0a47101856cf2f173df51292 Mon Sep 17 00:00:00 2001 From: janiussyafiq Date: Fri, 3 Apr 2026 15:22:33 +0800 Subject: [PATCH 1/2] fix: sort_route uses vars count as tiebreaker to match max vars --- lib/resty/radixtree.lua | 4 ++++ t/vars.t | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/resty/radixtree.lua b/lib/resty/radixtree.lua index 5be3463..0a8374b 100644 --- a/lib/resty/radixtree.lua +++ b/lib/resty/radixtree.lua @@ -205,6 +205,9 @@ local mt = { __index = _M, __gc = gc_free } local function sort_route(route_a, route_b) if route_a.priority == route_b.priority then + if #route_a.path_org == #route_b.path_org then + return (route_a.vars_len or 0) > (route_b.vars_len or 0) + end return #route_a.path_org > #route_b.path_org end return (route_a.priority or 0) > (route_b.priority or 0) @@ -438,6 +441,7 @@ local function common_route_data(path, route, route_opts, global_opts) error("failed to handle expression: " .. err, 2) end route_opts.vars = route_expr + route_opts.vars_len = #route.vars end local hosts = route.hosts diff --git a/t/vars.t b/t/vars.t index 8e5a7a8..1822a99 100644 --- a/t/vars.t +++ b/t/vars.t @@ -566,3 +566,38 @@ metadata /aa metadata /aa nil nil + + + +=== TEST 20: match max vars condition +--- config + location /t { + content_by_lua_block { + local radix = require("resty.radixtree") + local rx = radix.new({ + { + paths = {"/aa"}, + metadata = "metadata /aa", + vars = { + {"arg_k", "==", "v"}, + }, + }, + { + paths = {"/aa"}, + metadata = "metadata /aa2", + vars = { + {"arg_k", "==", "v"}, + {"arg_k2", "==", "v2"}, + }, + }, + }) + + ngx.say(rx:match("/aa", {vars = ngx.var})) + } + } +--- request +GET /t?k=v&k2=v2 +--- no_error_log +[error] +--- response_body +metadata /aa2 From 67e3d5d64eea031ced80aec89a42089fb73eb451 Mon Sep 17 00:00:00 2001 From: janiussyafiq Date: Fri, 3 Apr 2026 15:50:36 +0800 Subject: [PATCH 2/2] fix: used string equality --- lib/resty/radixtree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/radixtree.lua b/lib/resty/radixtree.lua index 0a8374b..5cf81bf 100644 --- a/lib/resty/radixtree.lua +++ b/lib/resty/radixtree.lua @@ -205,7 +205,7 @@ local mt = { __index = _M, __gc = gc_free } local function sort_route(route_a, route_b) if route_a.priority == route_b.priority then - if #route_a.path_org == #route_b.path_org then + if route_a.path_org == route_b.path_org then return (route_a.vars_len or 0) > (route_b.vars_len or 0) end return #route_a.path_org > #route_b.path_org