AlinsRan commented on code in PR #13818:
URL: https://github.com/apache/apisix/pull/13818#discussion_r3782228625
##########
t/plugin/data-mask.t:
##########
@@ -897,3 +897,263 @@ nginx_config:
GET /hello?password=secret&token=mytoken
--- access_log eval
qr/GET \/hello\?token=\*\*\*\*\* HTTP\/\d+\.\d+/
+
+
+
+=== TEST 21: create route for removing a middle JSON array element
+--- 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,
+ [[{
+ "plugins": {
+ "data-mask": {
+ "request": [
+ {
+ "action": "remove",
+ "body_format": "json",
+ "name": "$.items[1]",
+ "type": "body"
+ }
+ ]
+ },
+ "file-logger": {
+ "include_req_body": true,
+ "path": "mask-json-array-hole.log"
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1982": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+
+
+
+=== TEST 22: verify removing a middle array element compacts the array
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+
+ os.remove("mask-json-array-hole.log")
+ local code = t("/hello", ngx.HTTP_POST,
[[{"items":["a","drop-me","c"]}]])
+
+ local fd, err = io.open("mask-json-array-hole.log", "r")
+ if not fd then
+ core.log.error("failed to open file: ", err)
+ return
+ end
+ local line = fd:read()
+ local log = core.json.decode(line)
+ os.remove("mask-json-array-hole.log")
+
+ if not log or not log.request or not log.request.body then
+ ngx.say("missing logged request body")
+ return
+ end
+
+ local body = core.json.decode(log.request.body)
+ if not body or type(body.items) ~= "table" then
+ ngx.say("items missing: " .. tostring(log.request.body))
+ return
+ end
+ if #body.items ~= 2 then
+ ngx.say("expected compacted array of 2, got: " ..
core.json.encode(body.items))
+ return
+ end
+ if body.items[1] ~= "a" or body.items[2] ~= "c" then
+ ngx.say("array not compacted: " ..
core.json.encode(body.items))
+ return
+ end
+ ngx.say("success")
+ }
+ }
+--- response_body
+success
+
+
+
+=== TEST 23: create route for removing all JSON array elements
+--- 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,
+ [[{
+ "plugins": {
+ "data-mask": {
+ "request": [
+ {
+ "action": "remove",
+ "body_format": "json",
+ "name": "$.items[*]",
+ "type": "body"
+ }
+ ]
+ },
+ "file-logger": {
+ "include_req_body": true,
+ "path": "mask-json-array-star.log"
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1982": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+
+
+
+=== TEST 24: verify removing all array elements yields an empty array
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+
+ os.remove("mask-json-array-star.log")
+ local code = t("/hello", ngx.HTTP_POST,
[[{"items":["a","b","c"]}]])
+
+ local fd, err = io.open("mask-json-array-star.log", "r")
+ if not fd then
+ core.log.error("failed to open file: ", err)
+ return
+ end
+ local line = fd:read()
+ local log = core.json.decode(line)
+ os.remove("mask-json-array-star.log")
+
+ if not log or not log.request or not log.request.body then
+ ngx.say("missing logged request body")
+ return
+ end
+
+ local body = core.json.decode(log.request.body)
+ if not body or type(body.items) ~= "table" then
+ ngx.say("items missing: " .. tostring(log.request.body))
+ return
+ end
+ if #body.items ~= 0 then
Review Comment:
Minor: this assertion runs on the decoded value, so it cannot tell `[]` from
`{}` — both decode to an empty table with `#t == 0`. Asserting on the raw
logged string would also pin the JSON type:
```lua
if log.request.body ~= '{"items":[]}' then
ngx.say("expected an empty array, got: " .. log.request.body)
return
end
```
To be clear, the plugin already emits `[]` — no code change needed.
`apisix/patch.lua:385` calls `cjson.decode_array_with_array_mt(true)`, so
decoded arrays carry `array_mt` and `table.remove` preserves the metatable down
to empty. Measured on this branch:
```
RAW={"items":[]} | mt_is_array=true | len_assert_passes_for=true
```
Raising it because a review bot suggested re-applying `array_mt` after
removal on the EE mirror of this patch — that would be dead code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]