Skip to content

Commit caebe72

Browse files
committed
Proper resolution of Optional.empty() for header arguments
Issue: SPR-15151 (cherry picked from commit e9db4d6)
1 parent f79baec commit caebe72

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@
5353
* argument value if it doesn't match the method parameter type.
5454
*
5555
* @author Rossen Stoyanchev
56+
* @author Juergen Hoeller
5657
* @since 4.0
5758
*/
5859
public abstract class AbstractNamedValueMethodArgumentResolver implements HandlerMethodArgumentResolver {
@@ -107,7 +108,7 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
107108
arg = resolveStringValue(namedValueInfo.defaultValue);
108109
}
109110

110-
if (!ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
111+
if (parameter != nestedParameter || !ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
111112
arg = this.conversionService.convert(arg, TypeDescriptor.forObject(arg), new TypeDescriptor(parameter));
112113
}
113114

spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -42,6 +43,7 @@
4243
* Test fixture for {@link HeaderMethodArgumentResolver} tests.
4344
*
4445
* @author Rossen Stoyanchev
46+
* @author Juergen Hoeller
4547
* @since 4.0
4648
*/
4749
public class HeaderMethodArgumentResolverTests {
@@ -53,6 +55,7 @@ public class HeaderMethodArgumentResolverTests {
5355
private MethodParameter paramSystemPropertyDefaultValue;
5456
private MethodParameter paramSystemPropertyName;
5557
private MethodParameter paramNotAnnotated;
58+
private MethodParameter paramOptional;
5659
private MethodParameter paramNativeHeader;
5760

5861

@@ -69,7 +72,8 @@ public void setup() throws Exception {
6972
this.paramSystemPropertyDefaultValue = new SynthesizingMethodParameter(method, 2);
7073
this.paramSystemPropertyName = new SynthesizingMethodParameter(method, 3);
7174
this.paramNotAnnotated = new SynthesizingMethodParameter(method, 4);
72-
this.paramNativeHeader = new SynthesizingMethodParameter(method, 5);
75+
this.paramOptional = new SynthesizingMethodParameter(method, 5);
76+
this.paramNativeHeader = new SynthesizingMethodParameter(method, 6);
7377

7478
this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
7579
GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
@@ -147,13 +151,40 @@ public void resolveNameFromSystemProperty() throws Exception {
147151
}
148152
}
149153

154+
@Test
155+
public void resolveOptionalHeaderWithValue() throws Exception {
156+
GenericApplicationContext cxt = new GenericApplicationContext();
157+
cxt.refresh();
158+
159+
HeaderMethodArgumentResolver resolver =
160+
new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
161+
162+
Message<String> message = MessageBuilder.withPayload("foo").setHeader("foo", "bar").build();
163+
Object result = resolver.resolveArgument(paramOptional, message);
164+
assertEquals(Optional.of("bar"), result);
165+
}
166+
167+
@Test
168+
public void resolveOptionalHeaderAsEmpty() throws Exception {
169+
GenericApplicationContext cxt = new GenericApplicationContext();
170+
cxt.refresh();
171+
172+
HeaderMethodArgumentResolver resolver =
173+
new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
174+
175+
Message<String> message = MessageBuilder.withPayload("foo").build();
176+
Object result = resolver.resolveArgument(paramOptional, message);
177+
assertEquals(Optional.empty(), result);
178+
}
179+
150180

151181
public void handleMessage(
152182
@Header String param1,
153183
@Header(name = "name", defaultValue = "bar") String param2,
154184
@Header(name = "name", defaultValue = "#{systemProperties.systemProperty}") String param3,
155185
@Header(name = "#{systemProperties.systemProperty}") String param4,
156186
String param5,
187+
@Header("foo") Optional<String> param6,
157188
@Header("nativeHeaders.param1") String nativeHeaderParam1) {
158189
}
159190

0 commit comments

Comments
 (0)