Skip to content

Commit d471949

Browse files
committed
Empty string check after conversion in AbstractNamedValueArgumentResolver
Closes gh-33794
1 parent 59ec871 commit d471949

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.lang.Nullable;
3333
import org.springframework.util.Assert;
3434
import org.springframework.util.ObjectUtils;
35+
import org.springframework.util.StringUtils;
3536
import org.springframework.web.bind.annotation.ValueConstants;
3637

3738
/**
@@ -191,11 +192,15 @@ private void addSingleValue(
191192
}
192193

193194
if (this.conversionService != null && !(value instanceof String)) {
195+
Object beforeValue = value;
194196
parameter = parameter.nestedIfOptional();
195197
Class<?> type = parameter.getNestedParameterType();
196198
value = (type != Object.class && !type.isArray() ?
197199
this.conversionService.convert(value, new TypeDescriptor(parameter), STRING_TARGET_TYPE) :
198200
this.conversionService.convert(value, String.class));
201+
if (!StringUtils.hasText((String) value) && !required && beforeValue == null) {
202+
value = null;
203+
}
199204
}
200205

201206
if (value == null) {

spring-web/src/test/java/org/springframework/web/service/invoker/NamedValueArgumentResolverTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ void dateTestValue() {
7474
assertTestValue("value", "2022-09-16");
7575
}
7676

77+
@Test // gh-33794
78+
void dateNullValue() {
79+
this.service.executeDate(null);
80+
assertTestValue("value");
81+
}
82+
7783
@Test
7884
void objectTestValue() {
7985
this.service.execute(Boolean.TRUE);
@@ -182,7 +188,7 @@ private interface Service {
182188
void executeString(@TestValue String value);
183189

184190
@GetExchange
185-
void executeDate(@TestValue @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate value);
191+
void executeDate(@Nullable @TestValue(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate value);
186192

187193
@GetExchange
188194
void execute(@TestValue Object value);

0 commit comments

Comments
 (0)