Browse Source

DATACMNS-444 - DefaultPersistentPropertyPath now skips null converted names.

If a Converter handed into DefaultPersistentPropertyPath.toDotPath(…) returns null or an empty string for a mapped property name, that value is skipped during path construction.
pull/65/merge
Oliver Gierke 12 years ago
parent
commit
4cee676608
  1. 9
      src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java
  2. 41
      src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTests.java

9
src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java

@ -93,10 +93,15 @@ class DefaultPersistentPropertyPath<T extends PersistentProperty<T>> implements @@ -93,10 +93,15 @@ class DefaultPersistentPropertyPath<T extends PersistentProperty<T>> implements
List<String> result = new ArrayList<String>();
for (T property : properties) {
result.add(converterToUse.convert(property));
String convert = converterToUse.convert(property);
if (StringUtils.hasText(convert)) {
result.add(convert);
}
}
return StringUtils.collectionToDelimitedString(result, delimiterToUse);
return result.isEmpty() ? null : StringUtils.collectionToDelimitedString(result, delimiterToUse);
}
/*

41
src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTests.java

@ -38,11 +38,9 @@ import org.springframework.data.mapping.PersistentProperty; @@ -38,11 +38,9 @@ import org.springframework.data.mapping.PersistentProperty;
@RunWith(MockitoJUnitRunner.class)
public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty<T>> {
@Mock
T first, second;
@Mock T first, second;
@Mock
Converter<T, String> converter;
@Mock Converter<T, String> converter;
PersistentPropertyPath<T> oneLeg;
PersistentPropertyPath<T> twoLegs;
@ -121,4 +119,39 @@ public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty< @@ -121,4 +119,39 @@ public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty<
assertThat(oneLeg.getLength(), is(1));
assertThat(twoLegs.getLength(), is(2));
}
/**
* @see DATACMNS-444
*/
@Test
public void skipsMappedPropertyNameIfConverterReturnsNull() {
String result = twoLegs.toDotPath(new Converter<T, String>() {
@Override
public String convert(T source) {
return null;
}
});
assertThat(result, is(nullValue()));
}
/**
* @see DATACMNS-444
*/
@Test
public void skipsMappedPropertyNameIfConverterReturnsEmptyStrings() {
String result = twoLegs.toDotPath(new Converter<T, String>() {
@Override
public String convert(T source) {
return "";
}
});
assertThat(result, is(nullValue()));
}
}

Loading…
Cancel
Save