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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8bfdc9d413 fix(rewrite): look up rewrite metadata with an empty prefix 
when context-path is absent (#7796) (#7798)
8bfdc9d413 is described below

commit 8bfdc9d4135756751ff05ef854e116155080dc77
Author: konlue <[email protected]>
AuthorDate: Mon Oct 19 09:32:00 2026 +0800

    fix(rewrite): look up rewrite metadata with an empty prefix when 
context-path is absent (#7796) (#7798)
---
 .../shenyu/plugin/rewrite/RewritePlugin.java       |  5 +++--
 .../shenyu/plugin/rewrite/RewritePluginTest.java   | 24 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
 
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
index 398443bd0e..51c543b008 100644
--- 
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
@@ -81,8 +81,9 @@ public class RewritePlugin extends AbstractShenyuPlugin {
             Map<String, Object> attributes = exchange.getAttributes();
             if 
(Optional.ofNullable(rewriteHandle.getRewriteMetaData()).orElse(false)) {
                 // when the rewritten uri crosses plugins, this is necessary
-                final String contextPath = Optional.ofNullable((String) 
exchange.getAttribute(Constants.REWRITE_CONTEXT_PATH))
-                        .orElseGet(() -> 
exchange.getAttribute(Constants.CONTEXT_PATH));
+                // an absent context path must contribute an empty prefix, or 
the lookup key starts with the literal "null"
+                final String contextPath = 
StringUtils.defaultString(Optional.ofNullable((String) 
exchange.getAttribute(Constants.REWRITE_CONTEXT_PATH))
+                        .orElseGet(() -> 
exchange.getAttribute(Constants.CONTEXT_PATH)));
                 MetaData metaData = 
MetaDataCache.getInstance().obtain(contextPath + rewriteUri);
                 Optional.ofNullable(exchange.getAttribute(Constants.META_DATA))
                         .ifPresent(metadata -> 
attributes.put(Constants.OLD_CONTEXT_PATH_META_DATA, metadata));
diff --git 
a/shenyu-plugin/shenyu-plugin-rewrite/src/test/java/org/apache/shenyu/plugin/rewrite/RewritePluginTest.java
 
b/shenyu-plugin/shenyu-plugin-rewrite/src/test/java/org/apache/shenyu/plugin/rewrite/RewritePluginTest.java
index 5b5ec80bfc..d910bdd264 100644
--- 
a/shenyu-plugin/shenyu-plugin-rewrite/src/test/java/org/apache/shenyu/plugin/rewrite/RewritePluginTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-rewrite/src/test/java/org/apache/shenyu/plugin/rewrite/RewritePluginTest.java
@@ -19,12 +19,14 @@ package org.apache.shenyu.plugin.rewrite;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.dto.MetaData;
 import org.apache.shenyu.common.dto.RuleData;
 import org.apache.shenyu.common.dto.SelectorData;
 import org.apache.shenyu.common.dto.convert.rule.RewriteHandle;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.plugin.api.ShenyuPluginChain;
 import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.base.cache.MetaDataCache;
 import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
 import org.apache.shenyu.plugin.rewrite.handler.RewritePluginDataHandler;
 import org.junit.jupiter.api.Test;
@@ -88,6 +90,28 @@ public final class RewritePluginTest {
         assertEquals("/shenyu/rewrite", 
exchange.getAttributes().get(Constants.REWRITE_URI));
     }
 
+    @Test
+    public void shouldLookUpMetaDataWithoutContextPath() {
+        RuleData data = new RuleData();
+        
data.setHandle("{\"regex\":\"/shenyu/{id}\",\"replace\":\"/target/{id}\",\"rewriteMetaData\":true}");
+        RewriteHandle rewriteHandle = 
GsonUtils.getGson().fromJson(data.getHandle(), RewriteHandle.class);
+        
RewritePluginDataHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(data),
 rewriteHandle);
+
+        // no context-path plugin in play: neither rewrite nor global context 
path attribute is set
+        final ServerWebExchange rewriteExchange = 
MockServerWebExchange.from(MockServerHttpRequest.get("/shenyu/123").build());
+        rewriteExchange.getAttributes().put(Constants.CONTEXT, new 
ShenyuContext());
+        final MetaData metaData = 
MetaData.builder().id("meta-1").path("/target/123").rpcType("dubbo").enabled(true).build();
+        MetaDataCache.getInstance().initCache("/target/123", metaData, 
"/target/123");
+
+        when(chain.execute(rewriteExchange)).thenReturn(Mono.empty());
+        SelectorData selectorData = mock(SelectorData.class);
+        StepVerifier.create(rewritePlugin.doExecute(rewriteExchange, chain, 
selectorData, data)).expectSubscription().verifyComplete();
+
+        final ShenyuContext context = (ShenyuContext) 
rewriteExchange.getAttributes().get(Constants.CONTEXT);
+        assertEquals("dubbo", context.getRpcType(), "metadata lookup must use 
the bare rewritten path when no context path exists");
+        MetaDataCache.getInstance().clean();
+    }
+
     @Test
     public void shouldRewritePathVariableWithCompleteValue() {
         RuleData data = new RuleData();

Reply via email to