Skip to content

Commit cc576a7

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATACMNS-349 - Opened up SPI in DefaultTypeMapper to allow subclasses to resolve TypeAliases.
Added getAliasFor(TypeInformation) method in DefaultTypeMapper to allow subclasses to resolve TypeAliases.
1 parent 2afb570 commit cc576a7

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/main/java/org/springframework/data/convert/DefaultTypeMapper.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* respectively.
3636
*
3737
* @author Oliver Gierke
38+
* @author Thomas Darimont
3839
*/
3940
public class DefaultTypeMapper<S> implements TypeMapper<S> {
4041

@@ -193,12 +194,30 @@ public void writeType(TypeInformation<?> info, S sink) {
193194

194195
Assert.notNull(info);
195196

197+
Object alias = getAliasFor(info);
198+
if (alias != null) {
199+
accessor.writeTypeTo(sink, alias);
200+
}
201+
}
202+
203+
/**
204+
* Returns the alias to be used for the given {@link TypeInformation}.
205+
*
206+
* @param info must not be {@literal null}
207+
* @return the alias for the given {@link TypeInformation} or {@literal null} of none was found or all mappers
208+
* returned {@literal null}.
209+
*/
210+
protected final Object getAliasFor(TypeInformation<?> info) {
211+
212+
Assert.notNull(info);
213+
196214
for (TypeInformationMapper mapper : mappers) {
197215
Object alias = mapper.createAliasFor(info);
198216
if (alias != null) {
199-
accessor.writeTypeTo(sink, alias);
200-
return;
217+
return alias;
201218
}
202219
}
220+
221+
return null;
203222
}
204223
}

src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
@RunWith(MockitoJUnitRunner.class)
4040
public class DefaultTypeMapperUnitTests {
4141

42+
static final TypeInformation<String> STRING_TYPE_INFO = ClassTypeInformation.from(String.class);
4243
static final String STRING = String.class.getName();
4344

44-
@Mock
45-
TypeAliasAccessor<Map<String, String>> accessor;
45+
@Mock TypeAliasAccessor<Map<String, String>> accessor;
4646

47-
@Mock
48-
TypeInformationMapper mapper;
47+
@Mock TypeInformationMapper mapper;
4948

50-
TypeMapper<Map<String, String>> typeMapper;
49+
DefaultTypeMapper<Map<String, String>> typeMapper;
5150
Map<String, String> source;
5251

5352
@Before
@@ -58,18 +57,30 @@ public void setUp() {
5857
this.source = Collections.singletonMap("key", STRING);
5958

6059
when(accessor.readAliasFrom(source)).thenReturn(STRING);
61-
when(mapper.resolveTypeFrom(STRING)).thenReturn((TypeInformation) ClassTypeInformation.from(String.class));
60+
when(mapper.resolveTypeFrom(STRING)).thenReturn((TypeInformation) STRING_TYPE_INFO);
6261
}
6362

6463
@Test
6564
@SuppressWarnings("rawtypes")
6665
public void cachesResolvedTypeInformation() {
6766

6867
TypeInformation<?> information = typeMapper.readType(source);
69-
assertThat(information, is((TypeInformation) ClassTypeInformation.from(String.class)));
68+
assertThat(information, is((TypeInformation) STRING_TYPE_INFO));
7069
verify(mapper, times(1)).resolveTypeFrom(STRING);
7170

7271
typeMapper.readType(source);
7372
verify(mapper, times(1)).resolveTypeFrom(STRING);
7473
}
74+
75+
/**
76+
* @see DATACMNS-349
77+
*/
78+
@Test
79+
public void returnsTypeAliasForInformation() {
80+
81+
Object alias = "alias";
82+
when(mapper.createAliasFor(STRING_TYPE_INFO)).thenReturn(alias);
83+
84+
assertThat(this.typeMapper.getAliasFor(STRING_TYPE_INFO), is(alias));
85+
}
7586
}

0 commit comments

Comments
 (0)