Skip to content

PageableHandlerMethodArgumentResolver should fall back to unpaged Pageable if necessary #3094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
j-kitch opened this issue May 9, 2024 · 1 comment
Assignees
Labels
in: web Integration with Spring MVC type: bug A general bug

Comments

@j-kitch
Copy link

j-kitch commented May 9, 2024

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 9, 2024
@quaff
Copy link
Contributor

quaff commented May 10, 2024

It should be fixed by #2865.

@mp911de mp911de linked a pull request May 13, 2024 that will close this issue
odrotbohm pushed a commit that referenced this issue May 14, 2024
The (Reactive)PageableHandlerMethodArgumentResolver now falls back to a unpaged Pageable instance with a resolved sort if the the resolved Pageable is unpaged.

Fixes: GH-3094
Original pull request: GH-2865
odrotbohm added a commit that referenced this issue May 14, 2024
Ternary ifs and less nesting.

Related: GH-3094
Original pull request: GH-2865
odrotbohm added a commit that referenced this issue May 14, 2024
Ternary ifs and less nesting.

Related: GH-3094
Original pull request: GH-2865
@odrotbohm odrotbohm added this to the 3.2.6 (2023.1.6) milestone May 14, 2024
@odrotbohm odrotbohm removed the status: waiting-for-triage An issue we've not yet triaged label May 14, 2024
@odrotbohm odrotbohm changed the title Sorting with optional pagination PageableHandlerMethodArgumentResolver should fallback to unpaged Pageable if necessary May 14, 2024
@odrotbohm odrotbohm changed the title PageableHandlerMethodArgumentResolver should fallback to unpaged Pageable if necessary PageableHandlerMethodArgumentResolver should fall back to unpaged Pageable if necessary May 14, 2024
@odrotbohm odrotbohm added in: web Integration with Spring MVC type: bug A general bug labels May 14, 2024
natedanner pushed a commit to natedanner/spring-projects__spring-data-commons that referenced this issue May 20, 2024
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
natedanner pushed a commit to natedanner/spring-projects__spring-data-commons that referenced this issue May 20, 2024
Ternary ifs and less nesting.

Related: spring-projectsGH-3094
Original pull request: spring-projectsGH-2865
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Integration with Spring MVC type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants