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.
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
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> {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user