Skip to content

Commit

Permalink
fix: use Qualifier to force bean dependency (#3098) (CP: 24.6) (#3154)
Browse files Browse the repository at this point in the history
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: Luciano Vernaschi <[email protected]>
Co-authored-by: Artur <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent 9c70e4b commit a5ab968
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}

0 comments on commit a5ab968

Please sign in to comment.