diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua index 1d1afa06e694..5b5557f7d4d7 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 fd21621cbc8c..8baf2ef0de16 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([[ + + + + 33333333333 + + 33333333333 + + + + + + ]]) + } + } + + 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)) + } + }