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

tzssangglass 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 7a1287a  fix(admin): inject updatetime when the requst is PATCH with 
sub path (#4765)
7a1287a is described below

commit 7a1287a62aebb49c76c439be52835da940cb0ce0
Author: 罗泽轩 <[email protected]>
AuthorDate: Fri Aug 6 21:47:09 2021 +0800

    fix(admin): inject updatetime when the requst is PATCH with sub path (#4765)
    
    * fix(admin): inject updatetime when the requst is PATCH with sub path
    
    Fix #4763
    
    Signed-off-by: spacewander <[email protected]>
    
    * fix typo error
    
    Signed-off-by: spacewander <[email protected]>
---
 apisix/admin/global_rules.lua  |  4 ++--
 apisix/admin/plugin_config.lua |  4 ++--
 apisix/admin/routes.lua        |  4 ++--
 apisix/admin/services.lua      |  4 ++--
 apisix/admin/ssl.lua           |  5 ++---
 apisix/admin/upstreams.lua     |  4 ++--
 apisix/admin/utils.lua         | 11 +++++++++--
 t/admin/upstream4.t            | 43 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/apisix/admin/global_rules.lua b/apisix/admin/global_rules.lua
index 658ec57..caccc91 100644
--- a/apisix/admin/global_rules.lua
+++ b/apisix/admin/global_rules.lua
@@ -150,14 +150,14 @@ function _M.patch(id, conf, sub_path)
         if code then
             return code, err
         end
+        utils.inject_timestamp(node_value, nil, true)
     else
         node_value = core.table.merge(node_value, conf);
+        utils.inject_timestamp(node_value, nil, conf)
     end
 
     core.log.info("new conf: ", core.json.delay_encode(node_value, true))
 
-    utils.inject_timestamp(node_value, nil, conf)
-
     local ok, err = check_conf(id, node_value, true)
     if not ok then
         return 400, err
diff --git a/apisix/admin/plugin_config.lua b/apisix/admin/plugin_config.lua
index c7f7f44..74302df 100644
--- a/apisix/admin/plugin_config.lua
+++ b/apisix/admin/plugin_config.lua
@@ -148,14 +148,14 @@ function _M.patch(id, conf, sub_path)
         if code then
             return code, err
         end
+        utils.inject_timestamp(node_value, nil, true)
     else
         node_value = core.table.merge(node_value, conf);
+        utils.inject_timestamp(node_value, nil, conf)
     end
 
     core.log.info("new conf: ", core.json.delay_encode(node_value, true))
 
-    utils.inject_timestamp(node_value, nil, conf)
-
     local ok, err = check_conf(id, node_value, true)
     if not ok then
         return 400, err
diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua
index 8be4740..bed0524 100644
--- a/apisix/admin/routes.lua
+++ b/apisix/admin/routes.lua
@@ -283,12 +283,12 @@ function _M.patch(id, conf, sub_path, args)
         if code then
             return code, err
         end
+        utils.inject_timestamp(node_value, nil, true)
     else
         node_value = core.table.merge(node_value, conf);
+        utils.inject_timestamp(node_value, nil, conf)
     end
 
-    utils.inject_timestamp(node_value, nil, conf)
-
     core.log.info("new conf: ", core.json.delay_encode(node_value, true))
 
     local id, err = check_conf(id, node_value, true)
diff --git a/apisix/admin/services.lua b/apisix/admin/services.lua
index e57b551..faef65a 100644
--- a/apisix/admin/services.lua
+++ b/apisix/admin/services.lua
@@ -236,12 +236,12 @@ function _M.patch(id, conf, sub_path)
         if code then
             return code, err
         end
+        utils.inject_timestamp(node_value, nil, true)
     else
         node_value = core.table.merge(node_value, conf);
+        utils.inject_timestamp(node_value, nil, conf)
     end
 
-    utils.inject_timestamp(node_value, nil, conf)
-
     core.log.info("new value ", core.json.delay_encode(node_value, true))
 
     local id, err = check_conf(id, node_value, true)
diff --git a/apisix/admin/ssl.lua b/apisix/admin/ssl.lua
index 5fd3235..fc16e1c 100644
--- a/apisix/admin/ssl.lua
+++ b/apisix/admin/ssl.lua
@@ -203,6 +203,7 @@ function _M.patch(id, conf, sub_path)
         if code then
             return code, err
         end
+        utils.inject_timestamp(node_value, nil, true)
     else
         if conf.key then
             conf.key = apisix_ssl.aes_encrypt_pkey(conf.key)
@@ -215,11 +216,9 @@ function _M.patch(id, conf, sub_path)
         end
 
         node_value = core.table.merge(node_value, conf);
+        utils.inject_timestamp(node_value, nil, conf)
     end
 
-
-    utils.inject_timestamp(node_value, nil, conf)
-
     core.log.info("new ssl conf: ", core.json.delay_encode(node_value, true))
 
     local id, err = check_conf(id, node_value, true)
diff --git a/apisix/admin/upstreams.lua b/apisix/admin/upstreams.lua
index 3d93c75..8180cdb 100644
--- a/apisix/admin/upstreams.lua
+++ b/apisix/admin/upstreams.lua
@@ -204,12 +204,12 @@ function _M.patch(id, conf, sub_path)
         if code then
             return code, err
         end
+        utils.inject_timestamp(new_value, nil, true)
     else
         new_value = core.table.merge(new_value, conf);
+        utils.inject_timestamp(new_value, nil, conf)
     end
 
-    utils.inject_timestamp(new_value, nil, conf)
-
     core.log.info("new value ", core.json.delay_encode(new_value, true))
 
     local id, err = check_conf(id, new_value, true)
diff --git a/apisix/admin/utils.lua b/apisix/admin/utils.lua
index aa7a34f..3ff695a 100644
--- a/apisix/admin/utils.lua
+++ b/apisix/admin/utils.lua
@@ -33,8 +33,15 @@ local function inject_timestamp(conf, prev_conf, patch_conf)
         end
     end
 
-    -- For PATCH request, the modification is passed as 'patch_conf'
-    if not conf.update_time or (patch_conf and patch_conf.update_time == nil) 
then
+    if not conf.update_time or
+        -- For PATCH request, the modification is passed as 'patch_conf'
+        -- If the sub path is used, the 'patch_conf' will be a placeholder 
`true`
+        (patch_conf and (patch_conf == true or patch_conf.update_time == nil))
+    then
+        -- reset the update_time if:
+        -- 1. PATCH request, with sub path
+        -- 2. PATCH request, update_time not given
+        -- 3. Other request, update_time not given
         conf.update_time = ngx_time()
     end
 end
diff --git a/t/admin/upstream4.t b/t/admin/upstream4.t
index 49dbcb1..f3078a1 100644
--- a/t/admin/upstream4.t
+++ b/t/admin/upstream4.t
@@ -560,3 +560,46 @@ passed
     }
 --- response_body
 [delete] code: 200 message: passed
+
+
+
+=== TEST 17: patch upstream with sub_path, the data is number
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+            local etcd = require("apisix.core.etcd")
+            local code, message = t('/apisix/admin/upstreams/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "nodes": {},
+                    "type": "roundrobin"
+                 }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+            local id = 1
+            local res = assert(etcd.get('/upstreams/' .. id))
+            local prev_create_time = res.body.node.value.create_time
+            local prev_update_time = res.body.node.value.update_time
+            ngx.sleep(1)
+
+            local code, message = t('/apisix/admin/upstreams/1/retries',
+                 ngx.HTTP_PATCH,
+                 json.encode(1)
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(message)
+            local res = assert(etcd.get('/upstreams/' .. id))
+            local create_time = res.body.node.value.create_time
+            assert(prev_create_time == create_time, "create_time mismatched")
+            local update_time = res.body.node.value.update_time
+            assert(prev_update_time ~= update_time, "update_time should be 
changed")
+        }
+    }

Reply via email to