Browse Source

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.
pull/126/head
Thomas Darimont 11 years ago committed by Oliver Gierke
parent
commit
386644190a
  1. 13
      src/main/java/org/springframework/data/repository/support/DomainClassConverter.java
  2. 14
      src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,11 +31,12 @@ import org.springframework.util.StringUtils; @@ -31,11 +31,12 @@ import org.springframework.util.StringUtils;
/**
* {@link org.springframework.core.convert.converter.Converter} to convert arbitrary input into domain classes managed
* by Spring Data {@link CrudRepository} s. The implementation uses a {@link ConversionService} in turn to convert the
* by Spring Data {@link CrudRepository}s. The implementation uses a {@link ConversionService} in turn to convert the
* source type into the domain class' id type which is then converted into a domain class object by using a
* {@link CrudRepository}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class DomainClassConverter<T extends ConversionService & ConverterRegistry> implements
ConditionalGenericConverter, ApplicationContextAware {
@ -65,6 +66,10 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -65,6 +66,10 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
return null;
}
if (sourceType.equals(targetType)) {
return source;
}
Class<?> domainType = targetType.getType();
RepositoryInformation info = repositories.getRepositoryInformationFor(domainType);
@ -83,6 +88,10 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -83,6 +88,10 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
return false;
}
if (sourceType.equals(targetType)) {
return true;
}
return conversionService.canConvert(sourceType.getType(),
repositories.getRepositoryInformationFor(targetType.getType()).getIdType());
}

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

@ -39,6 +39,7 @@ import org.springframework.data.repository.core.support.DummyRepositoryFactoryBe @@ -39,6 +39,7 @@ import org.springframework.data.repository.core.support.DummyRepositoryFactoryBe
* Unit test for {@link DomainClassConverter}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class DomainClassConverterUnitTests {
@ -143,6 +144,19 @@ public class DomainClassConverterUnitTests { @@ -143,6 +144,19 @@ public class DomainClassConverterUnitTests {
assertThat(converter.matches(sourceDescriptor, targetDescriptor), is(true));
}
/**
* @DATACMNS-583
*/
@Test
public void shouldReturnSourceObjectIfSourceAndTargetTypesAreTheSame() {
ApplicationContext context = initContextWithRepo();
converter.setApplicationContext(context);
assertThat(converter.matches(targetDescriptor, targetDescriptor), is(true));
assertThat((User) converter.convert(USER, targetDescriptor, targetDescriptor), is(USER));
}
private ApplicationContext initContextWithRepo() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);

Loading…
Cancel
Save