Skip to content

Commit f82e985

Browse files
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 <noreply@anthropic.com>
1 parent 941b7b2 commit f82e985

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/main/java/it/aboutbits/springboot/toolbox/parameter/PageParameter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ private PageParameter(@Nullable Integer page, @Nullable Integer size) {
3737
if (actualSize > MAX_PAGE_SIZE) {
3838
log.warn("Page size exceeded maximum [actualSize={}, maxSize={}]", actualSize, MAX_PAGE_SIZE);
3939
}
40+
if (actualSize < 1) {
41+
log.warn("Page size below minimum [actualSize={}, minSize=1]", actualSize);
42+
}
4043

4144
pageInfo = new PageInfo(
4245
Math.max(0, actualPage),
43-
Math.min(actualSize, MAX_PAGE_SIZE),
46+
Math.min(Math.max(1, actualSize), MAX_PAGE_SIZE),
4447
true
4548
);
4649
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package it.aboutbits.springboot.toolbox.parameter;
2+
3+
import org.jspecify.annotations.NullMarked;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
@NullMarked
9+
class PageParameterTest {
10+
@Test
11+
void shouldUseDefaults_forNullPageAndSize() {
12+
// when
13+
var parameter = PageParameter.of(null, null);
14+
15+
// then
16+
assertThat(parameter.page()).isZero();
17+
assertThat(parameter.size()).isEqualTo(PageParameter.DEFAULT_PAGE_SIZE());
18+
}
19+
20+
@Test
21+
void shouldClampPageToZero_forNegativePage() {
22+
// when
23+
var parameter = PageParameter.of(-5, 10);
24+
25+
// then
26+
assertThat(parameter.page()).isZero();
27+
}
28+
29+
@Test
30+
void shouldClampSizeToMaximum_forSizeAboveMaximum() {
31+
// when
32+
var parameter = PageParameter.of(0, PageParameter.MAX_PAGE_SIZE() + 1);
33+
34+
// then
35+
assertThat(parameter.size()).isEqualTo(PageParameter.MAX_PAGE_SIZE());
36+
}
37+
38+
@Test
39+
void shouldClampSizeToOne_forZeroSize() {
40+
// when
41+
var parameter = PageParameter.of(0, 0);
42+
43+
// then
44+
assertThat(parameter.size()).isEqualTo(1);
45+
}
46+
47+
@Test
48+
void shouldClampSizeToOne_forNegativeSize() {
49+
// when
50+
var parameter = PageParameter.of(0, -10);
51+
52+
// then
53+
assertThat(parameter.size()).isEqualTo(1);
54+
}
55+
56+
@Test
57+
void shouldCreateValidPageRequest_forZeroSize() {
58+
// when
59+
var pageRequest = PageParameter.of(0, 0).toPageRequest();
60+
61+
// then
62+
assertThat(pageRequest.getPageNumber()).isZero();
63+
assertThat(pageRequest.getPageSize()).isEqualTo(1);
64+
}
65+
}

0 commit comments

Comments
 (0)