Skip to content

Commit

Permalink
Use Pageable.unpaged(sort) for sorted unpaged pageable.
Browse files Browse the repository at this point in the history
The (Reactive)PageableHandlerMethodArgumentResolver now falls back to a unpaged Pageable instance with a resolved sort if the the resolved Pageable is unpaged.

Fixes: spring-projectsGH-3094
Original pull request: spring-projectsGH-2865
  • Loading branch information
quaff authored and app committed May 20, 2024
1 parent 01f6bca commit 5155112
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Nick Williams
* @author Mark Paluch
* @author Christoph Strobl
* @author Yanming Zhou
* @since 1.6
*/
public class PageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolverSupport
Expand Down Expand Up @@ -83,7 +84,12 @@ public Pageable resolveArgument(MethodParameter methodParameter, @Nullable Model
Pageable pageable = getPageable(methodParameter, page, pageSize);

if (sort.isSorted()) {
return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
if (pageable.isPaged()) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
}
else {
pageable = Pageable.unpaged(sort);
}
}

return pageable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @since 2.2
* @author Mark Paluch
* @author Yanming Zhou
*/
public class ReactivePageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolverSupport
implements SyncHandlerMethodArgumentResolver {
Expand Down Expand Up @@ -75,9 +76,17 @@ public Pageable resolveArgumentValue(MethodParameter parameter, BindingContext b
String pageSize = queryParams.getFirst(getParameterNameToUse(getSizeParameterName(), parameter));

Sort sort = sortResolver.resolveArgumentValue(parameter, bindingContext, exchange);

Pageable pageable = getPageable(parameter, page, pageSize);

return sort.isSorted() ? PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort) : pageable;
if (sort.isSorted()) {
if (pageable.isPaged()) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
}
else {
pageable = Pageable.unpaged(sort);
}
}

return pageable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.SortDefault.SortDefaults;
import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -37,6 +38,7 @@
* @author Nick Williams
* @author Vedran Pavic
* @author Mark Paluch
* @author Yanming Zhou
*/
class PageableHandlerMethodArgumentResolverUnitTests extends PageableDefaultUnitTests {

Expand Down Expand Up @@ -159,7 +161,7 @@ void returnsUnpagedIfFallbackIsUnpagedAndNoParametersGiven() throws Exception {
}

@Test // DATACMNS-477
void returnsFallbackIfOnlyPageIsGiven() throws Exception {
void returnsFallbackIfOnlyPageIsGiven() {

var resolver = getResolver();
resolver.setFallbackPageable(Pageable.unpaged());
Expand All @@ -171,8 +173,35 @@ void returnsFallbackIfOnlyPageIsGiven() throws Exception {
.isEqualTo(Pageable.unpaged());
}

@Test
void returnsSortedUnpagedIfOnlyPageAndSortIsGiven() {

var resolver = getResolver();
resolver.setFallbackPageable(Pageable.unpaged());

var request = new MockHttpServletRequest();
request.addParameter("page", "20");
request.addParameter("sort", "foo");

assertThat(resolver.resolveArgument(supportedMethodParameter, null, new ServletWebRequest(request), null))
.isEqualTo(Pageable.unpaged(Sort.by("foo")));
}

@Test
void returnsSortedUnpagedIfOnlySortIsGiven() {

var resolver = getResolver();
resolver.setFallbackPageable(Pageable.unpaged());

var request = new MockHttpServletRequest();
request.addParameter("sort", "foo");

assertThat(resolver.resolveArgument(supportedMethodParameter, null, new ServletWebRequest(request), null))
.isEqualTo(Pageable.unpaged(Sort.by("foo")));
}

@Test // DATACMNS-477
void returnsFallbackIfFallbackIsUnpagedAndOnlySizeIsGiven() throws Exception {
void returnsFallbackIfFallbackIsUnpagedAndOnlySizeIsGiven() {

var resolver = getResolver();
resolver.setFallbackPageable(Pageable.unpaged());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.SortDefault.SortDefaults;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
Expand All @@ -35,6 +36,7 @@
* Unit tests for {@link ReactivePageableHandlerMethodArgumentResolver}.
*
* @author Mark Paluch
* @author Yanming Zhou
*/
class ReactivePageableHandlerMethodArgumentResolverUnitTests {

Expand Down Expand Up @@ -150,6 +152,28 @@ void returnsFallbackIfOnlyPageIsGiven() {
assertThat(resolve(resolver, request)).isEqualTo(Pageable.unpaged());
}

@Test
void returnsSortedUnpagedIfOnlyPageAndSortIsGiven() {

var resolver = getReactiveResolver();
resolver.setFallbackPageable(Pageable.unpaged());

var request = MockServerHttpRequest.get("foo?page=20&sort=test").build();

assertThat(resolve(resolver, request)).isEqualTo(Pageable.unpaged(Sort.by("test")));
}

@Test
void returnsSortedUnpagedIfOnlySortIsGiven() {

var resolver = getReactiveResolver();
resolver.setFallbackPageable(Pageable.unpaged());

var request = MockServerHttpRequest.get("foo?sort=test").build();

assertThat(resolve(resolver, request)).isEqualTo(Pageable.unpaged(Sort.by("test")));
}

@Test // DATACMNS-1211
void returnsFallbackIfFallbackIsUnpagedAndOnlySizeIsGiven() {

Expand Down

0 comments on commit 5155112

Please sign in to comment.