From 2699ef2d5441f23e4eceff164030ff1aa391a834 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Mon, 15 Jul 2024 15:03:08 +0800 Subject: [PATCH] fix(pdk.log): fix a bug that pdk.log.serialize() will throw an error when json entity set via pdk.log.set_serialize_value contains cjson.null as value. FTI-6096 --- kong/pdk/log.lua | 3 ++- spec/01-unit/10-log_serializer_spec.lua | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 990ea202471d..56a68651f54f 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -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 @@ -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 diff --git a/spec/01-unit/10-log_serializer_spec.lua b/spec/01-unit/10-log_serializer_spec.lua index df8a0d5ac550..d9923c23b0b4 100644 --- a/spec/01-unit/10-log_serializer_spec.lua +++ b/spec/01-unit/10-log_serializer_spec.lua @@ -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)