You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/04/15 08:24:25 UTC

[apisix] branch master updated: fix: be compatible with the router created before 2.5 (#4056)

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

spacewander 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 5259b8c  fix: be compatible with the router created before 2.5 (#4056)
5259b8c is described below

commit 5259b8c79601f3b9ac2839bf9c807a653456eda8
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Thu Apr 15 16:24:17 2021 +0800

    fix: be compatible with the router created before 2.5 (#4056)
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/patch.lua              | 24 +++++++++++++++++++++++
 docs/en/latest/admin-api.md   |  2 +-
 docs/zh/latest/admin-api.md   |  2 +-
 t/router/radixtree-uri-vars.t | 45 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/apisix/patch.lua b/apisix/patch.lua
index 850c6b1..6bea363 100644
--- a/apisix/patch.lua
+++ b/apisix/patch.lua
@@ -23,6 +23,7 @@ local ngx_socket = ngx.socket
 local original_tcp = ngx.socket.tcp
 local concat_tab = table.concat
 local new_tab = require("table.new")
+local expr = require("resty.expr.v1")
 local log = ngx.log
 local WARN = ngx.WARN
 local ipairs = ipairs
@@ -227,6 +228,27 @@ local function luasocket_tcp()
 end
 
 
+local patched_expr_new
+do
+    local function eval_empty_rule(self, ctx, ...)
+        return true
+    end
+
+
+    local mt = {__index = {eval = eval_empty_rule}}
+    local old_expr_new = expr.new
+
+
+    function patched_expr_new(rule)
+        if #rule == 0 then
+            return setmetatable({}, mt)
+        end
+
+        return old_expr_new(rule)
+    end
+end
+
+
 function _M.patch()
     -- make linter happy
     -- luacheck: ignore
@@ -238,6 +260,8 @@ function _M.patch()
 
         return luasocket_tcp()
     end
+
+    expr.new = patched_expr_new
 end
 
 
diff --git a/docs/en/latest/admin-api.md b/docs/en/latest/admin-api.md
index d48079b..0719df4 100644
--- a/docs/en/latest/admin-api.md
+++ b/docs/en/latest/admin-api.md
@@ -112,7 +112,7 @@ Config Example:
     "name": "route-xxx",
     "desc": "hello world",
     "remote_addrs": ["127.0.0.1"], # A set of Client IP.
-    "vars": [],                 # A list of one or more `{var, operator, val}` elements
+    "vars": [["http_user", "==", "ios"]], # A list of one or more `[var, operator, val]` elements
     "upstream_id": "1",         # upstream id, recommended
     "upstream": {},             # upstream, not recommended
     "filter_func": "",          # User-defined filtering function
diff --git a/docs/zh/latest/admin-api.md b/docs/zh/latest/admin-api.md
index d660976..c731537 100644
--- a/docs/zh/latest/admin-api.md
+++ b/docs/zh/latest/admin-api.md
@@ -107,7 +107,7 @@ route 对象 json 配置内容:
     "name": "路由xxx",
     "desc": "hello world",
     "remote_addrs": ["127.0.0.1"],  # 一组客户端请求 IP 地址
-    "vars": [],                 # 由一个或多个 {var, operator, val} 元素组成的列表
+    "vars": [["http_user", "==", "ios"]], # 由一个或多个 [var, operator, val] 元素组成的列表
     "upstream_id": "1",         # upstream 对象在 etcd 中的 id ,建议使用此值
     "upstream": {},             # upstream 信息对象,建议尽量不要使用
     "filter_func": "",          # 用户自定义的过滤函数,非必填
diff --git a/t/router/radixtree-uri-vars.t b/t/router/radixtree-uri-vars.t
index ea2f79a..32d24b2 100644
--- a/t/router/radixtree-uri-vars.t
+++ b/t/router/radixtree-uri-vars.t
@@ -383,3 +383,48 @@ demo: prod
 --- error_code: 404
 --- no_error_log
 [error]
+
+
+
+=== TEST 19: be compatible with empty vars
+--- 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,
+                 [=[{
+                        "methods": ["GET"],
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello",
+                        "vars": []
+                }]=]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 20: hit
+--- request
+GET /hello
+--- response_body
+hello world
+--- no_error_log
+[error]