Skip to content

Commit

Permalink
fix(api) infer types of shorthand fields when using form encoded
Browse files Browse the repository at this point in the history
Fixes #6352.
  • Loading branch information
hishamhm authored and bungle committed Sep 22, 2020
1 parent 4ce5620 commit fe00a8d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
22 changes: 22 additions & 0 deletions kong/api/arguments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ local function infer_value(value, field)
end
end
end

if field.shorthand_fields then
for _, item in ipairs(field.shorthand_fields) do
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 All @@ -248,6 +258,18 @@ infer = function(args, schema)
end
end

if schema.shorthand_fields then
for _, v in ipairs(schema.shorthand_fields) do
local field_name = next(v)
local field = v[field_name]

local value = args[field_name]
if value then
args[field_name] = infer_value(value, field)
end
end
end

if schema.ttl == true and args.ttl then
args.ttl = tonumber(args.ttl) or args.ttl
end
Expand Down
27 changes: 27 additions & 0 deletions spec/01-unit/01-db/03-arguments_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ describe("arguments.infer", function()
comments = ngx.null
}, infer(args, schema))
end)

it("infers shorthand_fields but does not run the func", function()
local schema = Schema.new({
fields = {
{ name = { type = "string" } },
{ another_array = { type = "array", elements = { type = { "string" } } } },
},
shorthand_fields = {
{ an_array = {
type = "array",
elements = { type = { "string" } },
func = function(value)
return { another_array = value:upper() }
end,
}
},
}
})

local args = { name = "peter",
an_array = "something" }
assert.same({
name = "peter",
an_array = { "something" },
}, infer(args, schema))
end)

end)

describe("arguments.combine", function()
Expand Down

0 comments on commit fe00a8d

Please sign in to comment.