From e3337af95c418b51a2a2f6225728ddf5ae03c531 Mon Sep 17 00:00:00 2001 From: xumin Date: Wed, 10 Jul 2024 17:50:34 +0800 Subject: [PATCH] fix(plugins): Request-Transformer rename behavior Fix KAG-4915 --- .../unreleased/kong/req-trans-rename.yml | 3 +++ kong/plugins/request-transformer/access.lua | 22 ++++++++++------ spec/01-unit/31-kong_cache_spec.lua | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 changelog/unreleased/kong/req-trans-rename.yml create mode 100644 spec/01-unit/31-kong_cache_spec.lua diff --git a/changelog/unreleased/kong/req-trans-rename.yml b/changelog/unreleased/kong/req-trans-rename.yml new file mode 100644 index 0000000000000..a9474d2f10fc9 --- /dev/null +++ b/changelog/unreleased/kong/req-trans-rename.yml @@ -0,0 +1,3 @@ +message: "**Request-Transformer**: Fixed an issue where renamed query parameters, url-encoded body parameters, and json body parameters were not handled properly when target name is the same as the source name in the request." +type: feature +scope: Plugin diff --git a/kong/plugins/request-transformer/access.lua b/kong/plugins/request-transformer/access.lua index f2b66457809f5..8b8a301e6a50b 100644 --- a/kong/plugins/request-transformer/access.lua +++ b/kong/plugins/request-transformer/access.lua @@ -231,8 +231,10 @@ local function transform_querystrings(conf, template_env) -- Rename querystring(s) for _, old_name, new_name in iter(conf.rename.querystring, template_env) do local value = querystring[old_name] - querystring[new_name] = value - querystring[old_name] = nil + if value then + querystring[old_name] = nil + querystring[new_name] = value + end end for _, name, value in iter(conf.replace.querystring, template_env) do @@ -276,9 +278,11 @@ local function transform_json_body(conf, body, content_length, template_env) if content_length > 0 and #conf.rename.body > 0 then for _, old_name, new_name in iter(conf.rename.body, template_env) do local value = parameters[old_name] - parameters[new_name] = value - parameters[old_name] = nil - renamed = true + if value then + parameters[old_name] = nil + parameters[new_name] = value + renamed = true + end end end @@ -327,9 +331,11 @@ local function transform_url_encoded_body(conf, body, content_length, template_e if content_length > 0 and #conf.rename.body > 0 then for _, old_name, new_name in iter(conf.rename.body, template_env) do local value = parameters[old_name] - parameters[new_name] = value - parameters[old_name] = nil - renamed = true + if value then + parameters[old_name] = nil + parameters[new_name] = value + renamed = true + end end end diff --git a/spec/01-unit/31-kong_cache_spec.lua b/spec/01-unit/31-kong_cache_spec.lua new file mode 100644 index 0000000000000..688191c6b63b4 --- /dev/null +++ b/spec/01-unit/31-kong_cache_spec.lua @@ -0,0 +1,25 @@ +local cache = require "kong.cache" + +local helpers = require "spec.helpers" + +local REDIS_HOST = helpers.redis_host +local REDIS_PORT = helpers.redis_port + +describe("kong.cache", function() + describe("redis strategy", function() + lazy_setup(function() + cache.new("my_cache", { + shm_name = "my_cache", + cluster_events = cluster_events, + worker_events = worker_events, + ttl = db_cache_ttl, + neg_ttl = db_cache_neg_ttl or db_cache_ttl, + resurrect_ttl = kong_config.resurrect_ttl, + page = page, + cache_pages = cache_pages, + resty_lock_opts = LOCK_OPTS, + lru_size = get_lru_size(kong_config), + }) + end) + end) +end \ No newline at end of file