Skip to content

Commit

Permalink
Adapt SortHandlerMethodArgumentResolver after changes to sort parameter.
Browse files Browse the repository at this point in the history
Since the introduction of 6e22ffd, the sort parameter has been rendered as composite parameter which will cause the comma used to delimit property from direction potentially be encoded by clients following the URI template RFC. Thus, we need to defensively decode the source parameter value before parsing.

Related ticket: GH-2531.
  • Loading branch information
odrotbohm committed Dec 12, 2023
1 parent ee64329 commit afc2ea8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.web;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.springframework.core.MethodParameter;
Expand All @@ -23,8 +24,8 @@
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.util.UriUtils;

/**
* {@link HandlerMethodArgumentResolver} to automatically create {@link Sort} instances from request parameters or
Expand Down Expand Up @@ -61,6 +62,10 @@ public Sort resolveArgument(MethodParameter parameter, @Nullable ModelAndViewCon
return getDefaultFromAnnotationOrFallback(parameter);
}

return parseParameterIntoSort(Arrays.asList(directionParameter), getPropertyDelimiter());
var decoded = Arrays.stream(directionParameter)
.map(it -> UriUtils.decode(it, StandardCharsets.UTF_8))
.toList();

return parseParameterIntoSort(decoded, getPropertyDelimiter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import jakarta.servlet.http.HttpServletRequest;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -35,6 +36,7 @@
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.util.UriUtils;

/**
* Unit tests for {@link SortHandlerMethodArgumentResolver}.
Expand Down Expand Up @@ -259,6 +261,18 @@ void mergedQualifierIsUsedInParameterLookup() {
assertSupportedAndResolvedTo(getRequestWithSort(reference, "merged"), parameter, reference);
}

@Test
void readsEncodedSort() {

var request = new MockHttpServletRequest();
request.addParameter("sort", UriUtils.encode("foo,desc", StandardCharsets.UTF_8));

var parameter = getParameterOfMethod("supportedMethod");
var encoded = UriUtils.encode("foo,desc", StandardCharsets.UTF_8);

assertSupportedAndResolvedTo(new ServletWebRequest(request), parameter, Sort.by("foo").descending());
}

private static Sort resolveSort(HttpServletRequest request, MethodParameter parameter) throws Exception {

var resolver = new SortHandlerMethodArgumentResolver();
Expand Down

0 comments on commit afc2ea8

Please sign in to comment.