Skip to content

Address the issue of missing sort criteria in the case of Entity class (TypedSort) #2377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/java/org/springframework/data/domain/Sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Hiufung Kwok
*/
public class Sort implements Streamable<org.springframework.data.domain.Sort.Order>, Serializable {

Expand Down Expand Up @@ -188,7 +189,7 @@ public Sort and(Sort sort) {

Assert.notNull(sort, "Sort must not be null!");

ArrayList<Order> these = new ArrayList<>(this.orders);
ArrayList<Order> these = new ArrayList<>(this.getOrders());

for (Order order : sort) {
these.add(order);
Expand Down Expand Up @@ -264,6 +265,10 @@ public String toString() {
return orders.isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
}

public List<Order> getOrders() {
return orders;
}

/**
* Creates a new {@link Sort} with the current setup but the given order direction.
*
Expand Down Expand Up @@ -721,6 +726,14 @@ public Iterator<Order> iterator() {

}

@Override
public List<Order> getOrders() {

return recorded.getPropertyPath() //
.map(Sort::by) //
.orElseGet(Sort::unsorted).getOrders();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.Sort#toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.geo.Circle;

/**
* Unit test for {@link Sort}.
Expand All @@ -33,6 +34,7 @@
* @author Kevin Raymond
* @author Thomas Darimont
* @author Mark Paluch
* @author Hiufung Kwok
*/
class SortUnitTests {

Expand Down Expand Up @@ -94,6 +96,13 @@ void allowsCombiningSorts() {
assertThat(sort).containsExactly(Order.by("foo"), Order.by("bar"));
}

@Test //DATACMNS-1704
void allowsCombiningTypedSorts() {
Sort sort = Sort.sort(Circle.class).by(Circle::getCenter)
.and(Sort.sort(Circle.class).by(Circle::getRadius));
assertThat(sort).containsExactly(Order.by("center"), Order.by("radius"));
}

@Test
void handlesAdditionalNullSort() {

Expand Down