-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
feat(pdk): increase the precision of JSON number encoding from 14 to 16 decimals #12019
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7015392
feat(pdk): increase the precision of JSON number encoding from 14 to 16
ms2008 2bd55b2
tests(*): fix failed tests
ms2008 0a2d5aa
feat(cjson): ensure `cjson.new` encode number with a precision of 16
ms2008 018b400
Revert "feat(cjson): ensure `cjson.new` encode number with a precisio…
ms2008 57ff5df
feat(PDK): ensure that the PDK uses a separate cjson instance
ms2008 1601bc8
chore(*): applying the suggestions from reviewer
ms2008 9deee70
chore(*): remove the default global cjson instance setting
ms2008 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
changelog/unreleased/kong/pdk-json-encoding-numbers-precision.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: Increase the precision of JSON number encoding from 14 to 16 decimals | ||
type: feature | ||
scope: PDK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
local cjson = require "cjson.safe".new() | ||
local constants = require "kong.constants" | ||
|
||
cjson.decode_array_with_array_mt(true) | ||
cjson.encode_sparse_array(nil, nil, 2^15) | ||
cjson.encode_number_precision(constants.CJSON_MAX_PRECISION) | ||
|
||
local _M = {} | ||
|
||
|
||
function _M.encode(json_text) | ||
return cjson.encode(json_text) | ||
end | ||
|
||
function _M.decode_with_array_mt(json_text) | ||
return cjson.decode(json_text) | ||
end | ||
|
||
_M.array_mt = cjson.array_mt | ||
|
||
return _M |
110 changes: 110 additions & 0 deletions
110
spec/02-integration/04-admin_api/25-max_safe_integer_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
local LMDB_MAP_SIZE = "10m" | ||
|
||
for _, strategy in helpers.each_strategy() do | ||
if strategy ~= "off" then | ||
describe("Admin API #" .. strategy, function() | ||
local bp | ||
local client, route | ||
|
||
lazy_setup(function() | ||
bp = helpers.get_db_utils(strategy, { | ||
"routes", | ||
"services", | ||
}) | ||
|
||
route = bp.routes:insert({ | ||
paths = { "/route_with_max_safe_integer_priority"}, | ||
regex_priority = 9007199254740992, | ||
}) | ||
|
||
assert(helpers.start_kong({ | ||
database = strategy, | ||
})) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong(nil, true) | ||
end) | ||
|
||
before_each(function() | ||
client = assert(helpers.admin_client()) | ||
end) | ||
|
||
after_each(function() | ||
if client then | ||
client:close() | ||
end | ||
end) | ||
|
||
it("the maximum safe integer can be accurately represented as a decimal number", function() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/routes/" .. route.id | ||
}) | ||
assert.res_status(200, res) | ||
assert.match_re(res:read_body(), "9007199254740992") | ||
end) | ||
end) | ||
end | ||
|
||
if strategy == "off" then | ||
describe("Admin API #off", function() | ||
local client | ||
|
||
lazy_setup(function() | ||
assert(helpers.start_kong({ | ||
database = "off", | ||
lmdb_map_size = LMDB_MAP_SIZE, | ||
stream_listen = "127.0.0.1:9011", | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
})) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong(nil, true) | ||
end) | ||
|
||
before_each(function() | ||
client = assert(helpers.admin_client()) | ||
end) | ||
|
||
after_each(function() | ||
if client then | ||
client:close() | ||
end | ||
end) | ||
|
||
it("the maximum safe integer can be accurately represented as a decimal number", function() | ||
local res = assert(client:send { | ||
method = "POST", | ||
path = "/config", | ||
body = { | ||
config = [[ | ||
_format_version: "1.1" | ||
services: | ||
- name: my-service | ||
id: 0855b320-0dd2-547d-891d-601e9b38647f | ||
url: https://localhost | ||
routes: | ||
- name: my-route | ||
id: 481a9539-f49c-51b6-b2e2-fe99ee68866c | ||
paths: | ||
- / | ||
regex_priority: 9007199254740992 | ||
]], | ||
}, | ||
headers = { | ||
["Content-Type"] = "application/json" | ||
} | ||
}) | ||
|
||
assert.response(res).has.status(201) | ||
local res = client:get("/routes/481a9539-f49c-51b6-b2e2-fe99ee68866c") | ||
assert.res_status(200, res) | ||
assert.match_re(res:read_body(), "9007199254740992") | ||
end) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we replace it with
kong.tools.cjson
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt you can, since this is in globalpatches, which gets loaded very early on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't simply replace it, because currently, our global patch doesn't set
cjson.decode_array_with_array_mt(true)
, whereaskong.tools.cjson
does.kong/kong/tools/cjson.lua
Line 4 in b7a8361
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got. The code makes me a bit confusing.