Skip to content

Commit

Permalink
fix(pdk) request.get_uri_captures now returns unnamed as an array
Browse files Browse the repository at this point in the history
the 'unnamed' entry was a regular Lua table, by adding the array_mt
it can be exported as an array even if empty
  • Loading branch information
Tieske committed Feb 28, 2023
1 parent 22d9bdf commit 6e5a4f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
- Fix an issue where validation to regex routes may be skipped when the old-fashioned config is used for DB-less Kong.
[#10348](https://github.com/Kong/kong/pull/10348)

#### PDK

- `request.get_uri_captures` now returns the unnamed part tagged as an array (for jsonification).
[#10390](https://github.com/Kong/kong/pull/10390)

#### Plugins

- **Request-Termination**: If the echo option was used, it would not return the uri-captures.
[#10390](https://github.com/Kong/kong/pull/10390)

## 3.2.0

### Breaking Changes
Expand Down
8 changes: 4 additions & 4 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -850,17 +850,17 @@ local function new(self)
local typ = type(k)
if typ == "number" then
unnamed_captures[k] = v

elseif typ == "string" then
named_captures[k] = v

else
kong.log.err("unknown capture key type: ", typ)
end
end

return {
unnamed = unnamed_captures,
unnamed = setmetatable(unnamed_captures, cjson.array_mt),
named = named_captures,
}
end
Expand Down
5 changes: 4 additions & 1 deletion t/01-pdk/04-request/21-get_uri_captures.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ __DATA__
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local m = ngx.re.match(ngx.var.uri, [[^\/t((\/\d+)(?P<tag>\/\w+))?]], "jo")
ngx.ctx.router_matches = {
Expand All @@ -28,6 +28,9 @@ __DATA__
ngx.say("uri_captures: ", "tag: ", captures.named["tag"],
", 0: ", captures.unnamed[0], ", 1: ", captures.unnamed[1],
", 2: ", captures.unnamed[2], ", 3: ", captures.unnamed[3])
array_mt = assert(require("cjson").array_mt, "expected array_mt to be truthy")
assert(getmetatable(captures.unnamed) == array_mt, "expected the 'unnamed' captures to be an array")
}
}
--- request
Expand Down

0 comments on commit 6e5a4f3

Please sign in to comment.