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

ashishtiwari 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 ee6a04cad feat: allow workflow config without case (#11787)
ee6a04cad is described below

commit ee6a04cad1bab24ae0305a3cd66bb4e88812edb7
Author: Ashish Tiwari <[email protected]>
AuthorDate: Fri Nov 29 15:47:07 2024 +0530

    feat: allow workflow config without case (#11787)
---
 apisix/plugins/workflow.lua        | 18 ++++----
 docs/en/latest/plugins/workflow.md |  3 +-
 t/plugin/workflow-without-case.t   | 85 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 8 deletions(-)

diff --git a/apisix/plugins/workflow.lua b/apisix/plugins/workflow.lua
index 73d68375d..bf5221dd1 100644
--- a/apisix/plugins/workflow.lua
+++ b/apisix/plugins/workflow.lua
@@ -49,7 +49,7 @@ local schema = {
                         }
                     }
                 },
-                required = {"case", "actions"}
+                required = {"actions"}
             }
         }
     },
@@ -117,9 +117,11 @@ function _M.check_schema(conf)
     end
 
     for idx, rule in ipairs(conf.rules) do
-        local ok, err = expr.new(rule.case)
-        if not ok then
-            return false, "failed to validate the 'case' expression: " .. err
+         if rule.case then
+            local ok, err = expr.new(rule.case)
+            if not ok then
+                return false, "failed to validate the 'case' expression: " .. 
err
+            end
         end
 
         local actions = rule.actions
@@ -143,10 +145,12 @@ end
 
 
 function _M.access(conf, ctx)
-    local match_result
     for _, rule in ipairs(conf.rules) do
-        local expr, _ = expr.new(rule.case)
-        match_result = expr:eval(ctx.var)
+        local match_result = true
+        if rule.case then
+            local expr, _ = expr.new(rule.case)
+            match_result = expr:eval(ctx.var)
+        end
         if match_result then
             -- only one action is currently supported
             local action = rule.actions[1]
diff --git a/docs/en/latest/plugins/workflow.md 
b/docs/en/latest/plugins/workflow.md
index 2e2fdace1..99fd8fd75 100644
--- a/docs/en/latest/plugins/workflow.md
+++ b/docs/en/latest/plugins/workflow.md
@@ -36,7 +36,7 @@ The `workflow` plugin is used to introduce 
[lua-resty-expr](https://github.com/a
 
 | Name                         | Type          | Required | Default | Valid 
values | Description                                                  |
 | ---------------------------- | ------------- | -------- | ------- | 
------------ | ------------------------------------------------------------ |
-| rules.case                   | array[array]  | True     |         |          
    | List of variables to match for filtering requests for conditional traffic 
split. It is in the format `{variable operator value}`. For example, 
`{"arg_name", "==", "json"}`. The variables here are consistent with NGINX 
internal variables. For details on supported operators, you can refer to 
[lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list). |
+| rules.case                   | array[array]  | False     |         |         
     | List of variables to match for filtering requests for conditional 
traffic split. It is in the format `{variable operator value}`. For example, 
`{"arg_name", "==", "json"}`. The variables here are consistent with NGINX 
internal variables. For details on supported operators, you can refer to 
[lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list). |
 | rules.actions                | array[object] | True     |         |          
    | The action to be performed when the case matches successfully. Currently, 
only one element is supported in actions. The first child element of the 
actions' only element can be `return` or `limit-count`. |
 
 ### `actions` Attributes
@@ -58,6 +58,7 @@ The `workflow` plugin is used to introduce 
[lua-resty-expr](https://github.com/a
 :::note
 
 In `rules`, match `case` in order according to the index of the `rules`, and 
execute `actions` directly if `case` match.
+If `case` is missing, the default behavior is to match.
 
 :::
 
diff --git a/t/plugin/workflow-without-case.t b/t/plugin/workflow-without-case.t
new file mode 100644
index 000000000..2ce469a7f
--- /dev/null
+++ b/t/plugin/workflow-without-case.t
@@ -0,0 +1,85 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+
+__DATA__
+
+=== TEST 1: set plugin
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "workflow": {
+                                "rules": [
+                                    {
+                                        "actions": [
+                                            [
+                                                "return",
+                                                {
+                                                    "code": 403
+                                                }
+                                            ]
+                                        ]
+                                    }
+                                ]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 2: trigger workflow
+--- request
+GET /hello
+--- error_code: 403

Reply via email to