Skip to content

Commit a0cff5c

Browse files
committed
DATACMNS-753 - Fixed default handling of empty sort parameter.
We now correctly inspect the sort parameter values and also fall back to the default if a single, empty parameter value is given. Related ticket: DATACMNS-408. Related pull request: #138.
1 parent f8962f8 commit a0cff5c

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolver.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,11 +108,17 @@ public Sort resolveArgument(MethodParameter parameter, ModelAndViewContainer mav
108108

109109
String[] directionParameter = webRequest.getParameterValues(getSortParameter(parameter));
110110

111-
if (directionParameter != null && directionParameter.length != 0) {
112-
return parseParameterIntoSort(directionParameter, propertyDelimiter);
113-
} else {
111+
// No parameter
112+
if (directionParameter == null) {
114113
return getDefaultFromAnnotationOrFallback(parameter);
115114
}
115+
116+
// Single empty parameter, e.g "sort="
117+
if (directionParameter.length == 1 && !StringUtils.hasText(directionParameter[0])) {
118+
return getDefaultFromAnnotationOrFallback(parameter);
119+
}
120+
121+
return parseParameterIntoSort(directionParameter, propertyDelimiter);
116122
}
117123

118124
/**
@@ -130,9 +136,9 @@ private Sort getDefaultFromAnnotationOrFallback(MethodParameter parameter) {
130136
SortDefault annotatedDefault = parameter.getParameterAnnotation(SortDefault.class);
131137

132138
if (annotatedDefault != null && annotatedDefaults != null) {
133-
throw new IllegalArgumentException(String.format(
134-
"Cannot use both @%s and @%s on parameter %s! Move %s into %s to define sorting order!", SORT_DEFAULTS_NAME,
135-
SORT_DEFAULT_NAME, parameter.toString(), SORT_DEFAULT_NAME, SORT_DEFAULTS_NAME));
139+
throw new IllegalArgumentException(
140+
String.format("Cannot use both @%s and @%s on parameter %s! Move %s into %s to define sorting order!",
141+
SORT_DEFAULTS_NAME, SORT_DEFAULT_NAME, parameter.toString(), SORT_DEFAULT_NAME, SORT_DEFAULTS_NAME));
136142
}
137143

138144
if (annotatedDefault != null) {

src/test/java/org/springframework/data/web/SortHandlerMethodArgumentResolverUnitTests.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,21 +149,6 @@ public void parsesEmptySortToNull() throws Exception {
149149
assertThat(resolveSort(request, PARAMETER), is(nullValue()));
150150
}
151151

152-
/**
153-
*
154-
* @see DATACMNS-753
155-
* see also DATACMNS-408
156-
*/
157-
@Test
158-
public void doesNotReturnNullWhenAnnotatedWithSortDefault() throws Exception {
159-
160-
MockHttpServletRequest request = new MockHttpServletRequest();
161-
request.addParameter("sort", ""); //valid input
162-
163-
assertNotNull(resolveSort(request, getParameterOfMethod("simpleDefault")));
164-
assertNotNull(resolveSort(request, getParameterOfMethod("containeredDefault")));
165-
}
166-
167152
/**
168153
* @see DATACMNS-408
169154
*/
@@ -213,6 +198,19 @@ public void parsesCommaParameterForSort() throws Exception {
213198
assertThat(resolveSort(request, PARAMETER), is(nullValue()));
214199
}
215200

201+
/**
202+
* @see DATACMNS-753, DATACMNS-408
203+
*/
204+
@Test
205+
public void doesNotReturnNullWhenAnnotatedWithSortDefault() throws Exception {
206+
207+
MockHttpServletRequest request = new MockHttpServletRequest();
208+
request.addParameter("sort", "");
209+
210+
assertThat(resolveSort(request, getParameterOfMethod("simpleDefault")), is(new Sort("firstname", "lastname")));
211+
assertThat(resolveSort(request, getParameterOfMethod("containeredDefault")), is(new Sort("foo", "bar")));
212+
}
213+
216214
private static Sort resolveSort(HttpServletRequest request, MethodParameter parameter) throws Exception {
217215

218216
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();

0 commit comments

Comments
 (0)