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(db): ensure that flattened_errors is encoded as a JSON array #14019

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-error-flattening-json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Fixed an issue where `POST /config?flatten_errors=1` could return a JSON object instead of an empty array."
type: bugfix
scope: Core
6 changes: 4 additions & 2 deletions kong/db/errors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local pl_keys = require("pl.tablex").keys
local nkeys = require("table.nkeys")
local table_isarray = require("table.isarray")
local uuid = require("kong.tools.uuid")
local json = require("kong.tools.cjson")


local type = type
Expand All @@ -21,6 +22,7 @@ local concat = table.concat
local sort = table.sort
local insert = table.insert
local remove = table.remove
local new_array = json.new_array


local sorted_keys = function(tbl)
Expand Down Expand Up @@ -720,7 +722,7 @@ do
---@param ns? string
---@param flattened? table
local function categorize_errors(errs, ns, flattened)
flattened = flattened or {}
flattened = flattened or new_array()

for field, err in drain(errs) do
local errtype = type(err)
Expand Down Expand Up @@ -1020,7 +1022,7 @@ do
---@param input table
---@return table
function flatten_errors(input, err_t)
local flattened = {}
local flattened = new_array()

for entity_type, section_errors in drain(err_t) do
if type(section_errors) ~= "table" then
Expand Down
19 changes: 17 additions & 2 deletions kong/tools/cjson.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local cjson = require "cjson.safe".new()
local CJSON_MAX_PRECISION = require "kong.constants".CJSON_MAX_PRECISION
local new_tab = require("table.new")

local setmetatable = setmetatable
local array_mt = cjson.array_mt

cjson.decode_array_with_array_mt(true)
cjson.encode_sparse_array(nil, nil, 2^15)
Expand All @@ -14,7 +17,19 @@ _M.encode = cjson.encode
_M.decode_with_array_mt = cjson.decode


_M.array_mt = cjson.array_mt

_M.array_mt = array_mt

--- Creates a new table with the cjson array metatable.
---
--- This ensures that the table will be encoded as a JSON array, even if it
--- is empty.
---
---@param size? integer
---@return table
function _M.new_array(size)
local t = size and new_tab(size, 0) or {}
setmetatable(t, array_mt)
return t
end

return _M
Loading
Loading