Skip to content

Commit

Permalink
fix(body-transformer): xml2lua: replace empty table with empty string (
Browse files Browse the repository at this point in the history
  • Loading branch information
kingluo committed Jun 25, 2023
1 parent f3b549b commit 632c7c0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apisix/plugins/body-transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local str_format = string.format
local type = type
local pcall = pcall
local pairs = pairs
local next = next


local transform_schema = {
Expand Down Expand Up @@ -74,6 +75,10 @@ end

local function remove_namespace(tbl)
for k, v in pairs(tbl) do
if type(v) == "table" and next(v) == nil then
v = ""
tbl[k] = v
end
if type(k) == "string" then
local newk = k:match(".*:(.*)")
if newk then
Expand Down Expand Up @@ -123,6 +128,8 @@ local function transform(conf, body, typ, ctx)
core.log.error(err, ", body=", body)
return nil, 400, err
end
else
core.log.warn("no input format to parse ", typ, " body")
end
end

Expand Down
75 changes: 74 additions & 1 deletion t/plugin/body-transformer.t
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ location /demo {
assert(res.status == 200)
local data1 = core.json.decode(res.body)
local data2 = core.json.decode[[{"status":"200","currency":"EUR","population":46704314,"capital":"Madrid","name":"Spain"}]]
assert(core.json.stably_encode(data1), core.json.stably_encode(data2))
assert(core.json.stably_encode(data1) == core.json.stably_encode(data2))
}
}
Expand Down Expand Up @@ -821,3 +821,76 @@ location /demo {
assert(data.raw_body == '{"result": "hello world"}')
}
}
=== TEST 12: empty xml value should be rendered as empty string
--- config
location /demo {
content_by_lua_block {
ngx.print([[
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xrd="http://x-road.eu/xsd/xroad.xsd" xmlns:prod="http://rr.x-road.eu/producer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:id="http://x-road.eu/xsd/identifiers" xmlns:repr="http://x-road.eu/xsd/representation.xsd" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<prod:RR58isikEpiletResponse>
<request><Isikukood>33333333333</Isikukood></request>
<response>
<Isikukood>33333333333</Isikukood>
<KOVKood></KOVKood>
</response>
</prod:RR58isikEpiletResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
]])
}
}
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local rsp_template = ngx.encode_base64[[
{ "KOVKood":"{{Envelope.Body.RR58isikEpiletResponse.response.KOVKood}}" }
]]
local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
string.format([[{
"uri": "/ws",
"plugins": {
"proxy-rewrite": {
"uri": "/demo"
},
"body-transformer": {
"response": {
"input_format": "xml",
"template": "%s"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:%d": 1
}
}
}]], rsp_template, ngx.var.server_port)
)
if code >= 300 then
ngx.status = code
return
end
ngx.sleep(0.5)
local core = require("apisix.core")
local http = require("resty.http")
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ws"
local opt = {method = "GET"}
local httpc = http.new()
local res = httpc:request_uri(uri, opt)
assert(res.status == 200)
local data1 = core.json.decode(res.body)
local data2 = core.json.decode[[{"KOVKood":""}]]
assert(core.json.stably_encode(data1) == core.json.stably_encode(data2))
}
}

0 comments on commit 632c7c0

Please sign in to comment.