From 7296263ac4e73891cb6cd50b3cac5cdad5c588bf Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 28 Oct 2014 15:16:43 +0100 Subject: [PATCH 1/2] DATACMNS-583 - DomainClassConverter#matches should return false when source- and targetType are equal. Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 44b1e51deb..4eb0f3e45d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATACMNS-583-SNAPSHOT Spring Data Core From b9900cf270b33ef721c7820d1475830cbf295529 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 28 Oct 2014 15:57:34 +0100 Subject: [PATCH 2/2] DATACMNS-583 - DomainClassConverter returns input if given types are the same. DomainClassConverter now returns input when sourceType equals targetType. 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. Origial pull request: #101. --- .../repository/support/DomainClassConverter.java | 13 +++++++++++-- .../support/DomainClassConverterUnitTests.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java b/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java index b2ce5242cd..948b332e5c 100644 --- a/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java +++ b/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java @@ -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 @@ /** * {@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 implements ConditionalGenericConverter, ApplicationContextAware { @@ -65,6 +66,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t return null; } + if(sourceType.equals(targetType)) { + return source; + } + Class domainType = targetType.getType(); RepositoryInformation info = repositories.getRepositoryInformationFor(domainType); @@ -82,6 +87,10 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (!repositories.hasRepositoryFor(targetType.getType())) { return false; } + + if(sourceType.equals(targetType)) { + return true; + } return conversionService.canConvert(sourceType.getType(), repositories.getRepositoryInformationFor(targetType.getType()).getIdType()); diff --git a/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java b/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java index f78778d642..d809c24924 100644 --- a/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java @@ -39,6 +39,7 @@ * Unit test for {@link DomainClassConverter}. * * @author Oliver Gierke + * @author Thomas Darimont */ @RunWith(MockitoJUnitRunner.class) public class DomainClassConverterUnitTests { @@ -143,6 +144,19 @@ public void discoversFactoryAndRepoFromParentApplicationContext() { 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);