Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-692

IllegalArgumentExceptions in PageableHandlerMethodArgumentResolver with one-based index parameters

    XMLWordPrintable

    Details

    • Sprint:
      57 - Fowler Aftermath

      Description

      When the PageableHandlerMethodArgumentResolver is configured with one-based index parameters:

      resolver.setOneIndexedParameters(true);
      

      and a zero or negative page parameter is provided, e.g. ?page=0 or ?page=-1, an IllegalArgumentException is thrown, rather than falling back to the first page.

      java.lang.IllegalArgumentException: Page index must not be less than zero!
      	at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:43) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
      	at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:63) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
      	at org.springframework.data.web.PageableHandlerMethodArgumentResolver.resolveArgument(PageableHandlerMethodArgumentResolver.java:253) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
      	. . .
      

      The page is initially set to the appropriate default page number of 0, but then, one is subtracted from that value, resulting in a negative value, which is passed to the PageRequest constructor, resulting in the IllegalArgumentException:

      PageableHandlerMethodArgumentResolver#resolveArgument
      int page = StringUtils.hasText(pageString) ? parseAndApplyBoundaries(pageString, 0, Integer.MAX_VALUE)
      				- (oneIndexedParameters ? 1 : 0) : defaultOrFallback.getPageNumber();
      
      . . . 
      
      return new PageRequest(page, pageSize, sort);
      

      There may be multiple ways to solve this problem, but one approach could be to validate the lower bound after the page var is initially set, in a similar way as the pageSize:

      PageableHandlerMethodArgumentResolver#resolveArgument
      // Limit lower bound
      pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize;
      // TODO add similar lower bound limit for page param as well...
      

        Attachments

          Activity

            People

            Assignee:
            olivergierke Oliver Drotbohm
            Reporter:
            shelleyb Shelley J. Baker
            Last updater:
            Oliver Drotbohm
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: