DATAREST-221 - Improved return type matching in ProjectingMethodInterceptor.
We're now using Spring's ClassUtils.isAssignableFrom(…) to make sure we match primitives and wrapper types as well.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.rest.core.projection;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link MethodInterceptor} to delegate the invocation to a different {@link MethodInterceptor} but creating a
|
||||
@@ -60,6 +61,8 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
|
||||
}
|
||||
|
||||
Class<?> returnType = invocation.getMethod().getReturnType();
|
||||
return returnType.isAssignableFrom(result.getClass()) ? result : factory.createProjection(result, returnType);
|
||||
|
||||
return ClassUtils.isAssignable(returnType, result.getClass()) ? result : factory.createProjection(result,
|
||||
returnType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.rest.core.projection;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
@@ -79,10 +80,27 @@ public class ProjectingMethodInterceptorUnitTests {
|
||||
assertThat(methodInterceptor.invoke(invocation), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-221
|
||||
*/
|
||||
@Test
|
||||
public void considersPrimitivesAsWrappers() throws Throwable {
|
||||
|
||||
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(factory, interceptor);
|
||||
|
||||
when(invocation.getMethod()).thenReturn(Helper.class.getMethod("getPrimitive"));
|
||||
when(interceptor.invoke(invocation)).thenReturn(1L);
|
||||
|
||||
assertThat(methodInterceptor.invoke(invocation), is((Object) 1L));
|
||||
verify(factory, times(0)).createProjection(anyObject(), (Class<?>) anyObject());
|
||||
}
|
||||
|
||||
interface Helper {
|
||||
|
||||
Helper getHelper();
|
||||
|
||||
String getString();
|
||||
|
||||
long getPrimitive();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user