DATACMNS-563 - PagedResourcesAssembler now correctly forwards one-index settings to PageMetadata.

Original pull request: #267.
This commit is contained in:
Marcel Overdijk
2017-12-30 12:12:47 +01:00
committed by Oliver Gierke
parent 9a5cd29e3c
commit 7c0ec77e40
2 changed files with 11 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @since 1.6
* @author Oliver Gierke
* @author Nick Williams
* @author Marcel Overdijk
*/
public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, PagedResources<Resource<T>>> {
@@ -285,10 +286,13 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @param page must not be {@literal null}.
* @return
*/
private static <T> PageMetadata asPageMetadata(Page<T> page) {
private PageMetadata asPageMetadata(Page<?> page) {
Assert.notNull(page, "Page must not be null!");
return new PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(), page.getTotalPages());
int number = pageableResolver.isOneIndexedParameters() ? page.getNumber() + 1 : page.getNumber();
return new PageMetadata(page.getSize(), number, page.getTotalElements(), page.getTotalPages());
}
private static class SimplePagedResourceAssembler<T> implements ResourceAssembler<T, Resource<T>> {

View File

@@ -48,6 +48,7 @@ import org.springframework.web.util.UriComponentsBuilder;
*
* @author Oliver Gierke
* @author Nick Williams
* @author Marcel Overdijk
*/
public class PagedResourcesAssemblerUnitTests {
@@ -164,6 +165,9 @@ public class PagedResourcesAssemblerUnitTests {
assertThat(resource.hasLink("prev"), is(true));
assertThat(resource.hasLink("next"), is(true));
// We expect 2 as the created page has index 1. Pages itself are always 0 indexed, so we created page 2 above.
assertThat(resource.getMetadata().getNumber(), is(2L));
assertThat(getQueryParameters(resource.getLink("prev")), hasEntry("page", "1"));
assertThat(getQueryParameters(resource.getLink("next")), hasEntry("page", "3"));
}