From c6494c1b74023c47b8162be0e1627675acf4a2ca Mon Sep 17 00:00:00 2001 From: Luciano Vernaschi Date: Thu, 9 Jan 2025 21:15:02 +0100 Subject: [PATCH] fix: use `Qualifier` to force bean dependency (#3098) * Use `dependsOn` to force bean dependency * Add qualifier * Add qualifier * Remove `@DependsOn` * Update test to match new code --------- Co-authored-by: Artur --- .../EndpointControllerConfiguration.java | 2 +- .../signals/config/SignalsConfiguration.java | 3 ++- .../EndpointControllerConfigurationTest.java | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointControllerConfiguration.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointControllerConfiguration.java index 3949492baf..a17111de91 100644 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointControllerConfiguration.java +++ b/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointControllerConfiguration.java @@ -155,7 +155,7 @@ private static ObjectMapper createDefaultEndpointMapper( */ @Bean EndpointInvoker endpointInvoker(ApplicationContext applicationContext, - ObjectMapper hillaEndpointObjectMapper, + @Qualifier("hillaEndpointObjectMapper") ObjectMapper hillaEndpointObjectMapper, ExplicitNullableTypeChecker explicitNullableTypeChecker, ServletContext servletContext, EndpointRegistry endpointRegistry) { return new EndpointInvoker(applicationContext, diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/config/SignalsConfiguration.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/config/SignalsConfiguration.java index c9873cd58b..680ff1c500 100644 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/config/SignalsConfiguration.java +++ b/packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/config/SignalsConfiguration.java @@ -23,6 +23,7 @@ import com.vaadin.hilla.signals.core.registry.SecureSignalsRegistry; import com.vaadin.hilla.signals.handler.SignalsHandler; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -37,7 +38,7 @@ public class SignalsConfiguration { private final EndpointInvoker endpointInvoker; public SignalsConfiguration(EndpointInvoker endpointInvoker, - ObjectMapper hillaEndpointObjectMapper) { + @Qualifier("hillaEndpointObjectMapper") ObjectMapper hillaEndpointObjectMapper) { this.endpointInvoker = endpointInvoker; Signal.setMapper(hillaEndpointObjectMapper); } diff --git a/packages/java/endpoint/src/test/java/com/vaadin/hilla/EndpointControllerConfigurationTest.java b/packages/java/endpoint/src/test/java/com/vaadin/hilla/EndpointControllerConfigurationTest.java index 0bc4ebc6c5..447d8523ee 100644 --- a/packages/java/endpoint/src/test/java/com/vaadin/hilla/EndpointControllerConfigurationTest.java +++ b/packages/java/endpoint/src/test/java/com/vaadin/hilla/EndpointControllerConfigurationTest.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jackson.JacksonProperties; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -25,9 +26,32 @@ public class EndpointControllerConfigurationTest { @Autowired private EndpointAccessChecker endpointAccessChecker; + @Autowired + private ConfigurableApplicationContext context; + @Test public void dependenciesAvailable() { Assert.assertNotNull(endpointRegistry); Assert.assertNotNull(endpointAccessChecker); } + + @Test + public void testEndpointInvokerUsesQualifiedObjectMapper() + throws NoSuchFieldException, IllegalAccessException { + var endpointInvoker = context.getBean(EndpointInvoker.class); + var objectMapper = context.getBean("hillaEndpointObjectMapper"); + + Assert.assertNotNull("EndpointInvoker should not be null", + endpointInvoker); + Assert.assertNotNull("hillaEndpointObjectMapper should not be null", + objectMapper); + + var field = EndpointInvoker.class + .getDeclaredField("endpointObjectMapper"); + field.setAccessible(true); + + Assert.assertSame( + "EndpointInvoker should use the qualified hillaEndpointObjectMapper", + objectMapper, field.get(endpointInvoker)); + } }