Skip to content

Commit

Permalink
fix(admin) inferring of arguments for record types
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Sep 15, 2018
1 parent 99c81b4 commit 17e8755
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion kong/api/arguments.lua
Expand Up @@ -13,6 +13,7 @@ local pairs = pairs
local lower = string.lower
local find = string.find
local sub = string.sub
local next = next
local type = type
local ngx = ngx
local req = ngx.req
Expand Down Expand Up @@ -213,7 +214,16 @@ local function infer_value(value, field)
elseif field.type == "record" and not field.abstract then
if type(value) == "table" then
for k, v in pairs(value) do
value[k] = infer_value(v, field.fields[k])
for i in ipairs(field.fields) do
local item = field.fields[i]
if item then
local key = next(item)
local fld = item[key]
if k == key then
value[k] = infer_value(v, fld)
end
end
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/01-unit/000-new-dao/03-arguments_spec.lua
Expand Up @@ -56,9 +56,9 @@ describe("arguments.infer_value", function()

it("infers records", function()
assert.same({ age = "1" }, infer_value({ age = "1" },
{ type = "record", fields = { age = { type = "string" } } }))
{ type = "record", fields = {{ age = { type = "string" } } }}))
assert.same({ age = 1 }, infer_value({ age = "1" },
{ type = "record", fields = { age = { type = "number" } } }))
{ type = "record", fields = {{ age = { type = "number" } } }}))
end)

it("returns the provided value when inferring is not possible", function()
Expand Down

0 comments on commit 17e8755

Please sign in to comment.