Skip to content

Commit

Permalink
fix: deepcopy doesn't copy the metatable (#6623)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Mar 16, 2022
1 parent ca2174a commit 91b57e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ do
end
end

local mt = getmetatable(orig)
if mt ~= nil then
setmetatable(copy, mt)
end

return copy
end

Expand Down
25 changes: 25 additions & 0 deletions t/core/table.t
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,28 @@ ok
GET /t
--- no_error_log
[error]
=== TEST 7: deepcopy should keep metatable
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local deepcopy = core.table.deepcopy
local t = setmetatable({}, core.json.array_mt)
local actual = core.json.encode(deepcopy(t))
local expect = "[]"
if actual ~= expect then
ngx.say("expect ", expect, ", actual ", actual)
return
end
ngx.say("ok")
}
}
--- request
GET /t
--- response_body
ok
--- no_error_log
[error]

0 comments on commit 91b57e6

Please sign in to comment.