From a80a8be42a15d4111236a899bd0d8cf76d5fb3e5 Mon Sep 17 00:00:00 2001 From: suryaparua-official Date: Tue, 12 May 2026 04:43:48 +0000 Subject: [PATCH 1/2] fix(limit-count): correct error() call in gen_limit_key The second argument to Lua's error(msg, level) must be an integer (stack level), not a string. Passing core.json.encode(parent) as the level causes LuaJIT to raise: bad argument #2 to error (number expected, got string) Fix: concatenate the JSON into the message string using '..'. Also add a test case to verify the error message is correct when _meta.parent is missing. Fixes #13357 --- apisix/plugins/limit-count/init.lua | 2 +- t/plugin/limit-count5.t | 42 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/limit-count/init.lua b/apisix/plugins/limit-count/init.lua index ea1d0158e5a1..75d9dcc35356 100644 --- a/apisix/plugins/limit-count/init.lua +++ b/apisix/plugins/limit-count/init.lua @@ -259,7 +259,7 @@ local function gen_limit_key(conf, ctx, key) -- A route which reuses a previous route's ID will inherits its counter. local parent = conf._meta and conf._meta.parent if not parent or not parent.resource_key then - error("failed to generate key invalid parent: ", core.json.encode(parent)) + error("failed to generate key invalid parent: " .. core.json.encode(parent)) end local new_key = parent.resource_key .. ':' .. apisix_plugin.conf_version(conf) diff --git a/t/plugin/limit-count5.t b/t/plugin/limit-count5.t index 15a3e0ba4467..a0d807d52732 100644 --- a/t/plugin/limit-count5.t +++ b/t/plugin/limit-count5.t @@ -307,3 +307,45 @@ peek3: 3 commit2: 0 peek4: 0 commit3: rejected + +=== TEST 9: error() call in gen_limit_key produces correct error message when parent is missing +--- config + location /t { + content_by_lua_block { + local init = require("apisix.plugins.limit-count.init") + local conf = { + policy = "local", + count = 5, + time_window = 60, + key = "remote_addr", + key_type = "var", + rejected_code = 503, + allow_degradation = false, + show_limit_quota_header = true, + _meta = {} + -- _meta.parent is intentionally missing + } + local ctx = { + var = {remote_addr = "127.0.0.1"}, + conf_version = 1, + conf_type = "route" + } + local ok, err = pcall(init.rate_limit, conf, ctx, "limit-count", 1) + if not ok then + -- error message should contain the JSON of parent (null) + if err:find("failed to generate key invalid parent") then + ngx.say("correct error message") + else + ngx.say("wrong error: " .. tostring(err)) + end + else + ngx.say("should have errored") + end + } + } +--- request +GET /t +--- response_body +correct error message +--- no_error_log +[alert] From 082c28572e63f43a2f3b4e098b66b6352d411000 Mon Sep 17 00:00:00 2001 From: suryaparua-official Date: Thu, 14 May 2026 15:18:40 +0000 Subject: [PATCH 2/2] test(limit-count): add three empty lines between tests --- t/plugin/limit-count5.t | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/plugin/limit-count5.t b/t/plugin/limit-count5.t index a0d807d52732..7d07b369c0d2 100644 --- a/t/plugin/limit-count5.t +++ b/t/plugin/limit-count5.t @@ -308,6 +308,8 @@ commit2: 0 peek4: 0 commit3: rejected + + === TEST 9: error() call in gen_limit_key produces correct error message when parent is missing --- config location /t {