diff --git a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java index 6e0aa4fb1..cae8a12b6 100644 --- a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java +++ b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java @@ -35,6 +35,7 @@ import org.springframework.util.Assert; * respectively. * * @author Oliver Gierke + * @author Thomas Darimont */ public class DefaultTypeMapper implements TypeMapper { @@ -193,12 +194,30 @@ public class DefaultTypeMapper implements TypeMapper { Assert.notNull(info); + Object alias = getAliasFor(info); + if (alias != null) { + accessor.writeTypeTo(sink, alias); + } + } + + /** + * Returns the alias to be used for the given {@link TypeInformation}. + * + * @param info must not be {@literal null} + * @return the alias for the given {@link TypeInformation} or {@literal null} of none was found or all mappers + * returned {@literal null}. + */ + protected final Object getAliasFor(TypeInformation info) { + + Assert.notNull(info); + for (TypeInformationMapper mapper : mappers) { Object alias = mapper.createAliasFor(info); if (alias != null) { - accessor.writeTypeTo(sink, alias); - return; + return alias; } } + + return null; } } diff --git a/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java b/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java index e8a27a0f5..d07a3251a 100644 --- a/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java +++ b/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java @@ -39,15 +39,14 @@ import org.springframework.data.util.TypeInformation; @RunWith(MockitoJUnitRunner.class) public class DefaultTypeMapperUnitTests { + static final TypeInformation STRING_TYPE_INFO = ClassTypeInformation.from(String.class); static final String STRING = String.class.getName(); - @Mock - TypeAliasAccessor> accessor; + @Mock TypeAliasAccessor> accessor; - @Mock - TypeInformationMapper mapper; + @Mock TypeInformationMapper mapper; - TypeMapper> typeMapper; + DefaultTypeMapper> typeMapper; Map source; @Before @@ -58,7 +57,7 @@ public class DefaultTypeMapperUnitTests { this.source = Collections.singletonMap("key", STRING); when(accessor.readAliasFrom(source)).thenReturn(STRING); - when(mapper.resolveTypeFrom(STRING)).thenReturn((TypeInformation) ClassTypeInformation.from(String.class)); + when(mapper.resolveTypeFrom(STRING)).thenReturn((TypeInformation) STRING_TYPE_INFO); } @Test @@ -66,10 +65,22 @@ public class DefaultTypeMapperUnitTests { public void cachesResolvedTypeInformation() { TypeInformation information = typeMapper.readType(source); - assertThat(information, is((TypeInformation) ClassTypeInformation.from(String.class))); + assertThat(information, is((TypeInformation) STRING_TYPE_INFO)); verify(mapper, times(1)).resolveTypeFrom(STRING); typeMapper.readType(source); verify(mapper, times(1)).resolveTypeFrom(STRING); } + + /** + * @see DATACMNS-349 + */ + @Test + public void returnsTypeAliasForInformation() { + + Object alias = "alias"; + when(mapper.createAliasFor(STRING_TYPE_INFO)).thenReturn(alias); + + assertThat(this.typeMapper.getAliasFor(STRING_TYPE_INFO), is(alias)); + } }