From ca2e88ae34ed54dd33c6b42b7e6642a89654e934 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 2 Jul 2014 13:26:03 +0200 Subject: [PATCH] =?UTF-8?q?DATAREST-346=20-=20Fixed=20invocation=20of=20ov?= =?UTF-8?q?erridden=20delete(=E2=80=A6)=20methods.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../data/rest/core/invoke/ReflectionRepositoryInvoker.java | 7 ++++--- .../ReflectionRepositoryInvokerIntegrationTests.java | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java index 06a164118..a07ac8828 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java @@ -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> 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)); diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java index 178369db1..5d4e4e1d2 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java @@ -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));