From 5a0c263d90d2f50d87d6e7b3b76055320091cbaa Mon Sep 17 00:00:00 2001 From: maxhov <14804474+maxhov@users.noreply.github.com> Date: Sat, 20 Jul 2024 15:10:59 +0200 Subject: [PATCH 1/2] Allow configuring HandlerMethodArgumentResolver beans on AnnotatedControllerConfigurer --- .../graphql/GraphQlAutoConfiguration.java | 5 ++++- .../GraphQlAutoConfigurationTests.java | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java index 9b9c408d7a58..599b613ad4a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java @@ -49,6 +49,7 @@ import org.springframework.core.log.LogMessage; import org.springframework.data.domain.ScrollPosition; import org.springframework.graphql.ExecutionGraphQlService; +import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer; import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor; import org.springframework.graphql.data.pagination.CursorEncoder; @@ -154,11 +155,13 @@ public ExecutionGraphQlService executionGraphQlService(GraphQlSource graphQlSour @Bean @ConditionalOnMissingBean public AnnotatedControllerConfigurer annotatedControllerConfigurer( - @Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider executorProvider) { + @Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider executorProvider, + ObjectProvider argumentResolvers) { AnnotatedControllerConfigurer controllerConfigurer = new AnnotatedControllerConfigurer(); controllerConfigurer .addFormatterRegistrar((registry) -> ApplicationConversionService.addBeans(registry, this.beanFactory)); executorProvider.ifAvailable(controllerConfigurer::setExecutor); + argumentResolvers.orderedStream().forEach(controllerConfigurer::addCustomArgumentResolver); return controllerConfigurer; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java index 75a44a5ce596..3304d143e53a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java @@ -45,6 +45,7 @@ import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; import org.springframework.graphql.ExecutionGraphQlService; +import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer; import org.springframework.graphql.data.pagination.EncodingCursorStrategy; import org.springframework.graphql.execution.BatchLoaderRegistry; @@ -236,11 +237,20 @@ void whenApplicationTaskExecutorIsDefinedThenAnnotatedControllerConfigurerShould void whenCustomExecutorIsDefinedThenAnnotatedControllerConfigurerDoesNotUseIt() { this.contextRunner.withUserConfiguration(CustomExecutorConfiguration.class).run((context) -> { AnnotatedControllerConfigurer annotatedControllerConfigurer = context - .getBean(AnnotatedControllerConfigurer.class); + .getBean(AnnotatedControllerConfigurer.class); assertThat(annotatedControllerConfigurer).extracting("executor").isNull(); }); } + @Test + void whenAHandlerMethodArgumentResolverIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() { + this.contextRunner.withUserConfiguration(CustomHandlerMethodArgumentResolverConfiguration.class).run((context) -> assertThat(context + .getBean(AnnotatedControllerConfigurer.class)) + .extracting("customArgumentResolvers") + .asInstanceOf(InstanceOfAssertFactories.LIST) + .hasSize(1)); + } + @Configuration(proxyBeanMethods = false) static class CustomGraphQlBuilderConfiguration { @@ -336,4 +346,12 @@ Executor customExecutor() { } + static class CustomHandlerMethodArgumentResolverConfiguration { + + @Bean + HandlerMethodArgumentResolver customHandlerMethodArgumentResolver() { + return mock(HandlerMethodArgumentResolver.class); + } + } + } From 7883248197832df93550a588bde8021376f2907b Mon Sep 17 00:00:00 2001 From: maxhov <14804474+maxhov@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:12:06 +0200 Subject: [PATCH 2/2] Fix formatting --- .../graphql/GraphQlAutoConfigurationTests.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java index 3304d143e53a..62aab64c17a9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java @@ -237,15 +237,15 @@ void whenApplicationTaskExecutorIsDefinedThenAnnotatedControllerConfigurerShould void whenCustomExecutorIsDefinedThenAnnotatedControllerConfigurerDoesNotUseIt() { this.contextRunner.withUserConfiguration(CustomExecutorConfiguration.class).run((context) -> { AnnotatedControllerConfigurer annotatedControllerConfigurer = context - .getBean(AnnotatedControllerConfigurer.class); + .getBean(AnnotatedControllerConfigurer.class); assertThat(annotatedControllerConfigurer).extracting("executor").isNull(); }); } @Test void whenAHandlerMethodArgumentResolverIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() { - this.contextRunner.withUserConfiguration(CustomHandlerMethodArgumentResolverConfiguration.class).run((context) -> assertThat(context - .getBean(AnnotatedControllerConfigurer.class)) + this.contextRunner.withUserConfiguration(CustomHandlerMethodArgumentResolverConfiguration.class) + .run((context) -> assertThat(context.getBean(AnnotatedControllerConfigurer.class)) .extracting("customArgumentResolvers") .asInstanceOf(InstanceOfAssertFactories.LIST) .hasSize(1)); @@ -352,6 +352,7 @@ static class CustomHandlerMethodArgumentResolverConfiguration { HandlerMethodArgumentResolver customHandlerMethodArgumentResolver() { return mock(HandlerMethodArgumentResolver.class); } + } }