Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit c840548

Browse files
committed
fix(#736): make exception handlers work with Spring proxies
1 parent c11a84f commit c840548

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

graphql-kickstart-spring-support/src/main/java/graphql/kickstart/spring/error/GraphQLErrorHandlerFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Arrays;
1010
import java.util.List;
1111
import lombok.extern.slf4j.Slf4j;
12+
import org.springframework.aop.support.AopUtils;
1213
import org.springframework.beans.factory.BeanCreationException;
1314
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
1415
import org.springframework.context.ApplicationContext;
@@ -47,6 +48,7 @@ private List<GraphQLErrorFactory> scanForExceptionHandlers(
4748
return emptyList();
4849
}
4950
return Arrays.stream(objClz.getDeclaredMethods())
51+
.map(method -> AopUtils.getMostSpecificMethod(method, objClz))
5052
.filter(ReflectiveMethodValidator::isGraphQLExceptionHandler)
5153
.map(method -> withReflection(context.getBean(name), method))
5254
.collect(toList());

graphql-kickstart-spring-support/src/test/java/graphql/kickstart/spring/error/GraphQLErrorHandlerFactoryTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.mockito.Mock;
1313
import org.mockito.Mockito;
1414
import org.mockito.junit.jupiter.MockitoExtension;
15+
import org.springframework.aop.framework.ProxyFactory;
1516
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
1617
import org.springframework.context.ConfigurableApplicationContext;
1718
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -29,13 +30,14 @@ public void setup() {
2930
Mockito.when(applicationContext.getBeanFactory()).thenReturn(beanFactory);
3031
Mockito.when(beanFactory.getBeanDefinitionNames()).thenReturn(new String[] {"Test"});
3132
Mockito.when(applicationContext.containsBean("Test")).thenReturn(true);
32-
Mockito.doReturn(TestClass.class).when(applicationContext).getType("Test");
3333

3434
errorHandlerFactory = new GraphQLErrorHandlerFactory();
3535
}
3636

3737
@Test
3838
void createFindsCollectionHandler() {
39+
Mockito.doReturn(TestClass.class).when(applicationContext).getType("Test");
40+
3941
GraphQLErrorHandler handler = errorHandlerFactory.create(applicationContext, true);
4042
assertThat(handler).isInstanceOf(GraphQLErrorFromExceptionHandler.class);
4143
GraphQLErrorFromExceptionHandler errorHandler = (GraphQLErrorFromExceptionHandler) handler;
@@ -44,6 +46,23 @@ void createFindsCollectionHandler() {
4446
.isNotEmpty();
4547
}
4648

49+
@Test
50+
void createFindsExceptionHandlerEvenIfProxy() {
51+
Mockito.doReturn(proxyTestClass()).when(applicationContext).getType("Test");
52+
53+
GraphQLErrorHandler handler = errorHandlerFactory.create(applicationContext, true);
54+
assertThat(handler).isInstanceOf(GraphQLErrorFromExceptionHandler.class);
55+
GraphQLErrorFromExceptionHandler errorHandler = (GraphQLErrorFromExceptionHandler) handler;
56+
assertThat(errorHandler.getFactories())
57+
.as("handler.factories should not be empty")
58+
.isNotEmpty();
59+
}
60+
61+
private Class<? extends TestClass> proxyTestClass() {
62+
TestClass proxy = (TestClass) new ProxyFactory(new TestClass()).getProxy();
63+
return proxy.getClass();
64+
}
65+
4766
public static class TestClass {
4867

4968
@ExceptionHandler(IllegalArgumentException.class)

0 commit comments

Comments
 (0)