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

Add Validator setter in EndpointRegistrar #893

Merged
merged 1 commit into from
Oct 4, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/sqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,7 @@ The following attributes can be configured in the registrar:
- `setMessageListenerContainerRegistryBeanName` - provide a different bean name to be used to retrieve the `MessageListenerContainerRegistry`
- `setObjectMapper` - set the `ObjectMapper` instance that will be used to deserialize payloads in listener methods.
See <<Message Conversion and Payload Deserialization>> for more information on where this is used.
- `setValidator` - set the `Validator` instance that will be used for payload validation in listener methods.
- `manageMessageConverters` - gives access to the list of message converters that will be used to convert messages.
By default, `StringMessageConverter`, `SimpleMessageConverter` and `MappingJackson2MessageConverter` are used.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ protected List<HandlerMethodArgumentResolver> createArgumentResolvers(MessageCon
new BatchAcknowledgmentArgumentResolver(),
new HeaderMethodArgumentResolver(new DefaultConversionService(), getConfigurableBeanFactory()),
new HeadersMethodArgumentResolver(),
new BatchPayloadMethodArgumentResolver(messageConverter),
new BatchPayloadMethodArgumentResolver(messageConverter, this.endpointRegistrar.getValidator()),
new MessageMethodArgumentResolver(messageConverter),
new PayloadMethodArgumentResolver(messageConverter));
new PayloadMethodArgumentResolver(messageConverter, this.endpointRegistrar.getValidator()));
}
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.Validator;

/**
* Processes the registered {@link Endpoint} instances using the appropriate {@link MessageListenerContainerFactory}.
Expand Down Expand Up @@ -70,6 +71,7 @@ public class EndpointRegistrar implements BeanFactoryAware, SmartInitializingSin
};

private ObjectMapper objectMapper;
private Validator validator;

/**
* Set a custom {@link MessageHandlerMethodFactory} implementation.
Expand Down Expand Up @@ -118,6 +120,14 @@ public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

/**
* Set the {@link Validator} instance used for payload validating in {@link HandlerMethodArgumentResolver} instances.
* @param validator payload validator.
*/
public void setValidator(Validator validator) {
this.validator = validator;
}

/**
* Manage the list of {@link MessageConverter} instances to be used to convert payloads.
* @param convertersConsumer a consumer for the converters list.
Expand Down Expand Up @@ -169,6 +179,14 @@ public MessageHandlerMethodFactory getMessageHandlerMethodFactory() {
return this.messageHandlerMethodFactory;
}

/**
* Return the {@link Validator} instance used for payload validating in {@link HandlerMethodArgumentResolver} instances.
* @return the payload validator.
*/
public Validator getValidator() {
return this.validator;
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.awspring.cloud.sqs.listener.DefaultListenerContainerRegistry;
import io.awspring.cloud.sqs.listener.MessageListenerContainer;
import io.awspring.cloud.sqs.listener.MessageListenerContainerRegistry;
import io.awspring.cloud.sqs.support.resolver.BatchPayloadMethodArgumentResolver;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -50,6 +51,7 @@
import org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.util.StringValueResolver;
import org.springframework.validation.Validator;

/**
* Tests for {@link SqsListenerAnnotationBeanPostProcessor}.
Expand All @@ -71,6 +73,7 @@ public void registerListenerContainer(MessageListenerContainer<?> listenerContai
String factoryName = "otherFactory";
MessageConverter converter = mock(MessageConverter.class);
HandlerMethodArgumentResolver resolver = mock(HandlerMethodArgumentResolver.class);
Validator validator = mock(Validator.class);

SqsListenerConfigurer customizer = registrar -> {
registrar.setDefaultListenerContainerFactoryBeanName(factoryName);
Expand All @@ -79,6 +82,7 @@ public void registerListenerContainer(MessageListenerContainer<?> listenerContai
registrar.setObjectMapper(objectMapper);
registrar.manageMessageConverters(converters -> converters.add(converter));
registrar.manageMethodArgumentResolvers(resolvers -> resolvers.add(resolver));
registrar.setValidator(validator);
};

when(beanFactory.getBeansOfType(SqsListenerConfigurer.class))
Expand Down Expand Up @@ -115,13 +119,15 @@ protected EndpointRegistrar createEndpointRegistrar() {
assertThat(endpoint).extracting("handlerMethodFactory").extracting("delegate").isEqualTo(methodFactory)
.extracting("argumentResolvers").extracting("argumentResolvers")
.asInstanceOf(list(HandlerMethodArgumentResolver.class)).hasSizeGreaterThan(1).contains(resolver)
.filteredOn(thisResolver -> thisResolver instanceof PayloadMethodArgumentResolver).element(0)
.extracting("converter").asInstanceOf(type(CompositeMessageConverter.class))
.extracting(CompositeMessageConverter::getConverters).asInstanceOf(list(MessageConverter.class))
.contains(converter)
.filteredOn(thisConverter -> thisConverter instanceof MappingJackson2MessageConverter).element(0)
.extracting("objectMapper").isEqualTo(objectMapper);

.filteredOn(thisResolver -> thisResolver instanceof PayloadMethodArgumentResolver || thisResolver instanceof BatchPayloadMethodArgumentResolver)
.allSatisfy(thisResolver -> {
assertThat(thisResolver).extracting("validator").isEqualTo(validator);
assertThat(thisResolver).extracting("converter").asInstanceOf(type(CompositeMessageConverter.class))
.extracting(CompositeMessageConverter::getConverters).asInstanceOf(list(MessageConverter.class))
.contains(converter)
.filteredOn(thisConverter -> thisConverter instanceof MappingJackson2MessageConverter).element(0)
.extracting("objectMapper").isEqualTo(objectMapper);
});
}

@Test
Expand Down