From b6bfe2ff1fc09a21d62c5c5b4c3202456fabb9f1 Mon Sep 17 00:00:00 2001 From: baiyun <337531158@qq.com> Date: Tue, 23 Jan 2024 14:13:48 +0800 Subject: [PATCH 1/3] feat: body-transformer plugin for other data formats without warnings --- apisix/plugins/body-transformer.lua | 4 +- t/plugin/body-transformer.t | 57 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua index 368ccf9de0d2..d883d967f51c 100644 --- a/apisix/plugins/body-transformer.lua +++ b/apisix/plugins/body-transformer.lua @@ -34,7 +34,7 @@ local next = next local transform_schema = { type = "object", properties = { - input_format = { type = "string", enum = {"xml", "json", "encoded", "args"} }, + input_format = { type = "string", enum = {"xml", "json", "encoded", "args", "none"} }, template = { type = "string" }, template_is_base64 = { type = "boolean" }, }, @@ -129,7 +129,7 @@ end local function transform(conf, body, typ, ctx, request_method) local out = {} local format = conf[typ].input_format - if body or request_method == "GET" then + if (body or request_method == "GET") and format ~= "none" then local err if format then out, err = decoders[format](body) diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t index 33b9b22610bf..0726d0be8cfb 100644 --- a/t/plugin/body-transformer.t +++ b/t/plugin/body-transformer.t @@ -1069,3 +1069,60 @@ location /demo { assert(res.status == 200) } } + + +=== TEST 16: test for missing Content-Type and skip body parsing +--- config + location /demo { + content_by_lua_block { + local core = require("apisix.core") + local body = core.request.get_body() + assert(body == "{\"message\": \"actually json\"}") + } + } + location /t { + content_by_lua_block { + local t = require("lib.test_admin") + local core = require("apisix.core") + + local code, body = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + string.format([[{ + "uri": "/foobar", + "plugins": { + "proxy-rewrite": { + "uri": "/demo" + }, + "body-transformer": { + "request": { + "input_format": "none", + "template": "{\"message\": \"{* string.gsub(_body, 'not ', '') *}\"}" + } + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:%d": 1 + } + } + }]], ngx.var.server_port) + ) + + if code >= 300 then + ngx.status = code + return + end + ngx.sleep(0.5) + + local http = require("resty.http") + local httpc = http.new() + local res, err = httpc:request_uri("http://127.0.0.1:" .. ngx.var.server_port .. "/foobar", { + method = "POST", + body = "not actually json", + }) + assert(res.status == 200) + } + } +--- no_error_log +no input format to parse From d7bc196760fcb6920486588c8361220997c96e06 Mon Sep 17 00:00:00 2001 From: baiyun <337531158@qq.com> Date: Wed, 24 Jan 2024 09:30:03 +0800 Subject: [PATCH 2/3] fix cody style --- t/plugin/body-transformer.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t index 0726d0be8cfb..857d1671b71f 100644 --- a/t/plugin/body-transformer.t +++ b/t/plugin/body-transformer.t @@ -1071,6 +1071,7 @@ location /demo { } + === TEST 16: test for missing Content-Type and skip body parsing --- config location /demo { From 1b768198ec8f071d26b70ab84790dfef874af463 Mon Sep 17 00:00:00 2001 From: baiyun <337531158@qq.com> Date: Wed, 24 Jan 2024 16:45:09 +0800 Subject: [PATCH 3/3] input_format is adjusted to plain --- apisix/plugins/body-transformer.lua | 4 ++-- t/plugin/body-transformer.t | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua index d883d967f51c..977ebce676f0 100644 --- a/apisix/plugins/body-transformer.lua +++ b/apisix/plugins/body-transformer.lua @@ -34,7 +34,7 @@ local next = next local transform_schema = { type = "object", properties = { - input_format = { type = "string", enum = {"xml", "json", "encoded", "args", "none"} }, + input_format = { type = "string", enum = {"xml", "json", "encoded", "args", "plain"} }, template = { type = "string" }, template_is_base64 = { type = "boolean" }, }, @@ -129,7 +129,7 @@ end local function transform(conf, body, typ, ctx, request_method) local out = {} local format = conf[typ].input_format - if (body or request_method == "GET") and format ~= "none" then + if (body or request_method == "GET") and format ~= "plain" then local err if format then out, err = decoders[format](body) diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t index 857d1671b71f..929ed1aac7c2 100644 --- a/t/plugin/body-transformer.t +++ b/t/plugin/body-transformer.t @@ -1096,7 +1096,7 @@ location /demo { }, "body-transformer": { "request": { - "input_format": "none", + "input_format": "plain", "template": "{\"message\": \"{* string.gsub(_body, 'not ', '') *}\"}" } }