Skip to content

Commit bff8cab

Browse files
committed
DATACMNS-641 - Order clauses in derived repository clauses now default to ascending order.
OrderBySource now considers the Asc and Desc keywords in an OrderBy-clause optional defaulting to ascending order as Sort defaults to anyway.
1 parent 2b4d173 commit bff8cab

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616
package org.springframework.data.repository.query.parser;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.HashSet;
1921
import java.util.List;
22+
import java.util.Set;
2023
import java.util.regex.Matcher;
2124
import java.util.regex.Pattern;
2225

@@ -36,7 +39,9 @@
3639
public class OrderBySource {
3740

3841
private static final String BLOCK_SPLIT = "(?<=Asc|Desc)(?=\\p{Lu})";
39-
private static final Pattern DIRECTION_SPLIT = Pattern.compile("(.+)(Asc|Desc)$");
42+
private static final Pattern DIRECTION_SPLIT = Pattern.compile("(.+?)(Asc|Desc)?$");
43+
private static final String INVALID_ORDER_SYNTAX = "Invalid order syntax for part %s!";
44+
private static final Set<String> DIRECTION_KEYWORDS = new HashSet<String>(Arrays.asList("Asc", "Desc"));
4045

4146
private final List<Order> orders;
4247

@@ -47,7 +52,6 @@ public class OrderBySource {
4752
* @param clause must not be {@literal null}.
4853
*/
4954
public OrderBySource(String clause) {
50-
5155
this(clause, null);
5256
}
5357

@@ -56,18 +60,30 @@ public OrderBySource(String clause) {
5660
* type.
5761
*
5862
* @param clause must not be {@literal null}.
59-
* @param domainClass
63+
* @param domainClass can be {@literal null}.
6064
*/
6165
public OrderBySource(String clause, Class<?> domainClass) {
6266

6367
this.orders = new ArrayList<Sort.Order>();
68+
6469
for (String part : clause.split(BLOCK_SPLIT)) {
70+
6571
Matcher matcher = DIRECTION_SPLIT.matcher(part);
72+
6673
if (!matcher.find()) {
67-
throw new IllegalArgumentException(String.format("Invalid order syntax for part %s!", part));
74+
throw new IllegalArgumentException(String.format(INVALID_ORDER_SYNTAX, part));
6875
}
69-
Direction direction = Direction.fromString(matcher.group(2));
70-
this.orders.add(createOrder(matcher.group(1), direction, domainClass));
76+
77+
String propertyString = matcher.group(1);
78+
String directionString = matcher.group(2);
79+
80+
// No property, but only a direction keyword
81+
if (DIRECTION_KEYWORDS.contains(propertyString) && directionString == null) {
82+
throw new IllegalArgumentException(String.format(INVALID_ORDER_SYNTAX, part));
83+
}
84+
85+
Direction direction = StringUtils.hasText(directionString) ? Direction.fromString(directionString) : null;
86+
this.orders.add(createOrder(propertyString, direction, domainClass));
7187
}
7288
}
7389

src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2010 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,7 +60,16 @@ public void usesNestedPropertyCorrectly() throws Exception {
6060

6161
OrderBySource source = new OrderBySource("BarNameDesc", Foo.class);
6262
assertThat(source.toSort(), is(new Sort(new Order(DESC, "bar.name"))));
63+
}
64+
65+
/**
66+
* @see DATACMNS-641
67+
*/
68+
@Test
69+
public void defaultsSortOrderToAscendingSort() {
6370

71+
OrderBySource source = new OrderBySource("lastname");
72+
assertThat(source.toSort(), is(new Sort("lastname")));
6473
}
6574

6675
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)