Skip to content

Commit 438b9af

Browse files
authored
bugfix: failed to match route when no matched in opts. (#50)
1 parent 998251b commit 438b9af

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

lib/resty/radixtree.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,23 +429,22 @@ local function fetch_pat(path)
429429
end
430430
end
431431

432-
local pat = table.concat(res, [[\/]])
432+
pat = table.concat(res, [[\/]])
433433
lru_pat:set(path, {pat, names}, 60 * 60)
434434
return pat, names
435435
end
436436

437437
local function compare_gin(l_v, r_v, opts)
438-
if not opts.matched then
439-
return true
440-
end
441-
442438
local pat, names = fetch_pat(r_v)
443439
-- log_info("pat: ", require("cjson").encode(pat))
444440
local m = re_match(l_v, pat, "jo")
445441
if not m then
446442
return false
447443
end
448444

445+
if not opts.matched then
446+
return true
447+
end
449448

450449
for i, v in ipairs(m) do
451450
local name = names[i]

t/parameter.t

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ matched: {}
189189
local opts = {matched = {}, method = "GET", host = "aa.bar.com"}
190190
local meta = rx:match("/bb/cc/xx", opts)
191191
ngx.say("match meta: ", meta)
192-
ngx.say("matched: ", json.encode(opts.matched))
192+
ngx.say("matched: ", json.encode(opts.matched))
193193
194194
}
195195
}
@@ -203,15 +203,15 @@ matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"*.bar.com"}
203203
204204
205205
206-
=== TEST 7: /name/*name/foo
206+
=== TEST 7: /name/*name/foo (cached parameter)
207207
--- config
208208
location /t {
209209
content_by_lua_block {
210210
local json = require("cjson.safe")
211211
local radix = require("resty.radixtree")
212212
local rx = radix.new({
213213
{
214-
paths = {"/name/*name/foo"},
214+
paths = {"/name/:name/foo"},
215215
metadata = "metadata /name",
216216
},
217217
})
@@ -232,6 +232,36 @@ GET /t
232232
[error]
233233
--- response_body
234234
match meta: metadata /name
235-
matched: {"_path":"\/name\/*name\/foo","name":"json"}
235+
matched: {"_path":"\/name\/:name\/foo","name":"json"}
236236
match meta: nil
237237
matched: {}
238+
239+
240+
241+
=== TEST 8: /name/*name/foo (no cached parameter)
242+
--- config
243+
location /t {
244+
content_by_lua_block {
245+
local json = require("cjson.safe")
246+
local radix = require("resty.radixtree")
247+
local rx = radix.new({
248+
{
249+
paths = {"/name/:name/foo"},
250+
metadata = "metadata /name",
251+
},
252+
})
253+
254+
local opts = {}
255+
local meta = rx:match("/name/json/foo", opts)
256+
ngx.say("match meta: ", meta)
257+
meta = rx:match("/name/json", opts)
258+
ngx.say("match meta: ", meta)
259+
}
260+
}
261+
--- request
262+
GET /t
263+
--- no_error_log
264+
[error]
265+
--- response_body
266+
match meta: metadata /name
267+
match meta: nil

0 commit comments

Comments
 (0)