DATACMNS-658 - Add IgnoreCase ordering option to SortHandlerMethodArgumentResolver.

We now pick up case-insensitive sorting flags by parsing it up from a sort query string argument.

To enable ignore case for one or more properties, we expect "ignorecase" as last element in a sort spec. Sort order and case-sensitivity options are parsed case-insensitive for a greater flexibility:

http://localhost/?sort=firstname,IgnoreCase or http://localhost/?sort=firstname,ASC,ignorecase

Original pull request: #172.
This commit is contained in:
Dan Nawrocki
2020-03-02 11:59:37 +01:00
committed by Mark Paluch
parent f2a6391273
commit 9ef3099711
2 changed files with 27 additions and 2 deletions

View File

@@ -187,8 +187,10 @@ public abstract class SortHandlerMethodArgumentResolverSupport {
/**
* Parses the given sort expressions into a {@link Sort} instance. The implementation expects the sources to be a
* concatenation of Strings using the given delimiter. If the last element can be parsed into a {@link Direction} it's
* considered a {@link Direction} and a simple property otherwise.
* concatenation of Strings using the given delimiter. If the last element is equal to "ignorecase" (when using a
* case-insensitive comparison), the sort order will be performed without respect to case. If the last element (or the
* penultimate element if the last is "ignorecase") can be parsed into a {@link Direction} it's considered a
* {@link Direction} and a simple property otherwise.
*
* @param source will never be {@literal null}.
* @param delimiter the delimiter to be used to split up the source elements, will never be {@literal null}.

View File

@@ -18,6 +18,7 @@ package org.springframework.data.web;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.Sort.Direction.*;
import java.util.Arrays;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
@@ -74,6 +75,28 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
assertThat(sort.isSorted()).isFalse();
}
@Test
public void sortParamHandlesSortOrderAndIgnoreCase() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("sort", "property,DESC,IgnoreCase");
request.addParameter("sort", "");
assertThat(resolveSort(request, PARAMETER))
.isEqualTo(Sort.by(Arrays.asList(new Order(DESC, "property").ignoreCase())));
}
@Test
public void sortParamHandlesIgnoreCase() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("sort", "property,IgnoreCase");
request.addParameter("sort", "");
assertThat(resolveSort(request, PARAMETER))
.isEqualTo(Sort.by(Arrays.asList(new Order(ASC, "property").ignoreCase())));
}
@Test
public void discoversSimpleSortFromRequest() {