Compatible with negative page number and size (#5311)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.13.x

#### What this PR does / why we need it:

This PR makes Halo compatible with negative page number and size parameters to resolve <https://github.com/halo-dev/halo/issues/5298>.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/5298

#### Special notes for your reviewer:

Try to install [Hao theme](https://github.com/liuzhihang/halo-theme-hao/releases/tag/v1.4.7) and [Chirpy theme](https://github.com/AirboZH/halo-theme-chirpy/releases/tag/v1.3.3) and check the frontend pages.

#### Does this PR introduce a user-facing change?

```release-note
修复因限制分页参数导致部分主题页面无法正常渲染的问题。
```
pull/5325/head
John Niang 2024-02-02 20:30:05 +08:00 committed by GitHub
parent 27db40f7f6
commit b4e196372d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View File

@ -13,8 +13,12 @@ public class PageRequestImpl implements PageRequest {
public PageRequestImpl(int pageNumber, int pageSize, Sort sort) {
Assert.notNull(sort, "Sort must not be null");
Assert.isTrue(pageNumber >= 0, "Page index must not be less than zero!");
Assert.isTrue(pageSize >= 0, "Page size must not be less than one!");
if (pageNumber < 1) {
pageNumber = 1;
}
if (pageSize < 0) {
pageSize = 0;
}
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.sort = sort;

View File

@ -0,0 +1,27 @@
package run.halo.app.extension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Random;
import org.junit.jupiter.api.RepeatedTest;
import org.springframework.data.domain.Sort;
class PageRequestImplTest {
@RepeatedTest(10)
void shouldBeCompatibleZeroAndNegativePageNumber() {
var randomPageNumber = -(new Random().nextInt(0, Integer.MAX_VALUE));
var page = new PageRequestImpl(randomPageNumber, 10, Sort.unsorted());
assertEquals(1, page.getPageNumber());
assertEquals(10, page.getPageSize());
}
@RepeatedTest(10)
void shouldBeCompatibleNegativePageSize() {
var randomPageSize = -(new Random().nextInt(1, Integer.MAX_VALUE));
var page = new PageRequestImpl(10, randomPageSize, Sort.unsorted());
assertEquals(10, page.getPageNumber());
assertEquals(0, page.getPageSize());
}
}