Skip to content

DATACMNS-583 - DomainClassConverter returns input if given types are the same. #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATACMNS-583-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<T extends ConversionService & ConverterRegistry> implements
ConditionalGenericConverter, ApplicationContextAware {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* Unit test for {@link DomainClassConverter}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class DomainClassConverterUnitTests {
Expand Down Expand Up @@ -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);
Expand Down