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:
@@ -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)) {
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.web;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.domain.Sort.Direction.*;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
@@ -182,6 +184,20 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
|
||||
assertThat(resolveSort(request, getParameterOfMethod("containeredDefault"))).isEqualTo(Sort.by("foo", "bar"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1551
|
||||
public void resolvesDotOnlyInputToDefault() {
|
||||
|
||||
Stream.of(".", ".,ASC").forEach(it -> {
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addParameter("sort", it);
|
||||
|
||||
assertThatCode(() -> {
|
||||
assertThat(resolveSort(request, PARAMETER)).isEqualTo(Sort.unsorted());
|
||||
}).doesNotThrowAnyException();
|
||||
});
|
||||
}
|
||||
|
||||
private static Sort resolveSort(HttpServletRequest request, MethodParameter parameter) throws Exception {
|
||||
|
||||
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
|
||||
|
||||
Reference in New Issue
Block a user