DATACMNS-1042 - PagedResourcesAssembler now keeps coordinates of supplied Page for self link.

We now use the Pageable information contained in the given Page to make sure the self link rendered by the assembler adds proper coordinates. If a base link is provided, that is used as is.
This commit is contained in:
Oliver Gierke
2017-04-18 13:17:09 +02:00
parent af05a086b6
commit a8ff4ea142
2 changed files with 15 additions and 3 deletions

View File

@@ -219,7 +219,9 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
resources.add(createLink(base, page.previousPageable(), Link.REL_PREVIOUS));
}
resources.add(createLink(base, null, Link.REL_SELF));
Pageable current = new PageRequest(page.getNumber(), page.getSize(), page.getSort());
resources.add(link == null ? createLink(base, current, Link.REL_SELF) : link.withSelfRel());
if (page.hasNext()) {
resources.add(createLink(base, page.nextPageable(), Link.REL_NEXT));

View File

@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.AbstractPageRequest;
@@ -52,6 +53,7 @@ public class PagedResourcesAssemblerUnitTests {
static final Pageable PAGEABLE = new PageRequest(0, 20);
static final Page<Person> EMPTY_PAGE = new PageImpl<Person>(Collections.<Person> emptyList(), PAGEABLE, 0);
static final Matcher<String> NO_TEMPLATE_VARIABLES = allOf(not(containsString("{")), not(containsString("}")));
HateoasPageableHandlerMethodArgumentResolver resolver = new HateoasPageableHandlerMethodArgumentResolver();
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
@@ -133,7 +135,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
Link selfLink = resources.getLink(Link.REL_SELF);
assertThat(selfLink.getHref(), endsWith("localhost"));
assertThat(selfLink.getHref(), NO_TEMPLATE_VARIABLES);
}
@Test // DATACMNS-418
@@ -171,7 +173,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
assertThat(resources.getLink(Link.REL_SELF).getHref(), endsWith("localhost"));
assertThat(resources.getLink(Link.REL_SELF).getHref(), NO_TEMPLATE_VARIABLES);
assertThat(resources.getLink(Link.REL_NEXT).getHref(), endsWith("?page=2&size=1"));
assertThat(resources.getLink(Link.REL_PREVIOUS).getHref(), endsWith("?page=0&size=1"));
}
@@ -247,6 +249,14 @@ public class PagedResourcesAssemblerUnitTests {
assertThat(assembler.toResource(EMPTY_PAGE), is(instanceOf(CustomPagedResources.class)));
}
@Test // DATACMNS-1042
public void selfLinkContainsCoordinatesForCurrentPage() {
PagedResources<Resource<Person>> resource = assembler.toResource(createPage(0));
assertThat(resource.getLink(Link.REL_SELF).getHref(), endsWith("?page=0&size=1"));
}
private static Page<Person> createPage(int index) {
AbstractPageRequest request = new PageRequest(index, 1);