From e2f33c5e076b884a6fcbad4f84db343701419b9c Mon Sep 17 00:00:00 2001 From: Kenneth Date: Wed, 23 Oct 2024 11:08:54 +0200 Subject: [PATCH] fix: body-transformer - failed to transform response body - XML to JSON --- apisix/plugins/body-transformer.lua | 33 ++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua index 977ebce676f0..881950a0d36d 100644 --- a/apisix/plugins/body-transformer.lua +++ b/apisix/plugins/body-transformer.lua @@ -78,23 +78,32 @@ end local function remove_namespace(tbl) - for k, v in pairs(tbl) do - if type(v) == "table" and next(v) == nil then + + local function update_namespace(ns) + local v = ns + if type(ns) == "table" and next(ns) == nil then v = "" - tbl[k] = v + elseif type(ns) == "table" then + v = remove_namespace(ns) end - if type(k) == "string" then - local newk = k:match(".*:(.*)") - if newk then - tbl[newk] = v - tbl[k] = nil - end - if type(v) == "table" then - remove_namespace(v) + return v + end + + local function remove_namespace_wrapper(old_table, new_table) + for k, v in pairs(old_table) do + if type(k) == "string" then + local newk = k:match(".*:(.*)") + if newk then + new_table[newk] = update_namespace(v) + else + new_table[k] = update_namespace(v) + end end end + return new_table end - return tbl + + return remove_namespace_wrapper(tbl, {}) end