@@ -897,3 +897,91 @@ nginx_config:
897897GET /hello?password=secret&token=mytoken
898898--- access_log eval
899899qr/GET \/hello\?token=\*\*\*\*\* HTTP\/\d+\.\d+/
900+
901+
902+
903+ === TEST 21: create route for removing a middle JSON array element
904+ --- config
905+ location /t {
906+ content_by_lua_block {
907+ local t = require("lib.test_admin").test
908+ local code, body = t('/apisix/admin/routes/1',
909+ ngx.HTTP_PUT,
910+ [[{
911+ "plugins": {
912+ "data-mask": {
913+ "request": [
914+ {
915+ "action": "remove",
916+ "body_format": "json",
917+ "name": "$.items[1]",
918+ "type": "body"
919+ }
920+ ]
921+ },
922+ "file-logger": {
923+ "include_req_body": true,
924+ "path": "mask-json-array-hole.log"
925+ }
926+ },
927+ "upstream": {
928+ "nodes": {
929+ "127.0.0.1:1982": 1
930+ },
931+ "type": "roundrobin"
932+ },
933+ "uri": "/hello"
934+ }]]
935+ )
936+
937+ if code >= 300 then
938+ ngx.status = code
939+ end
940+ ngx.say(body)
941+ }
942+ }
943+
944+
945+
946+ === TEST 22: verify removing a middle array element compacts the array
947+ --- config
948+ location /t {
949+ content_by_lua_block {
950+ local core = require("apisix.core")
951+ local t = require("lib.test_admin").test
952+
953+ os.remove("mask-json-array-hole.log")
954+ local code = t("/hello", ngx.HTTP_POST, [[{"items":["a","drop-me","c"]}]])
955+
956+ local fd, err = io.open("mask-json-array-hole.log", "r")
957+ if not fd then
958+ core.log.error("failed to open file: ", err)
959+ return
960+ end
961+ local line = fd:read()
962+ local log = core.json.decode(line)
963+ os.remove("mask-json-array-hole.log")
964+
965+ if not log or not log.request or not log.request.body then
966+ ngx.say("missing logged request body")
967+ return
968+ end
969+
970+ local body = core.json.decode(log.request.body)
971+ if not body or type(body.items) ~= "table" then
972+ ngx.say("items missing: " .. tostring(log.request.body))
973+ return
974+ end
975+ if #body.items ~= 2 then
976+ ngx.say("expected compacted array of 2, got: " .. core.json.encode(body.items))
977+ return
978+ end
979+ if body.items[1] ~= "a" or body.items[2] ~= "c" then
980+ ngx.say("array not compacted: " .. core.json.encode(body.items))
981+ return
982+ end
983+ ngx.say("success")
984+ }
985+ }
986+ --- response_body
987+ success
0 commit comments