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

shreemaanabhishek 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 6f73f7d7c feat: support set default value in resolve_var (#12963)
6f73f7d7c is described below

commit 6f73f7d7cb4ba6bc75080e529af9464f4fae1753
Author: Shreemaan Abhishek <[email protected]>
AuthorDate: Wed Feb 4 10:31:03 2026 +0545

    feat: support set default value in resolve_var (#12963)
    
    Signed-off-by: Nic <[email protected]>
    Co-authored-by: Nic <[email protected]>
---
 apisix/core/utils.lua | 12 +++++++++---
 t/core/utils.t        | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua
index f8865dedf..24df8abe6 100644
--- a/apisix/core/utils.lua
+++ b/apisix/core/utils.lua
@@ -296,15 +296,21 @@ local resolve_var
 do
     local _ctx
     local n_resolved
-    local pat = [[(?<!\\)\$(\{([\w\.]+)\}|([\w\.]+))]]
+    local pat = [[(?<!\\)\$(\{\s*([^}]+?)\s*\}|([\w\.]+))]]
     local _escaper
 
     local function resolve(m)
         local variable = m[2] or m[3]
-        local v = _ctx[variable]
+        -- handle the default value with ?? operator
+        local segs, err = ngx_re.split(variable, [[\s*\?\?\s*]])
+        if not segs then
+            log.error("failed to split variable ", variable, ": ", err)
+            return ""
+        end
+        local v = _ctx[segs[1]]
 
         if v == nil then
-            return ""
+            return segs[2] or ""
         end
         n_resolved = n_resolved + 1
         if _escaper then
diff --git a/t/core/utils.t b/t/core/utils.t
index 28fa537f3..638643ff1 100644
--- a/t/core/utils.t
+++ b/t/core/utils.t
@@ -215,6 +215,16 @@ close: 1 nil}
                 "${you}_${me}",
                 "${you}${me}",
                 "${you}$me",
+                "${you??}$me",
+                "${you??Rose}$me",
+                "${she??Rose}$me",
+                "${she ??Rose}$me",
+                "${she?? Rose}$me",
+                "${she ?? Rose}$me",
+                "${she   ??     Rose}$me",
+                "${ she ?? Rose }$me",
+                "${you ?? Rose}$he??",
+                "${you ?? Rose}$he??Jack",
             }
             local ctx = {
                 you = "John",
@@ -241,6 +251,16 @@ res:John and \$me
 res:John_David
 res:JohnDavid
 res:JohnDavid
+res:JohnDavid
+res:JohnDavid
+res:RoseDavid
+res:RoseDavid
+res:RoseDavid
+res:RoseDavid
+res:RoseDavid
+res:RoseDavid
+res:John??
+res:John??Jack
 
 
 

Reply via email to