Skip to content

Commit

Permalink
Empty string check after conversion in AbstractNamedValueArgumentReso…
Browse files Browse the repository at this point in the history
…lver

Closes gh-33794
  • Loading branch information
rstoyanchev committed Nov 8, 2024
1 parent 59ec871 commit d471949
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ValueConstants;

/**
Expand Down Expand Up @@ -191,11 +192,15 @@ private void addSingleValue(
}

if (this.conversionService != null && !(value instanceof String)) {
Object beforeValue = value;
parameter = parameter.nestedIfOptional();
Class<?> type = parameter.getNestedParameterType();
value = (type != Object.class && !type.isArray() ?
this.conversionService.convert(value, new TypeDescriptor(parameter), STRING_TARGET_TYPE) :
this.conversionService.convert(value, String.class));
if (!StringUtils.hasText((String) value) && !required && beforeValue == null) {
value = null;
}
}

if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ void dateTestValue() {
assertTestValue("value", "2022-09-16");
}

@Test // gh-33794
void dateNullValue() {
this.service.executeDate(null);
assertTestValue("value");
}

@Test
void objectTestValue() {
this.service.execute(Boolean.TRUE);
Expand Down Expand Up @@ -182,7 +188,7 @@ private interface Service {
void executeString(@TestValue String value);

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

@GetExchange
void execute(@TestValue Object value);
Expand Down

0 comments on commit d471949

Please sign in to comment.