Skip to content
Open
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
4 changes: 3 additions & 1 deletion apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local pairs = pairs
local type = type
local ngx_re = require("ngx.re")
local isarray = require("table.isarray")
local str_has_prefix = require("apisix.core.string").has_prefix


local _M = {
Expand Down Expand Up @@ -130,7 +131,8 @@ do
copied[orig] = copy
for orig_key, orig_value in pairs(orig) do
local path = parent .. "." .. tostring(orig_key)
if opts and array_find(opts.shallows, path) then
if opts and (array_find(opts.shallows, path) or
(opts.shallow_prefix and str_has_prefix(parent, opts.shallow_prefix))) then
copy[orig_key] = orig_value
Comment on lines 133 to 136
else
if type(orig_value) == "table" then
Expand Down
5 changes: 4 additions & 1 deletion apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,10 @@ local function merge_consumer_route(route_conf, consumer_conf, consumer_group_co
return route_conf
end

local new_route_conf = core.table.deepcopy(route_conf)
-- the plugins subtree is fully overwritten below, so there is no need to
-- deep-copy it; shallow-copy that subtree to avoid the wasted work
local new_route_conf = core.table.deepcopy(route_conf,
{ shallow_prefix = "self.value.plugins" })
Comment on lines +821 to +824

if has_group_plugins then
for name, conf in pairs(consumer_group_conf.value.plugins) do
Expand Down
34 changes: 34 additions & 0 deletions t/core/table.t
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,37 @@ tab_copied.a.b.c == tab.a.b.c1: true
tab_copied.a.b.c == t1: true
tab_copied.x.y == tab.x.y: false
tab_copied.x.y == t2: false



=== TEST 13: shallow_prefix
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local deepcopy = core.table.deepcopy
local plugin1 = {name = "plugin1"}
local plugin2 = {name = "plugin2"}
local tab = {
plugins = {
p1 = plugin1,
p2 = plugin2,
},
}

local tab_copied = deepcopy(tab, { shallow_prefix = "self.plugins" })

tab_copied.plugins.p1 = {name = "plugin1_new_modified"}
tab_copied.plugins.p2.name = "plugin2_modified"

ngx.say("table copied: ", require("toolkit.json").encode(tab_copied))
ngx.say("table original: ", require("toolkit.json").encode(tab))
}
}
--- request
GET /t
--- response_body
table copied: {"plugins":{"p1":{"name":"plugin1_new_modified"},"p2":{"name":"plugin2_modified"}}}
table original: {"plugins":{"p1":{"name":"plugin1"},"p2":{"name":"plugin2_modified"}}}
--- no_error_log
[error]
Loading