From 3a44df6169c4d4a5cd06489141901d0a040dfb7d Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Sat, 6 Nov 2021 13:06:20 +0800 Subject: [PATCH 1/2] fix(admin): modify boolean parameters with PATCH Closes #5430 Signed-off-by: tzssangglass --- apisix/admin/init.lua | 2 +- apisix/admin/routes.lua | 2 +- t/admin/routes3.t | 100 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua index f169d5aec6c9..9fec46403068 100644 --- a/apisix/admin/init.lua +++ b/apisix/admin/init.lua @@ -157,7 +157,7 @@ local function run() if req_body then local data, err = core.json.decode(req_body) - if not data then + if err then core.log.error("invalid request body: ", req_body, " err: ", err) core.response.exit(400, {error_msg = "invalid request body: " .. err, req_body = req_body}) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index bed0524e8384..c3705d4ad476 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -247,7 +247,7 @@ function _M.patch(id, conf, sub_path, args) return 400, {error_msg = "missing route id"} end - if not conf then + if conf == nil then return 400, {error_msg = "missing new configuration"} end diff --git a/t/admin/routes3.t b/t/admin/routes3.t index 6f0b13fc417e..72d8c5351a0a 100644 --- a/t/admin/routes3.t +++ b/t/admin/routes3.t @@ -700,3 +700,103 @@ passed } --- response_body passed + + + +=== TEST 20: set route(id: 1, parameters with boolean values) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/index.html", + "enable_websocket": true, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:8080":1 + } + } + }]], + [[{ + "node": { + "value": { + "uri": "/index.html", + "enable_websocket": true, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:8080":1 + } + } + }, + "key": "/apisix/routes/1" + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 21: patch route(modify the boolean value of parameters to false) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1/enable_websocket', + ngx.HTTP_PATCH, + 'false', + [[{ + "node": { + "value": { + "enable_websocket": false + }, + "key": "/apisix/routes/1" + }, + "action": "compareAndSwap" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 22: patch route(modify the boolean value of parameters to true) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1/enable_websocket', + ngx.HTTP_PATCH, + 'true', + [[{ + "node": { + "value": { + "enable_websocket": true + }, + "key": "/apisix/routes/1" + }, + "action": "compareAndSwap" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- response_body +passed From 8b6477b821236ef64be0ba3f698f1f78128127b4 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Mon, 8 Nov 2021 09:38:13 +0800 Subject: [PATCH 2/2] resolve code review Signed-off-by: tzssangglass --- t/admin/routes3.t | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/t/admin/routes3.t b/t/admin/routes3.t index 72d8c5351a0a..02fbc8711ba0 100644 --- a/t/admin/routes3.t +++ b/t/admin/routes3.t @@ -711,34 +711,16 @@ passed local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, [[{ - "uri": "/index.html", - "enable_websocket": true, - "upstream": { - "type": "roundrobin", - "nodes": { - "127.0.0.1:8080":1 - } - } - }]], - [[{ - "node": { - "value": { - "uri": "/index.html", - "enable_websocket": true, - "upstream": { - "type": "roundrobin", - "nodes": { - "127.0.0.1:8080":1 - } - } - }, - "key": "/apisix/routes/1" - }, - "action": "set" - }]] - ) + "uri": "/index.html", + "enable_websocket": true, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:8080":1 + } + } + }]]) - ngx.status = code ngx.say(body) } }