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:
Oliver Gierke
2012-09-17 09:53:20 +02:00
parent a83b8b7a48
commit 7c844df4d6
2 changed files with 19 additions and 0 deletions

View File

@@ -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());

View File

@@ -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));