DATACMNS-385 - Fixed repository method matching for iterable entities.
When matching the save(Iterable<T>) method of CrudRepository for an entity implementing Iterable we accidentally determined CrudRepository.save(T) as target method if the entity type under consideration implements Iterable. We now explicitly check for the parameter type not being Iterable when matching the domain type generics in matchesGenericType(…).
This commit is contained in:
@@ -184,7 +184,7 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements
|
||||
* @return
|
||||
*/
|
||||
private boolean isQueryMethodCandidate(Method method) {
|
||||
return isQueryAnnotationPresentOn(method) || (!isCustomMethod(method) && !isBaseClassMethod(method));
|
||||
return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -352,7 +352,13 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements
|
||||
Type boundType = variable.getBounds()[0];
|
||||
String referenceName = boundType instanceof TypeVariable ? boundType.toString() : variable.toString();
|
||||
|
||||
if (DOMAIN_TYPE_NAME.equals(referenceName) && parameterType.isAssignableFrom(entityType)) {
|
||||
boolean isDomainTypeVariableReference = DOMAIN_TYPE_NAME.equals(referenceName);
|
||||
boolean parameterMatchesEntityType = parameterType.isAssignableFrom(entityType);
|
||||
|
||||
// We need this check to besure not to match save(Iterable) for entities implementing Iterable
|
||||
boolean isNotIterable = !parameterType.equals(Iterable.class);
|
||||
|
||||
if (isDomainTypeVariableReference && parameterMatchesEntityType && isNotIterable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
@@ -9,6 +24,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
@@ -29,6 +46,8 @@ import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadataUnitTests.DummyGenericRepositorySupport;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultRepositoryInformation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -174,6 +193,21 @@ public class DefaultRepositoryInformationUnitTests {
|
||||
assertThat(information.getQueryMethods(), hasItem(method));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-385
|
||||
*/
|
||||
@Test
|
||||
public void findsTargetSaveForIterableIfEntityImplementsIterable() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(BossRepository.class);
|
||||
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, null);
|
||||
|
||||
Method method = BossRepository.class.getMethod("save", Iterable.class);
|
||||
Method reference = CrudRepository.class.getMethod("save", Iterable.class);
|
||||
|
||||
assertThat(information.getTargetClassMethod(method), is(reference));
|
||||
}
|
||||
|
||||
private Method getMethodFrom(Class<?> type, String name) {
|
||||
for (Method method : type.getMethods()) {
|
||||
if (method.getName().equals(name)) {
|
||||
@@ -215,6 +249,14 @@ public class DefaultRepositoryInformationUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class Boss implements Iterable<User> {
|
||||
|
||||
@Override
|
||||
public Iterator<User> iterator() {
|
||||
return Collections.<User> emptySet().iterator();
|
||||
}
|
||||
}
|
||||
|
||||
interface BaseRepository<S, ID extends Serializable> extends CrudRepository<S, ID> {
|
||||
|
||||
S findBySomething(String something);
|
||||
@@ -255,4 +297,6 @@ public class DefaultRepositoryInformationUnitTests {
|
||||
|
||||
Object save(Object object);
|
||||
}
|
||||
|
||||
interface BossRepository extends CrudRepository<Boss, Long> {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user