Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: deepcopy doesn't copy the metatable #6623

Merged
merged 1 commit into from
Mar 16, 2022
Merged
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
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]