Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use dependsOn to force bean dependency #3098

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
Expand Down Expand Up @@ -154,8 +155,9 @@ private static ObjectMapper createDefaultEndpointMapper(
* @return the endpoint invoker
*/
@Bean
@DependsOn("hillaEndpointObjectMapper")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that the bean named ”hillaEndpointObjectMapper” is created before this bean, right? But what ensures that that particular mapper is used here and not some other ObjectMapper in the project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a qualifier

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the same qualifier be used here

public SignalsConfiguration(EndpointInvoker endpointInvoker,
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the whole DependsOn now unnecessary, as it requires the specific Hilla object mapper?

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
@@ -1,11 +1,14 @@
package com.vaadin.hilla;

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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 +28,26 @@ public class EndpointControllerConfigurationTest {
@Autowired
private EndpointAccessChecker endpointAccessChecker;

@Autowired
private ConfigurableApplicationContext context;

@Test
public void dependenciesAvailable() {
Assert.assertNotNull(endpointRegistry);
Assert.assertNotNull(endpointAccessChecker);
}

@Test
public void testEndpointInvokerDependsOnHillaObjectMapper() {
// Fetch the BeanDefinition for endpointInvoker
var endpointInvokerDefinition = context.getBeanFactory()
.getBeanDefinition("endpointInvoker");

var dependsOn = endpointInvokerDefinition.getDependsOn();

Assert.assertNotNull("dependsOn should not be null", dependsOn);
Assert.assertTrue(
"endpointInvoker should depend on hillaEndpointObjectMapper",
Arrays.asList(dependsOn).contains("hillaEndpointObjectMapper"));
}
}
Loading