Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/resty/session/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ local encode_json, decode_json do
encode_json = function(value)
if not cjson then
cjson = require("cjson.safe").new()
-- decode arrays with the array metatable so an empty array round-trips
-- as `[]` instead of being re-encoded as `{}` (encode and decode share
-- this instance, so the option is set wherever it is created first).
cjson.decode_array_with_array_mt(true)
end
encode_json = cjson.encode
return encode_json(value)
Expand All @@ -263,6 +267,10 @@ local encode_json, decode_json do
decode_json = function(value)
if not cjson then
cjson = require("cjson.safe").new()
-- decode arrays with the array metatable so an empty array round-trips
-- as `[]` instead of being re-encoded as `{}` (encode and decode share
-- this instance, so the option is set wherever it is created first).
cjson.decode_array_with_array_mt(true)
end
decode_json = cjson.decode
return decode_json(value)
Expand Down
10 changes: 10 additions & 0 deletions spec/01-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ describe("Testing utils", function()

assert.same(decoded, input)
end)

it("preserves empty arrays across decode then re-encode", function()
-- An empty JSON array must survive a decode + re-encode round-trip as
-- `[]`, not turn into `{}`. Without decode_array_with_array_mt the decoded
-- empty array loses its array metatable and is re-encoded as an object.
local decoded = utils.decode_json('{"items":[]}')
local reencoded = utils.encode_json(decoded)

assert.equals('{"items":[]}', reencoded)
end)
end)

describe("encode/decode base64url", function()
Expand Down