Skip to content

Commit

Permalink
fix(pdk.log): fix a bug that pdk.log.serialize() will throw an error
Browse files Browse the repository at this point in the history
when json entity set via pdk.log.set_serialize_value contains cjson.null
as value.

FTI-6096
  • Loading branch information
liverpool8056 committed Jul 15, 2024
1 parent 1158224 commit 2699ef2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local inspect = require("inspect")
local phase_checker = require("kong.pdk.private.phases")
local constants = require("kong.constants")
local clear_tab = require("table.clear")
local cjson_null = require("cjson").null


local request_id_get = require("kong.observability.tracing.request_id").get
Expand Down Expand Up @@ -640,7 +641,7 @@ do

local function is_valid_value(v, visited)
local t = type(v)
if v == nil or t == "number" or t == "string" or t == "boolean" then
if v == nil or v == cjson_null or t == "number" or t == "string" or t == "boolean" then
return true
end

Expand Down
8 changes: 8 additions & 0 deletions spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ describe("kong.log.serialize", function()
assert.not_equal(tostring(ngx.ctx.service),
tostring(res.service))
end)

it("does not fail when coming across 'cjson.null' in response body", function()
local cjson_null = require "cjson".null
kong.log.set_serialize_value("response.body", cjson_null)
local pok, value = pcall(kong.log.serialize, {})
assert.is_true(pok)
assert.is_true(type(value) == "table")
end)
end)
end)

Expand Down

0 comments on commit 2699ef2

Please sign in to comment.