This is an automated email from the ASF dual-hosted git repository.

moonming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 96d333f7ad fix: run Consumer rewrite plugins when they override group 
plugins (#14240)
96d333f7ad is described below

commit 96d333f7ad26dcdad0e1b4fe6cc602cbf8869eb6
Author: AlinsRan <[email protected]>
AuthorDate: Tue Nov 24 15:49:39 2026 +0800

    fix: run Consumer rewrite plugins when they override group plugins (#14240)
---
 apisix/init.lua                       |  9 +++---
 apisix/plugin.lua                     | 43 ++++++++-------------------
 docs/en/latest/terminology/plugin.md  |  2 +-
 docs/zh/latest/terminology/plugin.md  |  2 +-
 t/config-center-yaml/consumer-group.t | 55 ++++++++++++++++++++++++++++++++++-
 5 files changed, 73 insertions(+), 38 deletions(-)

diff --git a/apisix/init.lua b/apisix/init.lua
index a2e98cfe5c..0bfda5310c 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -1082,6 +1082,7 @@ function _M.http_access_phase()
                 end
             end
 
+            local route_plugins = route.value.plugins
             route, changed = plugin.merge_consumer_route(
                 route,
                 api_ctx.consumer,
@@ -1095,10 +1096,10 @@ function _M.http_access_phase()
             if changed then
                 api_ctx.matched_route = route
                 core.table.clear(api_ctx.plugins)
-                local phase = "rewrite_in_consumer"
-                api_ctx.plugins = plugin.filter(api_ctx, route, 
api_ctx.plugins, nil, phase)
-                -- rerun rewrite phase for newly added plugins in consumer
-                plugin.run_plugin(phase, api_ctx.plugins, api_ctx)
+                api_ctx.plugins = plugin.filter(api_ctx, route, 
api_ctx.plugins)
+                api_ctx.plugins_before_consumer_merge = route_plugins
+                plugin.run_plugin("rewrite_in_consumer", api_ctx.plugins, 
api_ctx)
+                api_ctx.plugins_before_consumer_merge = nil
             end
         end
         plugin.run_global_rules(api_ctx, global_rules, conf_version, "access")
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index d2188aef17..bca8fdcba2 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -572,7 +572,7 @@ local function meta_filter(ctx, plugin_name, plugin_conf)
 end
 
 
-function _M.filter(ctx, conf, plugins, route_conf, phase)
+function _M.filter(ctx, conf, plugins, route_conf)
     local user_plugin_conf = conf.value.plugins
     if user_plugin_conf == nil or
        core.table.nkeys(user_plugin_conf) == 0 then
@@ -604,16 +604,6 @@ function _M.filter(ctx, conf, plugins, route_conf, phase)
             end
         end
 
-        -- in the rewrite phase, the plugin executes in the following order:
-        -- 1. execute the rewrite phase of the plugins on route(including the 
auth plugins)
-        -- 2. merge plugins from consumer and route
-        -- 3. execute the rewrite phase of the plugins on consumer(phase: 
rewrite_in_consumer)
-        -- in this case, we need to skip the plugins that was already 
executed(step 1)
-        if phase == "rewrite_in_consumer"
-                and (not plugin_conf._from_consumer or plugin_obj.type == 
"auth") then
-            plugin_conf._skip_rewrite_in_consumer = true
-        end
-
         if plugin_conf._meta and plugin_conf._meta.priority then
             custom_sort = true
         end
@@ -838,30 +828,18 @@ local function merge_consumer_route(route_conf, 
consumer_conf, consumer_group_co
     local new_route_conf = core.table.deepcopy(route_conf,
         { shallow_prefix = "self.value.plugins" })
 
+    local plugins = new_route_conf.value.plugins or {}
+    new_route_conf.value.plugins = plugins
+
     if has_group_plugins then
         for name, conf in pairs(consumer_group_conf.value.plugins) do
-            if not new_route_conf.value.plugins then
-                new_route_conf.value.plugins = {}
-            end
-
-            if new_route_conf.value.plugins[name] == nil then
-                conf._from_consumer = true
-            end
-            new_route_conf.value.plugins[name] = conf
+            plugins[name] = conf
         end
     end
 
-
     if has_consumer_plugins then
         for name, conf in pairs(consumer_conf.plugins) do
-            if not new_route_conf.value.plugins then
-                new_route_conf.value.plugins = {}
-            end
-
-            if new_route_conf.value.plugins[name] == nil then
-                conf._from_consumer = true
-            end
-            new_route_conf.value.plugins[name] = conf
+            plugins[name] = conf
         end
     end
 
@@ -1467,10 +1445,13 @@ function _M.run_plugin(phase, plugins, api_ctx)
     then
         -- in the "rewrite_in_consumer" phase, the executed functions
         -- are the "rewrite" ones
-        local exec_phase = phase == "rewrite_in_consumer" and "rewrite" or 
phase
+        local consumer_rewrite = phase == "rewrite_in_consumer"
+        local exec_phase = consumer_rewrite and "rewrite" or phase
+        local route_plugins = api_ctx.plugins_before_consumer_merge
         for i = 1, #plugins, 2 do
-
-            if phase == "rewrite_in_consumer" and plugins[i + 
1]._skip_rewrite_in_consumer then
+            -- Only run rewrite for non-auth plugins added by the Consumer or 
its group.
+            if consumer_rewrite and (plugins[i].type == "auth"
+                    or (route_plugins and route_plugins[plugins[i].name])) then
                 goto CONTINUE
             end
 
diff --git a/docs/en/latest/terminology/plugin.md 
b/docs/en/latest/terminology/plugin.md
index 6e6d7811db..e543cb608b 100644
--- a/docs/en/latest/terminology/plugin.md
+++ b/docs/en/latest/terminology/plugin.md
@@ -207,7 +207,7 @@ The above configuration means setting the priority of the 
serverless-pre-functio
 :::note
 
 - Custom plugin priority only affects the current object(route, service ...) 
of the plugin instance binding, not all instances of that plugin. For example, 
if the above plugin configuration belongs to Route A, the order of execution of 
the plugins serverless-post-function and serverless-post-function on Route B 
will not be affected and the default priority will be used.
-- Custom plugin priority does not apply to the rewrite phase of some plugins 
configured on the consumer. The rewrite phase of plugins configured on the 
route will be executed first, and then the rewrite phase of plugins (exclude 
auth plugins) from the consumer will be executed.
+- Route plugins run their `rewrite` handlers first. After APISIX identifies 
the Consumer, it runs `rewrite` for non-authentication plugins added by the 
Consumer or its Consumer Group. If both configure the same plugin, APISIX uses 
the Consumer configuration. Plugins already configured on the effective route 
skip this second pass, regardless of custom priority.
 
 :::
 
diff --git a/docs/zh/latest/terminology/plugin.md 
b/docs/zh/latest/terminology/plugin.md
index 4ebd0c3da4..5f1f214eab 100644
--- a/docs/zh/latest/terminology/plugin.md
+++ b/docs/zh/latest/terminology/plugin.md
@@ -204,7 +204,7 @@ plugins:
 :::note 注意
 
 - 自定义插件优先级只会影响插件实例绑定的主体,不会影响该插件的所有实例。比如上面的插件配置属于路由 A,路由 B 上的插件 
`serverless-post-function` 和 `serverless-post-function` 插件执行顺序不会受到影响,会使用默认优先级。
-- 自定义插件优先级不适用于 Consumer 上配置的插件的 `rewrite` 阶段。路由上配置的插件的 `rewrite` 
阶段将会优先运行,然后才会运行 Consumer 上除 `auth` 类插件之外的其他插件的 `rewrite` 阶段。
+- 路由插件先执行 `rewrite`。识别 Consumer 后,APISIX 再执行 Consumer 或 Consumer Group 
新增的非认证插件的 `rewrite`。两者配置同名插件时,使用 Consumer 
的配置。已在生效路由中配置的插件会跳过第二轮执行,自定义优先级不会改变这一顺序。
 
 :::
 
diff --git a/t/config-center-yaml/consumer-group.t 
b/t/config-center-yaml/consumer-group.t
index ff6f6a64cf..6001347979 100644
--- a/t/config-center-yaml/consumer-group.t
+++ b/t/config-center-yaml/consumer-group.t
@@ -35,7 +35,7 @@ _EOC_
 
     $block->set_value("yaml_config", $yaml_config);
 
-    my $routes = <<_EOC_;
+    my $routes = $block->routes // <<_EOC_;
 routes:
   -
     uri: /hello
@@ -138,3 +138,56 @@ consumers:
 --- error_log
 failed to check the configuration of plugin example-plugin
 failed to fetch consumer group config by id: foobar
+
+
+
+=== TEST 5: Consumer rewrite overrides the group once on routes without the 
plugin
+--- apisix_yaml
+consumer_groups:
+  - id: rewrite-group
+    plugins:
+      proxy-rewrite:
+        headers:
+          add: {X-Rewrite: group}
+consumers:
+  - username: member
+    group_id: rewrite-group
+    plugins:
+      key-auth: {key: member}
+      proxy-rewrite:
+        headers:
+          add: {X-Rewrite: consumer}
+  - username: group-member
+    group_id: rewrite-group
+    plugins:
+      key-auth: {key: group-member}
+upstreams:
+  - id: rewrite-backend
+    type: roundrobin
+    nodes:
+      "127.0.0.1:1980": 1
+--- routes
+routes:
+  - id: consumer-only-rewrite
+    uri: /rewrite-consumer
+    upstream_id: rewrite-backend
+    plugins:
+      key-auth: {}
+  - id: route-rewrite
+    uri: /rewrite-route
+    upstream_id: rewrite-backend
+    plugins:
+      key-auth: {}
+      proxy-rewrite:
+        headers:
+          add: {X-Rewrite: route}
+#END
+--- upstream_server_config
+    location ~ ^/rewrite- {
+        echo $http_x_rewrite;
+    }
+--- request eval
+["GET /rewrite-consumer?apikey=group-member", "GET 
/rewrite-consumer?apikey=member",
+ "GET /rewrite-route?apikey=member", "GET /rewrite-consumer?apikey=member"]
+--- response_body eval
+["group\n", "consumer\n", "route\n", "consumer\n"]

Reply via email to