Skip to content

Commit e02cecd

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATACMNS-583 - DomainClassConverter returns input if given types are the same.
DomainClassConverter now returns input when the source type is identical to the target type. Previously we erroneously tried to convert the sourceType to the idType of the targetType's backing repository. As a side effect we also avoid performing some unnecessary converter hops. Original pull request: #101.
1 parent 47e2c84 commit e02cecd

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,11 +31,12 @@
3131

3232
/**
3333
* {@link org.springframework.core.convert.converter.Converter} to convert arbitrary input into domain classes managed
34-
* by Spring Data {@link CrudRepository} s. The implementation uses a {@link ConversionService} in turn to convert the
34+
* by Spring Data {@link CrudRepository}s. The implementation uses a {@link ConversionService} in turn to convert the
3535
* source type into the domain class' id type which is then converted into a domain class object by using a
3636
* {@link CrudRepository}.
3737
*
3838
* @author Oliver Gierke
39+
* @author Thomas Darimont
3940
*/
4041
public class DomainClassConverter<T extends ConversionService & ConverterRegistry> implements
4142
ConditionalGenericConverter, ApplicationContextAware {
@@ -65,6 +66,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
6566
return null;
6667
}
6768

69+
if (sourceType.equals(targetType)) {
70+
return source;
71+
}
72+
6873
Class<?> domainType = targetType.getType();
6974

7075
RepositoryInformation info = repositories.getRepositoryInformationFor(domainType);
@@ -83,6 +88,10 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
8388
return false;
8489
}
8590

91+
if (sourceType.equals(targetType)) {
92+
return true;
93+
}
94+
8695
return conversionService.canConvert(sourceType.getType(),
8796
repositories.getRepositoryInformationFor(targetType.getType()).getIdType());
8897
}

src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* Unit test for {@link DomainClassConverter}.
4040
*
4141
* @author Oliver Gierke
42+
* @author Thomas Darimont
4243
*/
4344
@RunWith(MockitoJUnitRunner.class)
4445
public class DomainClassConverterUnitTests {
@@ -143,6 +144,19 @@ public void discoversFactoryAndRepoFromParentApplicationContext() {
143144
assertThat(converter.matches(sourceDescriptor, targetDescriptor), is(true));
144145
}
145146

147+
/**
148+
* @DATACMNS-583
149+
*/
150+
@Test
151+
public void shouldReturnSourceObjectIfSourceAndTargetTypesAreTheSame() {
152+
153+
ApplicationContext context = initContextWithRepo();
154+
converter.setApplicationContext(context);
155+
156+
assertThat(converter.matches(targetDescriptor, targetDescriptor), is(true));
157+
assertThat((User) converter.convert(USER, targetDescriptor, targetDescriptor), is(USER));
158+
}
159+
146160
private ApplicationContext initContextWithRepo() {
147161

148162
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);

0 commit comments

Comments
 (0)