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(api) fix output of records in /plugins/schema #4162

Merged
merged 1 commit into from
Jan 7, 2019
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
59 changes: 46 additions & 13 deletions kong/api/routes/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,52 @@ end
-- cjson can encode the schema.
local schema_to_jsonable
do
local function fdata_to_jsonable(fdata)
local insert = table.insert
local ipairs = ipairs
local next = next

local fdata_to_jsonable


local function fields_to_jsonable(fields)
local out = {}
for _, field in ipairs(fields) do
local fname = next(field)
local fdata = field[fname]
insert(out, { [fname] = fdata_to_jsonable(fdata, "no") })
end
setmetatable(out, cjson.array_mt)
return out
end

hishamhm marked this conversation as resolved.
Show resolved Hide resolved

-- Convert field data from schemas into something that can be
-- passed to a JSON encoder.
-- @tparam table fdata A Lua table with field data
-- @tparam string is_array A three-state enum: "yes", "no" or "maybe"
-- @treturn table A JSON-convertible Lua table
fdata_to_jsonable = function(fdata, is_array)
local out = {}
for k, v in pairs(fdata) do
local iter = is_array == "yes" and ipairs or pairs

for k, v in iter(fdata) do
if is_array == "maybe" and type(k) ~= "number" then
is_array = "no"
end

if k == "schema" then
out[k] = schema_to_jsonable(v)

elseif type(v) == "table" then
out[k] = fdata_to_jsonable(v)
if k == "fields" and fdata.type == "record" then
out[k] = fields_to_jsonable(v)

elseif k == "default" and fdata.type == "array" then
out[k] = fdata_to_jsonable(v, "yes")

else
out[k] = fdata_to_jsonable(v, "maybe")
end

elseif type(v) == "number" then
if v ~= v then
Expand All @@ -94,20 +132,15 @@ do
out[k] = v
end
end
if is_array == "yes" or is_array == "maybe" then
setmetatable(out, cjson.array_mt)
end
return out
end

local insert = table.insert
local ipairs = ipairs
local next = next

schema_to_jsonable = function(schema)
local fields = {}
for _, field in ipairs(schema.fields) do
local fname = next(field)
local fdata = field[fname]
insert(fields, { [fname] = fdata_to_jsonable(fdata) })
end
local fields = fields_to_jsonable(schema.fields)
return { fields = fields }
end
end
Expand Down Expand Up @@ -172,7 +205,7 @@ return {

["/plugins/enabled"] = {
GET = function(_, _, helpers)
local enabled_plugins = setmetatable({}, cjson.empty_array_mt)
local enabled_plugins = setmetatable({}, cjson.array_mt)
for k in pairs(singletons.configuration.loaded_plugins) do
enabled_plugins[#enabled_plugins+1] = k
end
Expand Down
11 changes: 11 additions & 0 deletions spec/02-integration/04-admin_api/04-plugins_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(json.fields)
end
end)
it("returns nested records and empty array defaults as arrays", function()
local res = assert(client:send {
method = "GET",
path = "/plugins/schema/request-transformer",
})
local body = assert.res_status(200, res)
assert.match('{"fields":[{', body, 1, true)
assert.not_match('"fields":{', body, 1, true)
assert.match('"default":[]', body, 1, true)
assert.not_match('"default":{}', body, 1, true)
end)
it("returns 404 on invalid plugin", function()
local res = assert(client:send {
method = "GET",
Expand Down