Skip to content

Commit

Permalink
fix(response-transformer) preserve empty arrays
Browse files Browse the repository at this point in the history
When a response contains an empty array and it gets transformed, the empty array should remain an empty array (and not get transformed into an empty cjson object).

Fixes #1208.
  • Loading branch information
kikito committed Aug 13, 2019
1 parent 4e5544f commit 454f09c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion kong/plugins/response-transformer/body_transformer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local cjson = require "cjson.safe"
local cjson = require("cjson.safe").new()


local insert = table.insert
Expand All @@ -10,6 +10,9 @@ local match = string.match
local lower = string.lower


cjson.decode_array_with_array_mt(true)


local noop = function() end


Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down

0 comments on commit 454f09c

Please sign in to comment.