DATACMNS-233 - DomainClassConverter is now safe against null and empty Strings.
Added explicit check for null or empty String source before attempting conversion. Returning null for both cases.
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.core.convert.converter.Converter} to convert arbitrary input into domain classes managed
|
||||
@@ -60,6 +61,10 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
|
||||
*/
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
||||
if (source == null || !StringUtils.hasText(source.toString())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
RepositoryInformation info = repositories.getRepositoryInformationFor(targetType.getType());
|
||||
|
||||
CrudRepository<?, Serializable> repository = repositories.getRepositoryFor(targetType.getType());
|
||||
|
||||
@@ -114,6 +114,20 @@ public class DomainClassConverterUnitTests {
|
||||
assertMatches(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-233
|
||||
*/
|
||||
public void returnsNullForNullSource() {
|
||||
assertThat(converter.convert(null, sourceDescriptor, targetDescriptor), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-233
|
||||
*/
|
||||
public void returnsNullForEmptyStringSource() {
|
||||
assertThat(converter.convert("", sourceDescriptor, targetDescriptor), is(nullValue()));
|
||||
}
|
||||
|
||||
private void assertMatches(boolean matchExpected) {
|
||||
|
||||
assertThat(converter.matches(sourceDescriptor, targetDescriptor), is(matchExpected));
|
||||
|
||||
Reference in New Issue
Block a user