From 17e875539d55900cebc3edbe0845075aefd4b9be Mon Sep 17 00:00:00 2001 From: Aapo Talvensaari Date: Fri, 14 Sep 2018 10:20:18 -0700 Subject: [PATCH] fix(admin) inferring of arguments for record types --- kong/api/arguments.lua | 12 +++++++++++- spec/01-unit/000-new-dao/03-arguments_spec.lua | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/kong/api/arguments.lua b/kong/api/arguments.lua index 7b103e9b46c..131032beae9 100644 --- a/kong/api/arguments.lua +++ b/kong/api/arguments.lua @@ -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 @@ -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 diff --git a/spec/01-unit/000-new-dao/03-arguments_spec.lua b/spec/01-unit/000-new-dao/03-arguments_spec.lua index ae181e5c8cc..bf7cf5a0206 100644 --- a/spec/01-unit/000-new-dao/03-arguments_spec.lua +++ b/spec/01-unit/000-new-dao/03-arguments_spec.lua @@ -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()