Browse Source

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.
1.3.x
Oliver Gierke 13 years ago
parent
commit
7c844df4d6
  1. 5
      spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java
  2. 14
      spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

5
spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

@ -27,6 +27,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; @@ -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 @@ -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());

14
spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

@ -114,6 +114,20 @@ public class DomainClassConverterUnitTests { @@ -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));

Loading…
Cancel
Save