From 9ef30997113b571f02816dc4e0fd87dd024c4d63 Mon Sep 17 00:00:00 2001 From: Dan Nawrocki Date: Mon, 2 Mar 2020 11:59:37 +0100 Subject: [PATCH] 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. --- ...tHandlerMethodArgumentResolverSupport.java | 6 +++-- ...andlerMethodArgumentResolverUnitTests.java | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java b/src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java index 422bb297e..3c5ecb237 100644 --- a/src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java +++ b/src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java @@ -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}. diff --git a/src/test/java/org/springframework/data/web/SortHandlerMethodArgumentResolverUnitTests.java b/src/test/java/org/springframework/data/web/SortHandlerMethodArgumentResolverUnitTests.java index dc29316ce..1bd12455a 100755 --- a/src/test/java/org/springframework/data/web/SortHandlerMethodArgumentResolverUnitTests.java +++ b/src/test/java/org/springframework/data/web/SortHandlerMethodArgumentResolverUnitTests.java @@ -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() {