Skip to content

Commit ee432a8

Browse files
committed
AnnotatedControllerExceptionResolver handles proxy class
Closes gh-710
1 parent fcb7f94 commit ee432a8

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/AnnotatedControllerExceptionResolver.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.graphql.execution.DataFetcherExceptionResolver;
4343
import org.springframework.lang.Nullable;
4444
import org.springframework.util.Assert;
45+
import org.springframework.util.ClassUtils;
4546
import org.springframework.util.ConcurrentReferenceHashMap;
4647
import org.springframework.util.ObjectUtils;
4748
import org.springframework.util.ReflectionUtils;
@@ -168,22 +169,24 @@ public Mono<List<GraphQLError>> resolveException(
168169

169170
Object controllerOrAdvice = null;
170171
MethodHolder methodHolder = null;
172+
Class<?> controllerType = null;
171173

172174
if (controller != null) {
173-
MethodResolver methodResolver = this.controllerCache.get(controller.getClass());
175+
controllerType = ClassUtils.getUserClass(controller.getClass());
176+
MethodResolver methodResolver = this.controllerCache.get(controllerType);
174177
if (methodResolver != null) {
175178
controllerOrAdvice = controller;
176179
methodHolder = methodResolver.resolveMethod(ex);
177180
}
178181
else if (logger.isWarnEnabled()) {
179-
logger.warn("No registration for controller type: " + controller.getClass().getName());
182+
logger.warn("No registration for controller type: " + controllerType.getName());
180183
}
181184
}
182185

183186
if (methodHolder == null) {
184187
for (Map.Entry<ControllerAdviceBean, MethodResolver> entry : this.controllerAdviceCache.entrySet()) {
185188
ControllerAdviceBean advice = entry.getKey();
186-
if (controller == null || advice.isApplicableToBeanType(controller.getClass())) {
189+
if (controller == null || advice.isApplicableToBeanType(controllerType)) {
187190
methodHolder = entry.getValue().resolveMethod(ex);
188191
if (methodHolder != null) {
189192
controllerOrAdvice = advice.resolveBean();

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/AnnotatedControllerExceptionResolverTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import reactor.core.publisher.Mono;
2727
import reactor.test.StepVerifier;
2828

29+
import org.springframework.aop.framework.ProxyFactory;
30+
import org.springframework.aop.target.SingletonTargetSource;
2931
import org.springframework.context.ApplicationContext;
3032
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3133
import org.springframework.context.support.StaticApplicationContext;
@@ -125,6 +127,20 @@ void invalidReturnType() {
125127
exceptionResolver().registerController(InvalidReturnTypeController.class));
126128
}
127129

130+
@Test // gh-710
131+
void controllerWithProxy() {
132+
TestController controller = new TestController();
133+
AnnotatedControllerExceptionResolver resolver = exceptionResolver();
134+
resolver.registerController(controller.getClass());
135+
136+
Exception ex = new IllegalArgumentException("Bad input");
137+
Object proxy = ProxyFactory.getProxy(new SingletonTargetSource(controller));
138+
List<GraphQLError> actual = resolver.resolveException(ex, this.environment, proxy).block();
139+
140+
assertThat(actual).hasSize(1);
141+
assertThat(actual.get(0).getMessage()).isEqualTo("handleToSingleError: Bad input");
142+
}
143+
128144
private void testResolve(Throwable ex, TestController controller, List<String> expected) {
129145

130146
AnnotatedControllerExceptionResolver resolver = exceptionResolver();
@@ -156,7 +172,7 @@ private AnnotatedControllerExceptionResolver exceptionResolver(ApplicationContex
156172

157173
@SuppressWarnings("unused")
158174
@Controller
159-
private static class TestController {
175+
static class TestController {
160176

161177
@GraphQlExceptionHandler
162178
GraphQLError handleToSingleError(IllegalArgumentException ex) {

0 commit comments

Comments
 (0)