Browse Source

DATACMNS-1277 - Fix invocation of redeclared delete(…) method in ReflectionRepositoryInvoker.

As a consequence of our renaming efforts in CRUD methods, we lost some generics lookup information to distinguish deleteById(ID) from delete(T) solely based on the method parameter type in case of an intermediate generic repository redeclaring the method as the bound is now expanded to Object and not Serializable, as it did in 1.x.

We now rather check for the method name ending in …ById which is much simpler anyway.
pull/303/head
Oliver Gierke 8 years ago
parent
commit
553eefa480
  1. 4
      src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java
  2. 37
      src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java

4
src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java

@ -165,10 +165,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { @@ -165,10 +165,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
Method method = methods.getDeleteMethod()
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a delete-method declared!"));
Class<?> parameterType = method.getParameterTypes()[0];
List<Class<?>> idTypes = Arrays.asList(idType, Object.class);
if (idTypes.contains(parameterType)) {
if (method.getName().endsWith("ById")) {
invoke(method, convertId(id));
} else {
invoke(method, this.<Object> invokeFindById(id).orElse(null));

37
src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java

@ -272,6 +272,28 @@ public class ReflectionRepositoryInvokerUnitTests { @@ -272,6 +272,28 @@ public class ReflectionRepositoryInvokerUnitTests {
});
}
@Test // DATACMNS-1277
public void invokesFindByIdBeforeDeletingOnOverride() {
DeleteByEntityOverrideSubRepository mock = mock(DeleteByEntityOverrideSubRepository.class);
doReturn(Optional.of(new Domain())).when(mock).findById(any());
getInvokerFor(mock).invokeDeleteById(1L);
verify(mock).findById(1L);
verify(mock).delete(any(Domain.class));
}
@Test // DATACMNS-1277
public void invokesDeleteByIdOnOverride() {
DeleteByIdOverrideSubRepository mock = mock(DeleteByIdOverrideSubRepository.class);
getInvokerFor(mock).invokeDeleteById(1L);
verify(mock).deleteById(1L);
}
private static RepositoryInvoker getInvokerFor(Object repository) {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repository.getClass().getInterfaces()[0]);
@ -336,4 +358,19 @@ public class ReflectionRepositoryInvokerUnitTests { @@ -336,4 +358,19 @@ public class ReflectionRepositoryInvokerUnitTests {
com.google.common.base.Optional<Domain> findById(Long id);
}
// DATACMNS-1277
interface DeleteByEntityOverrideRepository<T, ID> extends CrudRepository<T, ID> {
@Override
void delete(T entity);
}
interface DeleteByEntityOverrideSubRepository extends DeleteByEntityOverrideRepository<Domain, Long> {}
// DATACMNS-1277
interface DeleteByIdOverrideRepository<T, ID> extends Repository<T, ID> {
void deleteById(ID entity);
}
interface DeleteByIdOverrideSubRepository extends DeleteByIdOverrideRepository<Domain, Long> {}
}

Loading…
Cancel
Save