Skip to content

Commit 9c98489

Browse files
committed
handle issue with 5.0 testlatestdeps
1 parent 559d89f commit 9c98489

File tree

1 file changed

+28
-1
lines changed
  • instrumentation/spring/spring-webflux/spring-webflux-5.0/testing/src/main/java/io/opentelemetry/instrumentation/spring/webflux/server

1 file changed

+28
-1
lines changed

instrumentation/spring/spring-webflux/spring-webflux-5.0/testing/src/main/java/io/opentelemetry/instrumentation/spring/webflux/server/ServerTestController.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.instrumentation.spring.webflux.server.AbstractSpringWebFluxServerTest.NESTED_PATH;
99

1010
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
11+
import java.lang.reflect.Method;
1112
import java.net.URI;
1213
import java.util.function.Supplier;
1314
import org.springframework.http.HttpStatus;
@@ -19,6 +20,27 @@
1920

2021
@SuppressWarnings("IdentifierName") // method names are snake_case to match endpoints
2122
public abstract class ServerTestController {
23+
24+
// Spring 5.x uses setStatusCode(HttpStatus), Spring 6+ uses setStatusCode(HttpStatusCode)
25+
private static final Method setStatusCodeMethod;
26+
27+
static {
28+
Method method;
29+
try {
30+
// Try Spring 6+ signature first (HttpStatusCode interface)
31+
Class<?> httpStatusCodeClass = Class.forName("org.springframework.http.HttpStatusCode");
32+
method = ServerHttpResponse.class.getMethod("setStatusCode", httpStatusCodeClass);
33+
} catch (ClassNotFoundException | NoSuchMethodException e) {
34+
// Fall back to Spring 5.x signature (HttpStatus enum)
35+
try {
36+
method = ServerHttpResponse.class.getMethod("setStatusCode", HttpStatus.class);
37+
} catch (NoSuchMethodException ex) {
38+
throw new IllegalStateException(ex);
39+
}
40+
}
41+
setStatusCodeMethod = method;
42+
}
43+
2244
@GetMapping("/success")
2345
public Mono<String> success(ServerHttpResponse response) {
2446
ServerEndpoint endpoint = ServerEndpoint.SUCCESS;
@@ -134,6 +156,11 @@ public Mono<String> nested_path(ServerHttpRequest request, ServerHttpResponse re
134156
protected abstract <T> Mono<T> wrapControllerMethod(ServerEndpoint endpoint, Supplier<T> handler);
135157

136158
protected void setStatus(ServerHttpResponse response, ServerEndpoint endpoint) {
137-
response.setStatusCode(HttpStatus.resolve(endpoint.getStatus()));
159+
HttpStatus status = HttpStatus.resolve(endpoint.getStatus());
160+
try {
161+
setStatusCodeMethod.invoke(response, status);
162+
} catch (ReflectiveOperationException e) {
163+
throw new IllegalStateException("Failed to set status code", e);
164+
}
138165
}
139166
}

0 commit comments

Comments
 (0)