GH-1491: Fix Fallback When Parameter is Optional

Only pass `Optional.empty()` if the method parameter is `Optional`.
This commit is contained in:
Gary Russell
2022-08-10 12:23:13 -04:00
parent c67f80b0de
commit f2e1e98317

View File

@@ -18,6 +18,8 @@ package org.springframework.amqp.rabbit.annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -1054,7 +1056,8 @@ public class RabbitListenerAnnotationBeanPostProcessor
resolved = super.resolveArgument(parameter, message);
}
catch (MethodArgumentNotValidException ex) {
if (message.getPayload().equals(Optional.empty())) {
Type type = parameter.getGenericParameterType();
if (isOptional(message, type)) {
BindingResult bindingResult = ex.getBindingResult();
if (bindingResult != null) {
List<ObjectError> allErrors = bindingResult.getAllErrors();
@@ -1082,6 +1085,12 @@ public class RabbitListenerAnnotationBeanPostProcessor
return resolved;
}
private boolean isOptional(Message<?> message, Type type) {
return (Optional.class.equals(type) || (type instanceof ParameterizedType
&& Optional.class.equals(((ParameterizedType) type).getRawType())))
&& message.getPayload().equals(Optional.empty());
}
@Override
protected boolean isEmptyPayload(Object payload) {
return payload == null || payload.equals(Optional.empty());