This is an automated email from the ASF dual-hosted git repository.
monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 632c7c0b0 fix(body-transformer): xml2lua: replace empty table with
empty string (#9669)
632c7c0b0 is described below
commit 632c7c0b0f8373f2250a8c52d9345e514a2e1731
Author: jinhua luo <[email protected]>
AuthorDate: Sun Jun 25 14:46:33 2023 +0800
fix(body-transformer): xml2lua: replace empty table with empty string
(#9669)
---
apisix/plugins/body-transformer.lua | 7 ++++
t/plugin/body-transformer.t | 75 ++++++++++++++++++++++++++++++++++++-
2 files changed, 81 insertions(+), 1 deletion(-)
diff --git a/apisix/plugins/body-transformer.lua
b/apisix/plugins/body-transformer.lua
index 1d1afa06e..5b5557f7d 100644
--- a/apisix/plugins/body-transformer.lua
+++ b/apisix/plugins/body-transformer.lua
@@ -25,6 +25,7 @@ local str_format = string.format
local type = type
local pcall = pcall
local pairs = pairs
+local next = next
local transform_schema = {
@@ -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
@@ -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
diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t
index fd21621cb..8baf2ef0d 100644
--- a/t/plugin/body-transformer.t
+++ b/t/plugin/body-transformer.t
@@ -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))
}
}
@@ -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))
+ }
+ }