Skip to content

Commit

Permalink
Compatible with negative page number and size (#5311)
Browse files Browse the repository at this point in the history
#### 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 <#5298>.

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

Fixes #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
修复因限制分页参数导致部分主题页面无法正常渲染的问题。
```
  • Loading branch information
JohnNiang authored Feb 2, 2024
1 parent 27db40f commit b4e1963
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/src/main/java/run/halo/app/extension/PageRequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions api/src/test/java/run/halo/app/extension/PageRequestImplTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit b4e1963

Please sign in to comment.