GH-2395 - Respect null values for primitives in DTO projection conversion.

Closes #2395
This commit is contained in:
Gerrit Meier
2021-10-05 12:13:16 +02:00
parent 49b7578176
commit fb38347284
2 changed files with 11 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -102,11 +103,17 @@ public final class EntityFromDtoInstantiatingConverter<T> implements Converter<O
PersistentPropertyAccessor<?> sourceAccessor) {
String targetPropertyName = targetProperty.getName();
Class<?> targetPropertyType = targetProperty.getType();
PersistentProperty<?> sourceProperty = sourceEntity.getPersistentProperty(targetPropertyName);
Object propertyValue = null;
if (sourceProperty != null) {
return sourceAccessor.getProperty(sourceProperty);
propertyValue = sourceAccessor.getProperty(sourceProperty);
}
return null;
if (propertyValue == null && targetPropertyType.isPrimitive()) {
return ReflectionUtils.getPrimitiveDefault(targetPropertyType);
}
return propertyValue;
}
}

View File

@@ -30,6 +30,8 @@ public class Person {
private String firstName;
private String lastName;
private int primitiveValue; // never used but always null
@Relationship("LIVES_AT") private Address address;
/**