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:15:38 +02:00
parent c9bc885df5
commit 6959234b55
2 changed files with 28 additions and 5 deletions

View File

@@ -213,16 +213,25 @@ public class SortHandlerMethodArgumentResolver implements SortArgumentResolver {
continue;
}
String[] elements = part.split(delimiter);
Direction direction = elements.length == 0 ? null : Direction.fromStringOrNull(elements[elements.length - 1]);
List<String> elements = new ArrayList<String>();
for (int i = 0; i < elements.length; i++) {
for (String candidate : part.split(delimiter)) {
if (StringUtils.hasText(candidate.replace(".", ""))) {
elements.add(candidate);
}
}
if (i == elements.length - 1 && direction != null) {
int numberOfElements = elements.size();
Direction direction = numberOfElements == 0 ? null
: Direction.fromStringOrNull(elements.get(numberOfElements - 1));
for (int i = 0; i < numberOfElements; i++) {
if (i == numberOfElements - 1 && direction != null) {
continue;
}
String property = elements[i];
String property = elements.get(i);
if (!StringUtils.hasText(property)) {
continue;

View File

@@ -19,6 +19,8 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.Sort.Direction.*;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.junit.BeforeClass;
@@ -183,6 +185,18 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
assertThat(resolveSort(request, getParameterOfMethod("containeredDefault")), is(new Sort("foo", "bar")));
}
@Test // DATACMNS-1551
public void resolvesDotOnlyInputToDefault() throws Exception {
for (String source : Arrays.asList(".", ".,ASC")) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("sort", source);
assertThat(resolveSort(request, PARAMETER), is(nullValue()));
}
}
private static Sort resolveSort(HttpServletRequest request, MethodParameter parameter) throws Exception {
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();