Skip to content

Commit

Permalink
feat(cjson): ensure cjson.new encode number with a precision of 16
Browse files Browse the repository at this point in the history
decimals
  • Loading branch information
ms2008 committed Dec 30, 2023
1 parent 541487a commit a8740bf
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 0 additions & 2 deletions kong/db/strategies/postgres/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ local utils = require "kong.tools.utils"
local new_tab = require "table.new"
local clear_tab = require "table.clear"

cjson.encode_number_precision(16)
cjson_safe.encode_number_precision(16)

local kong = kong
local ngx = ngx
Expand Down
21 changes: 21 additions & 0 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ return function(options)
options = options or {}
local meta = require "kong.meta"

do -- make cjson encode numbers with a precision of up to 16 decimals
local cjson = require "cjson"
local cjson_safe = require "cjson.safe"
local native_cjson_new = cjson.new
local native_cjson_safe_new = cjson_safe.new

cjson.encode_number_precision(16)
cjson_safe.encode_number_precision(16)

cjson.new = function()
local json = native_cjson_new()
json.encode_number_precision(16)
return json
end

cjson_safe.new = function()
local json = native_cjson_safe_new()
json.encode_number_precision(16)
return json
end
end

local cjson = require("cjson.safe")
cjson.encode_sparse_array(nil, nil, 2^15)
Expand Down
1 change: 0 additions & 1 deletion kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local utils = require "kong.tools.utils"
local request_id = require "kong.tracing.request_id"
local constants = require "kong.constants"

cjson.encode_number_precision(16)

local ngx = ngx
local arg = ngx.arg
Expand Down
1 change: 0 additions & 1 deletion kong/plugins/zipkin/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local to_hex = require "resty.string".to_hex
local cjson = require "cjson".new()
local Queue = require "kong.tools.queue"

cjson.encode_number_precision(16)

local zipkin_reporter_methods = {}
local zipkin_reporter_mt = {
Expand Down
60 changes: 60 additions & 0 deletions spec/02-integration/05-proxy/34-max_safe_integer_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local helpers = require "spec.helpers"

for _, strategy in helpers.each_strategy() do
describe("cjson.new encode number with a precision of 16 decimals [#" .. strategy .. "]", function()
local proxy_client

lazy_setup(function()
local bp = helpers.get_db_utils(strategy, {
"routes",
"services",
"plugins",
}, { "pre-function" })

local route = bp.routes:insert({
paths = { "/route_with_max_safe_integer_priority"},
})

bp.plugins:insert {
route = { id = route.id },
name = "pre-function",
config = {
access = {
[[
local cjson = require("cjson").new()
ngx.say(cjson.encode({ n = 9007199254740992 }))
]]
},
}
}

assert(helpers.start_kong({
database = strategy,
untrusted_lua = "on",
plugins = "bundled",
nginx_conf = "spec/fixtures/custom_nginx.template",
nginx_worker_processes = 1
}))

proxy_client = helpers.proxy_client()
end)

lazy_teardown(function()
if proxy_client then
proxy_client:close()
end

helpers.stop_kong()
end)

it("the maximum safe integer can be accurately represented as a decimal number", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/route_with_max_safe_integer_priority"
})

assert.res_status(200, res)
assert.match_re(res:read_body(), "9007199254740992")
end)
end)
end
1 change: 1 addition & 0 deletions t/01-pdk/08-response/11-exit.t
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ manually setting Transfer-Encoding. Ignored.
location = /t {
default_type 'text/test';
access_by_lua_block {
require("kong.globalpatches")()
local PDK = require "kong.pdk"
local pdk = PDK.new()
Expand Down

0 comments on commit a8740bf

Please sign in to comment.