Skip to content

Commit

Permalink
fix: 解决排序设置BUG
Browse files Browse the repository at this point in the history
解决当设置 OrderBy.unsorted 后,调用 toSort 出现空异常
  • Loading branch information
limaofeng committed Sep 16, 2021
1 parent 2cd0560 commit 5fdca75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/jfantasy/framework/dao/OrderBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class OrderBy {
private String property;
private Direction direction;
private static final OrderBy UNSORTED = OrderBy.by(new OrderBy[0]);
private static final OrderBy UNSORTED = OrderBy.by();
private final List<OrderBy> orders;
private Sort.NullHandling nullHandling = Sort.NullHandling.NATIVE;

Expand Down Expand Up @@ -69,6 +69,10 @@ public static OrderBy by(List<OrderBy> orders) {
}

public Sort toSort() {
boolean isEmpty = direction == null && this.orders.isEmpty();
if (this == UNSORTED || isEmpty) {
return Sort.unsorted();
}
if (this.isMulti()) {
return Sort.by(
this.getOrders().stream()
Expand All @@ -92,7 +96,7 @@ public String toString() {
if (this.orders.isEmpty()) {
return property + "_" + this.direction.name();
}
return this.orders.stream().map(item -> item.toString()).collect(Collectors.joining(","));
return this.orders.stream().map(OrderBy::toString).collect(Collectors.joining(","));
}

public boolean isMulti() {
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/org/jfantasy/framework/dao/OrderByTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jfantasy.framework.dao;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Sort;

class OrderByTest {

@Test
void toSort() {
OrderBy orderBy = OrderBy.unsorted();
assertEquals(orderBy.toSort(), Sort.unsorted());
}
}

0 comments on commit 5fdca75

Please sign in to comment.