Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pdk.log): fix a bug that pdk.log.serialize() does not properly handle JSON nulls #13376

Merged
merged 5 commits into from
Jul 22, 2024

Conversation

liverpool8056
Copy link
Contributor

@liverpool8056 liverpool8056 commented Jul 15, 2024

Summary

As titled, this PR is to fix the bug reported by FTI-6096

Checklist

Issue reference

Fix #FTI-6096

when json entity set via pdk.log.set_serialize_value contains cjson.null
as value.

FTI-6096
@github-actions github-actions bot added core/logs cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee labels Jul 15, 2024
@liverpool8056 liverpool8056 changed the title fix(pdk.log): fix a bug that pdk.log.serialize() throws error when json entity set via pdk.log.set_serialize_value contains cjson.null fix(pdk.log): fix a bug that pdk.log.serialize() does not properly handle JSON nulls Jul 15, 2024
changelog/unreleased/kong/pdk-log-error.yml Outdated Show resolved Hide resolved
Co-authored-by: Zachary Hu <6426329+outsinre@users.noreply.github.com>
Copy link
Contributor

@vm-001 vm-001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both cjson.null and ngx.null are userdata. Since the PR is treating cjson.null as a valid value, do you think the last one ngx.null should also be considered?

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, {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it need be wrap by a pcall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just have a change to handle exception thrown from the serialize()

@ms2008
Copy link
Contributor

ms2008 commented Jul 15, 2024

Both cjson.null and ngx.null are userdata. Since the PR is treating cjson.null as a valid value, do you think the last one ngx.null should also be considered?

cjson.null is the same as ngx.null:

$ resty -e 'local cjson = require "cjson"; print(cjson.null == ngx.null)'
true

kong/pdk/log.lua Outdated
@@ -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
Copy link
Contributor

@fffonion fffonion Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker for this PR, but we should also exclude FFI null from it

local ffi = require "ffi"

local n = ffi.new("void*")
print(tostring(n))
print(n == nil)

local cjson = require "cjson"

print(cjson.encode(n))

outputs

cdata<void *>: NULL
true
ERROR: /tmp/a.lua:9: Cannot serialise cdata: type not supported

@liverpool8056 liverpool8056 requested a review from ms2008 July 22, 2024 03:00
@ms2008 ms2008 merged commit 163f72a into master Jul 22, 2024
25 checks passed
@ms2008 ms2008 deleted the pdk-log-error branch July 22, 2024 03:10
@team-gateway-bot
Copy link
Collaborator

Successfully created cherry-pick PR for master:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee core/logs size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants