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 f271f6c74d fix(sdk): accept all 2xx responses and validate failures on 
void methods (#7711) (#7740)
f271f6c74d is described below

commit f271f6c74dde837923c9899ae3851027a7a50aef
Author: konlue <[email protected]>
AuthorDate: Mon Oct 19 09:33:36 2026 +0800

    fix(sdk): accept all 2xx responses and validate failures on void methods 
(#7711) (#7740)
---
 .../spring/proxy/ShenyuClientMethodHandler.java    | 20 ++++++---
 .../proxy/ShenyuClientMethodHandlerTest.java       | 51 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 6 deletions(-)

diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandler.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandler.java
index 5673096c5d..574c9cfce6 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandler.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandler.java
@@ -75,18 +75,26 @@ public class ShenyuClientMethodHandler {
     }
 
     private Object handlerResponse(final ShenyuResponse shenyuResponse, final 
Class<?> returnType) {
-        if (Objects.isNull(shenyuResponse) || void.class == returnType) {
+        if (Objects.isNull(shenyuResponse)) {
             return null;
-        } else if (ShenyuResponse.class == returnType) {
+        }
+        // the raw ShenyuResponse return type is an explicit escape hatch: 
hand the response back untouched
+        if (ShenyuResponse.class == returnType) {
             return shenyuResponse;
-        } else if (shenyuResponse.getStatus() != HttpStatus.OK.value()) {
+        }
+        if (!isSuccessful(shenyuResponse.getStatus())) {
             log.warn("handlerResponse http status warn shenyuResponse {}", 
JsonUtils.toJson(shenyuResponse));
             throw new 
HttpClientErrorException(HttpStatus.valueOf(shenyuResponse.getStatus()));
-        } else if (StringUtils.hasText(shenyuResponse.getBody())) {
-            return JsonUtils.jsonToObject(shenyuResponse.getBody(), 
returnType);
-        } else {
+        }
+        if (void.class == returnType || 
!StringUtils.hasText(shenyuResponse.getBody())) {
             return null;
         }
+        return JsonUtils.jsonToObject(shenyuResponse.getBody(), returnType);
+    }
+
+    private boolean isSuccessful(final int status) {
+        final HttpStatus httpStatus = HttpStatus.resolve(status);
+        return Objects.nonNull(httpStatus) && httpStatus.is2xxSuccessful();
     }
 
     private ShenyuRequest targetProcessor(final RequestTemplate 
requestTemplate, final Object[] args) {
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandlerTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandlerTest.java
index bca37a98e7..bb7269f975 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandlerTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientMethodHandlerTest.java
@@ -19,12 +19,22 @@ package org.apache.shenyu.sdk.spring.proxy;
 
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
 import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.apache.shenyu.sdk.core.ShenyuResponse;
 import org.apache.shenyu.sdk.core.common.RequestTemplate;
 import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.DisabledForJreRange;
 import org.junit.jupiter.api.condition.JRE;
+import org.springframework.http.HttpStatus;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.web.client.HttpClientErrorException;
 
 /**
  * {@link ShenyuClientMethodHandler} test.
@@ -44,4 +54,45 @@ public class ShenyuClientMethodHandlerTest extends 
AbstractProxyTest {
         }));
     }
 
+    @Test
+    public void handlerResponseAcceptsAllSuccessfulStatuses() {
+        assertEquals(42, handlerResponse(200, "42", Integer.class));
+        assertEquals(42, handlerResponse(201, "42", Integer.class));
+        assertEquals(42, handlerResponse(202, "42", Integer.class));
+        assertNull(handlerResponse(204, "", Integer.class));
+    }
+
+    @Test
+    public void handlerResponseThrowsForNonSuccessfulStatusesOnTypedMethods() {
+        assertThrows(HttpClientErrorException.class, () -> 
handlerResponse(400, "bad request", Integer.class));
+        assertThrows(HttpClientErrorException.class, () -> 
handlerResponse(500, "server error", Integer.class));
+    }
+
+    @Test
+    public void handlerResponseValidatesVoidMethods() {
+        assertThrows(HttpClientErrorException.class, () -> 
handlerResponse(500, "", void.class));
+        assertNull(handlerResponse(204, "", void.class));
+    }
+
+    @Test
+    public void handlerResponseReturnsRawResponseRegardlessOfStatus() {
+        final ShenyuResponse response = response(500, "server error");
+        final ShenyuClientMethodHandler handler = newHandler();
+        assertSame(response, ReflectionTestUtils.invokeMethod(handler, 
"handlerResponse", response, ShenyuResponse.class));
+    }
+
+    private Object handlerResponse(final int status, final String body, final 
Class<?> returnType) {
+        return ReflectionTestUtils.invokeMethod(newHandler(), 
"handlerResponse", response(status, body), returnType);
+    }
+
+    private ShenyuClientMethodHandler newHandler() {
+        return new ShenyuClientMethodHandler(null, new RequestTemplate(), 
null, Collections.emptyMap());
+    }
+
+    private ShenyuResponse response(final int status, final String body) {
+        final ShenyuRequest request = 
ShenyuRequest.create(ShenyuRequest.HttpMethod.GET, "http://localhost/test";,
+                new HashMap<>(), "", "test", new RequestTemplate());
+        return new ShenyuResponse(status, 
HttpStatus.valueOf(status).getReasonPhrase(), Collections.emptyMap(), body, 
request);
+    }
+
 }

Reply via email to