Browse Source

DATACMNS-513 - PagedResourcesAssemblerArgumentResolver now correctly resolves mappings for sub-class invocations.

We're now using the newly introduced method on ControllerLinkBuilderFactory that takes both a type and a method to forward the method the invocation is happening on independently from the method being invoked.
pull/84/merge
Oliver Gierke 12 years ago
parent
commit
d41da583e9
  1. 2
      pom.xml
  2. 2
      src/main/java/org/springframework/data/web/PagedResourcesAssemblerArgumentResolver.java
  3. 34
      src/test/java/org/springframework/data/web/PagedResourcesAssemblerArgumentResolverUnitTests.java

2
pom.xml

@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
<properties>
<guava>17.0</guava>
<springhateoas>0.12.0.RELEASE</springhateoas>
<springhateoas>0.13.0.BUILD-SNAPSHOT</springhateoas>
<dist.key>DATACMNS</dist.key>
</properties>

2
src/main/java/org/springframework/data/web/PagedResourcesAssemblerArgumentResolver.java

@ -101,7 +101,7 @@ public class PagedResourcesAssemblerArgumentResolver implements HandlerMethodArg @@ -101,7 +101,7 @@ public class PagedResourcesAssemblerArgumentResolver implements HandlerMethodArg
private UriComponents resolveBaseUri(MethodParameter parameter) {
try {
Link linkToMethod = linkBuilderFactory.linkTo(parameter.getMethod(), new Object[0]).withSelfRel();
Link linkToMethod = linkBuilderFactory.linkTo(parameter.getDeclaringClass(), parameter.getMethod()).withSelfRel();
return UriComponentsBuilder.fromUriString(linkToMethod.getHref()).build();
} catch (IllegalArgumentException o_O) {
return null;

34
src/test/java/org/springframework/data/web/PagedResourcesAssemblerArgumentResolverUnitTests.java

@ -27,7 +27,9 @@ import org.junit.rules.ExpectedException; @@ -27,7 +27,9 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Pageable;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.UriComponents;
/**
* Unit tests for {@link PagedResourcesAssemblerArgumentResolver}.
@ -140,6 +142,30 @@ public class PagedResourcesAssemblerArgumentResolverUnitTests { @@ -140,6 +142,30 @@ public class PagedResourcesAssemblerArgumentResolverUnitTests {
assertThat(result, is(notNullValue()));
}
/**
* @see DATACMNS-513
*/
@Test
public void detectsMappingOfInvokedSubType() throws Exception {
Method method = Controller.class.getMethod("methodWithMapping", PagedResourcesAssembler.class);
// Simulate HandlerMethod.HandlerMethodParameter.getDeclaringClass()
// as it's returning the invoked class as the declared one
MethodParameter methodParameter = new MethodParameter(method, 0) {
public java.lang.Class<?> getDeclaringClass() {
return SubController.class;
}
};
Object result = resolver.resolveArgument(methodParameter, null, null, null);
assertThat(result, is(instanceOf(PagedResourcesAssembler.class)));
UriComponents uriComponents = (UriComponents) ReflectionTestUtils.getField(result, "baseUri");
assertThat(uriComponents.getPath(), is("/foo/mapping"));
}
private void assertSelectsParameter(Method method, int expectedIndex) throws Exception {
MethodParameter parameter = new MethodParameter(method, 0);
@ -190,5 +216,13 @@ public class PagedResourcesAssemblerArgumentResolverUnitTests { @@ -190,5 +216,13 @@ public class PagedResourcesAssemblerArgumentResolverUnitTests {
@RequestMapping("/{variable}/foo")
void methodWithPathVariable(PagedResourcesAssembler<Object> assembler);
@RequestMapping("/mapping")
Object methodWithMapping(PagedResourcesAssembler<Object> pageable);
}
@RequestMapping("/foo")
interface SubController extends Controller {
}
}

Loading…
Cancel
Save