Skip to content

Commit

Permalink
fix(db): ensure that flattened_errors is encoded as a JSON array (#14019
Browse files Browse the repository at this point in the history
)
  • Loading branch information
flrgh authored Dec 18, 2024
1 parent 33a43c7 commit f7f7a23
Show file tree
Hide file tree
Showing 4 changed files with 2,677 additions and 4 deletions.
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

1 comment on commit f7f7a23

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:f7f7a2349ecc96b505a6f3dac7f4e8fae84d004f
Artifacts available https://github.com/Kong/kong/actions/runs/12395345399

Please sign in to comment.