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 0363808cba fix(context-path): preserve paths when prefix does not 
match (#6508) (#7632)
0363808cba is described below

commit 0363808cba5ff0a4243bc8209c4c131d6051be4f
Author: Jerry聊AI <[email protected]>
AuthorDate: Thu Oct 15 12:58:12 2026 +0800

    fix(context-path): preserve paths when prefix does not match (#6508) (#7632)
    
    Signed-off-by: jerry <[email protected]>
---
 .../plugin/context/path/ContextPathPlugin.java     |  3 +++
 .../plugin/context/path/ContextPathPluginTest.java | 24 ++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git 
a/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java
 
b/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java
index 7d30a6429b..028eae9e37 100644
--- 
a/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java
@@ -85,6 +85,9 @@ public class ContextPathPlugin extends AbstractShenyuPlugin {
         String realURI = "";
         String contextPath = handle.getContextPath();
         if (StringUtils.isNoneBlank(contextPath)) {
+            if (!StringUtils.startsWith(context.getPath(), contextPath)) {
+                return;
+            }
             realURI = context.getPath().substring(contextPath.length());
             attributes.put(Constants.CONTEXT_PATH, contextPath);
         }
diff --git 
a/shenyu-plugin/shenyu-plugin-context-path/src/test/java/org/apache/shenyu/plugin/context/path/ContextPathPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-context-path/src/test/java/org/apache/shenyu/plugin/context/path/ContextPathPluginTest.java
index d265dde694..db6b14d6bc 100644
--- 
a/shenyu-plugin/shenyu-plugin-context-path/src/test/java/org/apache/shenyu/plugin/context/path/ContextPathPluginTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-context-path/src/test/java/org/apache/shenyu/plugin/context/path/ContextPathPluginTest.java
@@ -33,6 +33,8 @@ import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
 import org.springframework.mock.web.server.MockServerWebExchange;
@@ -43,6 +45,7 @@ import static 
org.apache.shenyu.plugin.context.path.handler.ContextPathPluginDat
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 /**
@@ -102,6 +105,27 @@ public final class ContextPathPluginTest {
         Assertions.assertDoesNotThrow(() -> 
contextPathPlugin.doExecute(exchange, chain, selectorData, ruleData));
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {"/foo", "/api/order/longer"})
+    public void testNonMatchingContextPathPreservesExchange(final String 
configuredPath) {
+        shenyuContext.setPath("/api/order");
+        shenyuContext.setRealUrl("/previous/order");
+        exchange.getAttributes().put(Constants.CONTEXT_PATH, "/previous");
+        ContextMappingRuleHandle handle = new ContextMappingRuleHandle();
+        handle.setContextPath(configuredPath);
+        handle.setRewriteContextPath("/rewritten");
+        handle.setAddPrefix("/prefix");
+        when(ruleData.getId()).thenReturn("nonMatching");
+        CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), 
handle);
+
+        contextPathPlugin.doExecute(exchange, chain, selectorData, 
ruleData).block();
+
+        assertEquals("/previous/order", shenyuContext.getRealUrl());
+        assertEquals("/previous", 
exchange.getAttribute(Constants.CONTEXT_PATH));
+        
assertFalse(exchange.getAttributes().containsKey(Constants.REWRITE_CONTEXT_PATH));
+        verify(chain).execute(exchange);
+    }
+
     /**
      * Skip.
      */

Reply via email to