DATACMNS-1551 - SortHandlerMethodArgumentResolver now drops dot-only property segments.

If plain dots were submitted as elements in a Sort expression to be parsed by SortHandlerMethodArgumentResolver, those dots would be considered a property of the sort expression, which is of course wrong. We now drop property candidates solely consisting of dots and whitespace.
This commit is contained in:
Oliver Drotbohm
2019-07-01 14:04:10 +02:00
parent bf1e00e93f
commit 4c1aed27d6
2 changed files with 30 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.web;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -221,7 +222,9 @@ public class SortHandlerMethodArgumentResolver implements SortArgumentResolver {
continue;
}
String[] elements = part.split(delimiter);
String[] elements = Arrays.stream(part.split(delimiter)) //
.filter(SortHandlerMethodArgumentResolver::notOnlyDots) //
.toArray(String[]::new);
Optional<Direction> direction = elements.length == 0 ? Optional.empty()
: Direction.fromOptionalString(elements[elements.length - 1]);
@@ -236,6 +239,16 @@ public class SortHandlerMethodArgumentResolver implements SortArgumentResolver {
return allOrders.isEmpty() ? Sort.unsorted() : Sort.by(allOrders);
}
/**
* Returns whether the given source {@link String} consists of dots only.
*
* @param source must not be {@literal null}.
* @return
*/
private static boolean notOnlyDots(String source) {
return StringUtils.hasText(source.replace(".", ""));
}
private static Optional<Order> toOrder(String property, Optional<Direction> direction) {
if (!StringUtils.hasText(property)) {