DATACMNS-1377 - ConvertingPropertyAccessor now properly coverts values for property paths.
Previously, ConvertingPropertyAccessor did not override PersistentPropertyAccessor.setProperty(PersistentPropertyPath, Object), so that the target value had to be of the leaf property's type. We now implement that method and convert it into that type before invoking the super method.
This commit is contained in:
@@ -24,8 +24,10 @@ import lombok.Value;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
@@ -114,6 +116,23 @@ public class PersistentPropertyAccessorUnitTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1377
|
||||
public void shouldConvertToPropertyPathLeafType() {
|
||||
|
||||
Order order = new Order(new Customer("1"));
|
||||
|
||||
PersistentPropertyAccessor<Order> accessor = context.getPersistentEntity(Order.class).getPropertyAccessor(order);
|
||||
ConvertingPropertyAccessor<Order> convertingAccessor = new ConvertingPropertyAccessor<>(accessor,
|
||||
new DefaultConversionService());
|
||||
|
||||
PersistentPropertyPath<SamplePersistentProperty> path = context.getPersistentPropertyPath("customer.firstname",
|
||||
Order.class);
|
||||
|
||||
convertingAccessor.setProperty(path, 2);
|
||||
|
||||
assertThat(convertingAccessor.getBean().getCustomer().getFirstname()).isEqualTo("2");
|
||||
}
|
||||
|
||||
@Value
|
||||
static class Order {
|
||||
Customer customer;
|
||||
|
||||
Reference in New Issue
Block a user