Skip to content

Commit 904a8fc

Browse files
committed
Fix routes overlapping by *any pattern in route's path
1 parent 0e5d4c0 commit 904a8fc

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

http/router/matching.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ local function transform_pattern(path)
5757
if string.match(match, '^.') ~= '/' then
5858
match = '/' .. match
5959
end
60-
match = '^' .. match .. '$'
60+
match = '^(' .. match .. ')$'
6161
end
6262

6363
return match, stash
@@ -70,9 +70,14 @@ local function matches(r, filter)
7070
end
7171

7272
local regex_groups_matched = {string.match(filter.path, r.match)}
73+
7374
if #regex_groups_matched == 0 then
7475
return false
7576
end
77+
78+
-- first regex_group is whole path, we should omit it
79+
table.remove(regex_groups_matched, 1)
80+
7681
if #r.stash > 0 and #r.stash ~= #regex_groups_matched then
7782
return false
7883
end
@@ -86,7 +91,6 @@ local function matches(r, filter)
8691
return true, {
8792
route = r,
8893
stash = regex_groups_matched,
89-
9094
-- the more symbols were known in advance by route,
9195
-- the more priority we give the route
9296
specificity = -symbols_didnt_know,

test/integration/router_test.lua

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ g.after_each(function()
1616
g.server:stop()
1717
end)
1818

19+
g.test_route_priority_any = function()
20+
g.router:route({ path = 'test/*any', method = 'GET' }, function() return {body = 'any'} end)
21+
t.assert_equals(http_client.get(helper.base_uri .. 'test/some').body, 'any')
22+
23+
g.router:route({ path = 'test/some', method = 'GET'}, function () return {body = 'some'} end)
24+
t.assert_equals(http_client.get(helper.base_uri .. 'test/some').body, 'some')
25+
end
26+
1927
g.test_route_priority_stash = function()
2028
g.router:route({method = 'GET', path = '*stashname'}, function(_)
2129
return {

0 commit comments

Comments
 (0)