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

wy471x 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 15f926de4e fix(sdk): resolve Spring annotation name aliases in 
parameter processors (#7491)
15f926de4e is described below

commit 15f926de4e82e3f2742bf3b648bf89edb8762dc9
Author: Sean-Walker0 <[email protected]>
AuthorDate: Sat Oct 10 22:00:56 2026 +0800

    fix(sdk): resolve Spring annotation name aliases in parameter processors 
(#7491)
    
    The SDK parameter processors read only annotation.value() for
    @RequestParam, @RequestHeader, @PathVariable and @CookieValue. Spring
    treats value and name as aliases on these annotations, so valid
    declarations like @RequestParam(name = "id") resolved to an empty
    name and either failed the processors' non-empty check or omitted the
    intended data.
    
    Fall back to annotation.name() when value is blank. Regression tests
    cover name-only declarations for all four processors (red on the
    previous code, which threw IllegalStateException or dropped the
    parameter).
    
    Fixes #7465
    
    Co-authored-by: Sean-Walker0 
<[email protected]>
---
 .../spring/annotation/CookieValueParameterProcessor.java |  2 +-
 .../annotation/PathVariableParameterProcessor.java       |  2 +-
 .../annotation/RequestHeaderParameterProcessor.java      |  3 ++-
 .../annotation/RequestParamParameterProcessor.java       |  2 +-
 .../annotation/CookieValueParameterProcessorTest.java    | 10 ++++++++++
 .../annotation/PathVariableParameterProcessorTest.java   | 16 ++++++++++++++++
 .../annotation/RequestHeaderParameterProcessorTest.java  | 11 +++++++++++
 .../annotation/RequestParamParameterProcessorTest.java   | 14 ++++++++++++++
 8 files changed, 56 insertions(+), 4 deletions(-)

diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessor.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessor.java
index e54991f642..e5e8831e18 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessor.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessor.java
@@ -49,7 +49,7 @@ public class CookieValueParameterProcessor implements 
AnnotatedParameterProcesso
     public boolean processArgument(final ShenyuRequest shenyuRequest, final 
Annotation annotation, final Object arg) {
         RequestTemplate requestTemplate = shenyuRequest.getRequestTemplate();
         CookieValue cookie = ANNOTATION.cast(annotation);
-        String name = cookie.value().trim();
+        String name = StringUtils.defaultIfBlank(cookie.value(), 
StringUtils.defaultIfBlank(cookie.name(), StringUtils.EMPTY)).trim();
         checkState(StringUtils.isNotBlank(name), "Cookie.name() was empty on 
parameter %s", requestTemplate.getMethod());
         Collection<String> cookieExpression = 
requestTemplate.getHeaders().getOrDefault(HttpHeaders.COOKIE, 
Lists.newArrayList());
         cookieExpression.add(String.format("%s=%s", name, arg));
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessor.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessor.java
index 3b7e5480b7..28e5c21479 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessor.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessor.java
@@ -42,7 +42,7 @@ public class PathVariableParameterProcessor implements 
AnnotatedParameterProcess
 
     @Override
     public boolean processArgument(final ShenyuRequest shenyuRequest, final 
Annotation annotation, final Object arg) {
-        String name = ANNOTATION.cast(annotation).value();
+        String name = 
StringUtils.defaultIfBlank(ANNOTATION.cast(annotation).value(), 
ANNOTATION.cast(annotation).name());
         RequestTemplate requestTemplate = shenyuRequest.getRequestTemplate();
         checkState(arg instanceof String && StringUtils.isNotBlank(name) || 
arg instanceof Map,
             "PathVariable Object class pls is String or Map<String, String> 
and PathVariable annotation value could not be empty when String class at the 
method %s.", requestTemplate.getMethod());
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessor.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessor.java
index 97d0aef0f9..344635e2dc 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessor.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessor.java
@@ -26,6 +26,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.sdk.core.ShenyuRequest;
 import org.apache.shenyu.sdk.core.common.RequestTemplate;
 import static org.apache.shenyu.sdk.core.util.Util.checkState;
@@ -46,7 +47,7 @@ public class RequestHeaderParameterProcessor implements 
AnnotatedParameterProces
 
     @Override
     public boolean processArgument(final ShenyuRequest shenyuRequest, final 
Annotation annotation, final Object arg) {
-        String name = ANNOTATION.cast(annotation).value();
+        String name = 
StringUtils.defaultIfBlank(ANNOTATION.cast(annotation).value(), 
ANNOTATION.cast(annotation).name());
         RequestTemplate requestTemplate = shenyuRequest.getRequestTemplate();
         checkState(Objects.nonNull(emptyToNull(name)), "RequestHeader.value() 
was empty on parameter %s", requestTemplate.getMethod().getName());
         Map<String, Collection<String>> headers = shenyuRequest.getHeaders();
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessor.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessor.java
index f575edbfaf..c5c27f5e79 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessor.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessor.java
@@ -47,7 +47,7 @@ public class RequestParamParameterProcessor implements 
AnnotatedParameterProcess
     public boolean processArgument(final ShenyuRequest shenyuRequest, final 
Annotation annotation, final Object arg) {
         RequestTemplate requestTemplate = shenyuRequest.getRequestTemplate();
         RequestParam requestParam = ANNOTATION.cast(annotation);
-        String name = requestParam.value();
+        String name = StringUtils.defaultIfBlank(requestParam.value(), 
requestParam.name());
         checkState(StringUtils.isNotBlank(name) || arg instanceof Map, 
"RequestParam.value() was empty on parameter %s#%s",
             requestTemplate.getMethod().getDeclaringClass().getSimpleName(), 
requestTemplate.getMethod().getName());
         StringBuilder urlResult = new StringBuilder(shenyuRequest.getUrl());
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessorTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessorTest.java
index ef692fa88a..b012b7be4a 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessorTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/CookieValueParameterProcessorTest.java
@@ -59,6 +59,16 @@ public class CookieValueParameterProcessorTest {
         this.request = req;
     }
 
+    @Test
+    public void processArgumentNameOnlyTest() {
+        final CookieValue cookie = spy(CookieValue.class);
+        when(cookie.value()).thenReturn("");
+        when(cookie.name()).thenReturn("name2");
+        processor.processArgument(request, cookie, "two");
+
+        
assertTrue(request.getHeaders().get(HttpHeaders.COOKIE).contains("name2=two"), 
"cookie name alias resolve error");
+    }
+
     @Test
     public void processArgumentNullTest() {
         final CookieValue cookie = spy(CookieValue.class);
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessorTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessorTest.java
index 7f71dd19ed..30a1846813 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessorTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/PathVariableParameterProcessorTest.java
@@ -27,6 +27,7 @@ import 
org.apache.shenyu.sdk.spring.factory.AnnotatedParameterProcessor;
 import org.junit.Assert;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
 import static org.mockito.Mockito.mock;
@@ -48,6 +49,21 @@ public class PathVariableParameterProcessorTest {
         this.processor = new PathVariableParameterProcessor();
     }
 
+    @Test
+    public void processArgumentNameOnlyTest() {
+        RequestTemplate template = new RequestTemplate();
+        template.setUrl("/url/a/b");
+        template.setPath("/id/{id}");
+        this.request = ShenyuRequest.create(ShenyuRequest.HttpMethod.POST, 
template.getUrl() + template.getPath(), Maps.newHashMap(), "", "test", 
template);
+        final PathVariable path = spy(PathVariable.class);
+        when(path.value()).thenReturn("");
+        when(path.name()).thenReturn("id");
+
+        processor.processArgument(request, path, "one");
+
+        assertTrue(request.getUrl().contains("/one"), "path name alias resolve 
fail");
+    }
+
     @ParameterizedTest
     
@MethodSource("org.apache.shenyu.sdk.spring.annotation.PathVariableParameterProcessorTest#templateStream")
     public void processArgumentNullTest(final RequestTemplate template) {
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessorTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessorTest.java
index c76a195a3c..c8fa466d6e 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessorTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestHeaderParameterProcessorTest.java
@@ -61,6 +61,17 @@ public class RequestHeaderParameterProcessorTest {
         this.request = req;
     }
 
+    @Test
+    public void processArgumentNameOnlyTest() {
+        final RequestHeader header = spy(RequestHeader.class);
+        when(header.value()).thenReturn("");
+        when(header.name()).thenReturn("header2");
+
+        processor.processArgument(request, header, "value2");
+
+        assertTrue(headers.get("header2").contains("value2"), "header name 
alias resolve failed.");
+    }
+
     @Test
     public void processArgumentEmptyTest() {
         final RequestHeader header = spy(RequestHeader.class);
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessorTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessorTest.java
index 9fc246b0a3..9f3e3fb0e2 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessorTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessorTest.java
@@ -53,6 +53,20 @@ public class RequestParamParameterProcessorTest {
         this.method2 = clazz.getMethod("method2", Map.class);
     }
 
+
+    @Test
+    public void processArgumentNameOnlyTest() {
+        RequestTemplate template = new RequestTemplate(Void.class, method1, 
"method1", "/dev/url/param", "", "/path", ShenyuRequest.HttpMethod.GET, null, 
null, null);
+        this.request = ShenyuRequest.create(ShenyuRequest.HttpMethod.POST, 
template.getUrl() + template.getPath(), Maps.newHashMap(), "", "test", 
template);
+        final RequestParam param = spy(RequestParam.class);
+        when(param.value()).thenReturn("");
+        when(param.name()).thenReturn("id");
+
+        processor.processArgument(request, param, "idValue");
+
+        assertTrue(request.getUrl().endsWith("id=idValue"), "param name alias 
resolve failed.");
+    }
+
     @Test
     public void processArgumentEncodesReservedCharactersTest() {
         RequestTemplate template = new RequestTemplate(Void.class, method1, 
"method1", "/dev/url/param", "", "/path", ShenyuRequest.HttpMethod.GET, null, 
null, null);

Reply via email to