diff --git a/kong/plugins/response-transformer/body_transformer.lua b/kong/plugins/response-transformer/body_transformer.lua index 57b9cff73eb2..3a30968ceb4b 100644 --- a/kong/plugins/response-transformer/body_transformer.lua +++ b/kong/plugins/response-transformer/body_transformer.lua @@ -1,4 +1,4 @@ -local cjson = require "cjson.safe" +local cjson = require("cjson.safe").new() local insert = table.insert @@ -10,6 +10,9 @@ local match = string.match local lower = string.lower +cjson.decode_array_with_array_mt(true) + + local noop = function() end diff --git a/spec/03-plugins/15-response-transformer/02-body_transformer_spec.lua b/spec/03-plugins/15-response-transformer/02-body_transformer_spec.lua index a550753af363..4cd15ce5e48d 100644 --- a/spec/03-plugins/15-response-transformer/02-body_transformer_spec.lua +++ b/spec/03-plugins/15-response-transformer/02-body_transformer_spec.lua @@ -1,5 +1,6 @@ local body_transformer = require "kong.plugins.response-transformer.body_transformer" -local cjson = require "cjson" +local cjson = require("cjson.safe").new() +cjson.decode_array_with_array_mt(true) describe("Plugin: response-transformer", function() describe("transform_json_body()", function() @@ -30,6 +31,13 @@ describe("Plugin: response-transformer", function() local body_json = cjson.decode(body) assert.same({p1 = "v1", p2 = "v1", p3 = "value:3", p4 = '"v1"'}, body_json) end) + it("preserves empty arrays", function() + local json = [[{"p2":"v1", "a":[]}]] + local body = body_transformer.transform_json_body(conf, json) + local body_json = cjson.decode(body) + assert.same({p1 = "v1", p2 = "v1", p3 = "value:3", p4 = '"v1"', a = {}}, body_json) + assert.equals('[]', cjson.encode(body_json.a)) + end) end) describe("append", function() @@ -65,6 +73,13 @@ describe("Plugin: response-transformer", function() local body_json = cjson.decode(body) assert.same({p1 = {"v1"}, p3 = {"v2",'"v1"'}}, body_json) end) + it("preserves empty arrays", function() + local json = [[{"p2":"v1", "a":[]}]] + local body = body_transformer.transform_json_body(conf, json) + local body_json = cjson.decode(body) + assert.same({ p2 = "v1", p1 = {"v1"}, p3 = {'"v1"'}, a = {} }, body_json) + assert.equals('[]', cjson.encode(body_json.a)) + end) end) describe("remove", function() @@ -87,6 +102,13 @@ describe("Plugin: response-transformer", function() local body = body_transformer.transform_json_body(conf, json) assert.equals("{}", body) end) + it("preserves empty arrays", function() + local json = [[{"p1" : "v1", "p2" : "v1", "a": []}]] + local body = body_transformer.transform_json_body(conf, json) + local body_json = cjson.decode(body) + assert.same({a = {}}, body_json) + assert.equals('[]', cjson.encode(body_json.a)) + end) end) describe("replace", function() @@ -122,6 +144,13 @@ describe("Plugin: response-transformer", function() local body_json = cjson.decode(body) assert.same({p2 = '"v2"'}, body_json) end) + it("preserves empty arrays", function() + local json = [[{"p1" : "v1", "p2" : "v1", "a": []}]] + local body = body_transformer.transform_json_body(conf, json) + local body_json = cjson.decode(body) + assert.same({p1 = "v2", p2 = '"v2"', a = {}}, body_json) + assert.equals('[]', cjson.encode(body_json.a)) + end) end) describe("remove, replace, add, append", function() @@ -145,6 +174,13 @@ describe("Plugin: response-transformer", function() local body_json = cjson.decode(body) assert.same({p2 = "v2", p3 = {"v1", "v2"}}, body_json) end) + it("preserves empty array", function() + local json = [[{"p1" : "v1", "p2" : "v1", "a" : []}]] + local body = body_transformer.transform_json_body(conf, json) + local body_json = cjson.decode(body) + assert.same({p2 = "v2", p3 = {"v1", "v2"}, a = {}}, body_json) + assert.equals('[]', cjson.encode(body_json.a)) + end) end) end)