DATACMNS-1743 - Fixed handling of failed aggregate lookup in DomainClassConverter.
We now fall back to null in case the repository lookup for the aggregate results in an absent value rather than returning the source unconverted.
This commit is contained in:
committed by
Oliver Drotbohm
parent
18d6e63ab0
commit
ba012ff25b
@@ -82,7 +82,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
|
||||
@Nullable
|
||||
@Override
|
||||
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return getConverter(targetType).map(it -> it.convert(source, sourceType, targetType)).orElse(source);
|
||||
return getConverter(targetType).map(it -> it.convert(source, sourceType, targetType)).orElse(null);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.AopProxyUtils;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -203,6 +204,23 @@ class DomainClassConverterUnitTests {
|
||||
assertThat(conversionService.canConvert(String.class, User.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1743
|
||||
void returnsNullForFailedLookup() {
|
||||
|
||||
ApplicationContext context = initContextWithRepo();
|
||||
converter.setApplicationContext(context);
|
||||
|
||||
// Expect ID conversion
|
||||
doReturn(4711L).when(service).convert("4711", Long.class);
|
||||
|
||||
// Configure aggregate lookup to fail
|
||||
UserRepository users = context.getBean(UserRepository.class);
|
||||
users = (UserRepository) AopProxyUtils.getSingletonTarget(users);
|
||||
doReturn(Optional.empty()).when(users).findById(any());
|
||||
|
||||
assertThat(converter.convert("4711", STRING_TYPE, USER_TYPE)).isNull();
|
||||
}
|
||||
|
||||
private ApplicationContext initContextWithRepo() {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
|
||||
|
||||
Reference in New Issue
Block a user