DATAREST-346 - Fixed invocation of overridden delete(…) methods.

We now defensively invoke the ConversionService prior to the reflective invocation of the delete method as we have to assume the clients providing the id value in a raw form, i.e. as obtained from the request and thus most likely as String.

Related ticket: DATAREST-335.
This commit is contained in:
Oliver Gierke
2014-07-02 13:26:03 +02:00
parent 15b47b1f03
commit a29d625086
2 changed files with 8 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -200,14 +201,14 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeDelete(java.io.Serializable)
*/
@Override
@SuppressWarnings("unchecked")
public void invokeDelete(Serializable id) {
Method method = methods.getDeleteMethod();
Class<?> parameterType = method.getParameterTypes()[0];
List<Class<? extends Serializable>> idTypes = Arrays.asList(information.getIdType(), Serializable.class);
if (parameterType.equals(information.getIdType())) {
invoke(method, id);
} else if (parameterType.equals(Serializable.class)) {
if (idTypes.contains(parameterType)) {
invoke(method, convertId(id));
} else {
invoke(method, invokeFindOne(id));

View File

@@ -151,7 +151,7 @@ public class ReflectionRepositoryInvokerIntegrationTests extends AbstractIntegra
}
/**
* @see DATAREST-335
* @see DATAREST-335, DATAREST-346
*/
@Test
public void invokesOverriddenDeleteMethodCorrectly() {
@@ -168,7 +168,9 @@ public class ReflectionRepositoryInvokerIntegrationTests extends AbstractIntegra
ReflectionRepositoryInvoker invoker = new ReflectionRepositoryInvoker(repository,
factory.getRepositoryInformation(), conversionService);
invoker.invokeDelete(id);
// We must assume a non matching type here as clients might provide the raw ID value obtained from the request
invoker.invokeDelete(id.toString());
verify((CustomRepo) repository, times(1)).delete(id);
verify(repository, times(0)).findOne(Matchers.any(ObjectId.class));