Skip to content

Ensure DataBinder once again binds null values for nullable/optional constructor arguments #31800

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

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ private Object createObject(ResolvableType objectType, String nestedPath, ValueR
Class<?> paramType = paramTypes[i];
Object value = valueResolver.resolveValue(paramPath, paramType);

if (value == null && shouldConstructArgument(param)) {
if (value == null && !param.isOptional() && shouldConstructArgument(param)) {
ResolvableType type = ResolvableType.forMethodParameter(param);
args[i] = createObject(type, paramPath + ".", valueResolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.springframework.core.ResolvableType;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -89,6 +90,20 @@ void dataClassBindingWithConversionError() {
assertThat(bindingResult.getFieldValue("param3")).isNull();
}

@Test
void dataClassBindingWithNestedClassAndNullableValue() {
MapValueResolver valueResolver = new MapValueResolver(Map.of("nested.name", "value1"));
DataBinder binder = new DataBinder(null);
binder.setTargetType(ResolvableType.forClass(OuterClass.class));

binder.construct(valueResolver);
OuterClass outerClass = getTarget(binder);

assertThat(outerClass.getNested().getName()).isEqualTo("value1");
assertThat(outerClass.getOptionalNested().isPresent()).isFalse();
assertThat(outerClass.getNullableNested()).isNull();
}

@SuppressWarnings("SameParameterValue")
private static DataBinder initDataBinder(Class<DataClass> targetType) {
DataBinder binder = new DataBinder(null);
Expand Down Expand Up @@ -151,4 +166,41 @@ public Object resolveValue(String name, Class<?> type) {
}
}

private static class NestedClass {
private final String name;

public NestedClass(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

private static class OuterClass {
private final NestedClass nested;
private final Optional<NestedClass> optionalNested;
@Nullable
private final NestedClass nullableNested;

public OuterClass(NestedClass nested, Optional<NestedClass> optionalNested, @Nullable NestedClass nullableNested) {
this.nested = nested;
this.optionalNested = optionalNested;
this.nullableNested = nullableNested;
}

public NestedClass getNested() {
return nested;
}

public Optional<NestedClass> getOptionalNested() {
return optionalNested;
}

@Nullable
public NestedClass getNullableNested() {
return nullableNested;
}
}
}