Skip to content

PageableHandlerMethodArgumentResolver should fall back to unpaged Pageable if necessary #3094

Closed
@j-kitch

Description

@j-kitch

Hi team,

I have the following use case, that I believe will be fairly common for rest endpoints.

Use Case

I have a use case for a controller that can support pagination and sorting, but both are optional.

class FooController {

    @GetMapping
    List<Foo> getFoos(Pageable pageable) {
        if (pageable.isPaged()) {
            return foos.fetchPage(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort());
        } else {
            return foos.fetchAll(pageable.getSort());
        }
    }
}
  • /foos will return all resources
  • /foos?page=2&size=3 will return page 2, size 3 of resources, without sorting.
  • /foos?sort=bar,desc will return all resources, sorted by property bar descending.
  • /foos?page=2&size=3&sort=bar,desc will return page 2, size 3 of the sorted resource by property bar, descending.

Issue

I have set PageableHandlerMethodArgumentResolverSupport.fallbackPageable to Pageable.unpaged().

This works for /foos, returning all items without sorting.

But there is an error when attempting /foos?sort=bar,desc due to

if (sort.isSorted()) {
return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
}

  • Attempting to construct PageRequest fails on pageable.getPageNumber() throwing UnsupportedOperationException

I would have expected the above code to have looked something like

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

as this would allow returning all items in the resource with sorting.

This feels like a bug to me, the ability to sort a resource shouldn't require pagination.

Fallback to unpaged property

As an aside, it seems strange that the fallbackPageable is the only property not settable via configuration

https://github.com/spring-projects/spring-boot/blob/22386f4ddd3589a7059891026b0fb512a46237e4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java#L61-L74

A property like spring.data.web.pageable.default-unpaged=true that can set the fallbackPageable in SpringDataWebAutoConfiguration.pageableCustomizer() to Pageable.unpaged() could make sense to me.

Thanks,

Josh

Metadata

Metadata

Assignees

Labels

in: webIntegration with Spring MVCtype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions