From f82e985da9bcb86ec9a96b1949563a66d16074bf Mon Sep 17 00:00:00 2001 From: Martin Malfertheiner Date: Thu, 27 Aug 2026 09:02:28 +0200 Subject: [PATCH] Clamp page size to a minimum of one PageParameter clamped the page to 0 and the size to MAX_PAGE_SIZE, but a size below 1 passed through unchanged. toPageRequest then called PageRequest.of, which throws IllegalArgumentException for a size below one. In consuming applications a request with size=0 ended in an unhandled exception instead of a corrected page request. The size is now clamped to a minimum of 1, with a warn log in the same style as the existing maximum clamp. Co-Authored-By: Claude Fable 5 --- .../toolbox/parameter/PageParameter.java | 5 +- .../toolbox/parameter/PageParameterTest.java | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/test/java/it/aboutbits/springboot/toolbox/parameter/PageParameterTest.java diff --git a/src/main/java/it/aboutbits/springboot/toolbox/parameter/PageParameter.java b/src/main/java/it/aboutbits/springboot/toolbox/parameter/PageParameter.java index af21d88..dbf1bf8 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/parameter/PageParameter.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/parameter/PageParameter.java @@ -37,10 +37,13 @@ private PageParameter(@Nullable Integer page, @Nullable Integer size) { if (actualSize > MAX_PAGE_SIZE) { log.warn("Page size exceeded maximum [actualSize={}, maxSize={}]", actualSize, MAX_PAGE_SIZE); } + if (actualSize < 1) { + log.warn("Page size below minimum [actualSize={}, minSize=1]", actualSize); + } pageInfo = new PageInfo( Math.max(0, actualPage), - Math.min(actualSize, MAX_PAGE_SIZE), + Math.min(Math.max(1, actualSize), MAX_PAGE_SIZE), true ); } diff --git a/src/test/java/it/aboutbits/springboot/toolbox/parameter/PageParameterTest.java b/src/test/java/it/aboutbits/springboot/toolbox/parameter/PageParameterTest.java new file mode 100644 index 0000000..3b10613 --- /dev/null +++ b/src/test/java/it/aboutbits/springboot/toolbox/parameter/PageParameterTest.java @@ -0,0 +1,65 @@ +package it.aboutbits.springboot.toolbox.parameter; + +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@NullMarked +class PageParameterTest { + @Test + void shouldUseDefaults_forNullPageAndSize() { + // when + var parameter = PageParameter.of(null, null); + + // then + assertThat(parameter.page()).isZero(); + assertThat(parameter.size()).isEqualTo(PageParameter.DEFAULT_PAGE_SIZE()); + } + + @Test + void shouldClampPageToZero_forNegativePage() { + // when + var parameter = PageParameter.of(-5, 10); + + // then + assertThat(parameter.page()).isZero(); + } + + @Test + void shouldClampSizeToMaximum_forSizeAboveMaximum() { + // when + var parameter = PageParameter.of(0, PageParameter.MAX_PAGE_SIZE() + 1); + + // then + assertThat(parameter.size()).isEqualTo(PageParameter.MAX_PAGE_SIZE()); + } + + @Test + void shouldClampSizeToOne_forZeroSize() { + // when + var parameter = PageParameter.of(0, 0); + + // then + assertThat(parameter.size()).isEqualTo(1); + } + + @Test + void shouldClampSizeToOne_forNegativeSize() { + // when + var parameter = PageParameter.of(0, -10); + + // then + assertThat(parameter.size()).isEqualTo(1); + } + + @Test + void shouldCreateValidPageRequest_forZeroSize() { + // when + var pageRequest = PageParameter.of(0, 0).toPageRequest(); + + // then + assertThat(pageRequest.getPageNumber()).isZero(); + assertThat(pageRequest.getPageSize()).isEqualTo(1); + } +}