diff --git a/changelog/unreleased/kong/fix-parse-nested-parameters.yml b/changelog/unreleased/kong/fix-parse-nested-parameters.yml new file mode 100644 index 000000000000..e1cdd3fd21e9 --- /dev/null +++ b/changelog/unreleased/kong/fix-parse-nested-parameters.yml @@ -0,0 +1,3 @@ +message: Fixed an issue where nested parameters can not be parsed correctly when using form-urlencoded requests. +type: bugfix +scope: Admin API diff --git a/kong/tools/http.lua b/kong/tools/http.lua index 58e5bb908b5e..344c2a6f3f21 100644 --- a/kong/tools/http.lua +++ b/kong/tools/http.lua @@ -141,6 +141,8 @@ do end + -- { ["1"] = "a", ["2"] = "b" } becomes {"a", "b"} + -- { "a", "b" } becomes { "a", "b" } local function decode_array(t) local keys = {} local len = 0 @@ -160,7 +162,7 @@ do if keys[i] ~= i then return nil end - new_t[i] = t[tostring(i)] + new_t[i] = t[tostring(i)] or t[i] end return new_t diff --git a/spec/02-integration/04-admin_api/13-plugin-endpoints_spec.lua b/spec/02-integration/04-admin_api/13-plugin-endpoints_spec.lua index 24a4c7af8dd1..bdd637ffde4c 100644 --- a/spec/02-integration/04-admin_api/13-plugin-endpoints_spec.lua +++ b/spec/02-integration/04-admin_api/13-plugin-endpoints_spec.lua @@ -38,9 +38,23 @@ describe("Admin API endpoints added via plugins #" .. strategy, function() path = "/method_without_exit", headers = { ["Content-Type"] = "application/json" } }) - local body = assert.res_status(201 , res) + local body = assert.res_status(201, res) assert.same("hello", body) end) + + it("nested parameters can be parsed correctly when using form-urlencoded requests", function() + local res = assert(client:send{ + method = "POST", + path = "/parsed_params", + body = ngx.encode_args{ + ["items[1]"] = "foo", + ["items[2]"] = "bar", + }, + headers = { ["Content-Type"] = "application/x-www-form-urlencoded" } + }) + local body = assert.res_status(200, res) + assert.same('{"items":["foo","bar"]}', body) + end) end) end diff --git a/spec/fixtures/custom_plugins/kong/plugins/admin-api-method/api.lua b/spec/fixtures/custom_plugins/kong/plugins/admin-api-method/api.lua index 73c42b11cf9c..cec0e17eb297 100644 --- a/spec/fixtures/custom_plugins/kong/plugins/admin-api-method/api.lua +++ b/spec/fixtures/custom_plugins/kong/plugins/admin-api-method/api.lua @@ -6,4 +6,14 @@ return { ngx.print("hello") end, }, + ["/parsed_params"] = { + -- The purpose of the dummy filter is to let `parse_params` + -- of api/api_helpers.lua to be called twice. + before = function(self, db, helpers, parent) + end, + + POST = function(self, db, helpers, parent) + kong.response.exit(200, self.params) + end, + }, }