From 20d18bd8f2cbb8240f8aa657a0c223b0b03b3bfa Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Wed, 13 Mar 2013 15:06:14 -0500 Subject: [PATCH] Fix PagingAndSorting resolver to recognize property aliases from the configuration. --- ...agingAndSortingMethodArgumentResolver.java | 43 ++++++++++++++++--- .../RepositoryRestMvcConfiguration.java | 18 ++++---- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java index f90c94bdc..2e0b2234c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java @@ -42,6 +42,19 @@ public class PagingAndSortingMethodArgumentResolver implements HandlerMethodArgu this.config = config; } + public RepositoryRestConfiguration getConfig() { + return config; + } + + public RepositoryInformationHandlerMethodArgumentResolver getRepoInfoResolver() { + return repoInfoResolver; + } + + public PagingAndSortingMethodArgumentResolver setRepoInfoResolver(RepositoryInformationHandlerMethodArgumentResolver repoInfoResolver) { + this.repoInfoResolver = repoInfoResolver; + return this; + } + @Override public boolean supportsParameter(MethodParameter parameter) { return ClassUtils.isAssignable(parameter.getParameterType(), PagingAndSorting.class); } @@ -51,11 +64,6 @@ public class PagingAndSortingMethodArgumentResolver implements HandlerMethodArgu ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { - RepositoryInformation repoInfo = (RepositoryInformation)repoInfoResolver.resolveArgument(parameter, - mavContainer, - webRequest, - binderFactory); - ResourceMapping repoMapping = config.getResourceMappingForDomainType(repoInfo.getDomainType()); HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest(); PageRequest pr = null; @@ -89,7 +97,11 @@ public class PagingAndSortingMethodArgumentResolver implements HandlerMethodArgu String[] orderValues = request.getParameterValues(config.getSortParamName()); if(null != orderValues) { for(String orderParam : orderValues) { - String name = repoMapping.getNameForPath(orderParam); + String name = nameForParam(parameter, + mavContainer, + webRequest, + binderFactory, + orderParam); String sortDir = request.getParameter(orderParam + ".dir"); Sort.Direction dir = (null != sortDir ? Sort.Direction.valueOf(sortDir.toUpperCase()) : Sort.Direction.ASC); orders.add(new Sort.Order(dir, name)); @@ -109,4 +121,23 @@ public class PagingAndSortingMethodArgumentResolver implements HandlerMethodArgu return new PagingAndSorting(config, pr); } + private String nameForParam(MethodParameter parameter, + ModelAndViewContainer mavContainer, + NativeWebRequest webRequest, + WebDataBinderFactory binderFactory, + String orderParam) throws Exception { + if(null == repoInfoResolver) { + return orderParam; + } + RepositoryInformation repoInfo = (RepositoryInformation)repoInfoResolver.resolveArgument(parameter, + mavContainer, + webRequest, + binderFactory); + ResourceMapping repoMapping = config.getResourceMappingForDomainType(repoInfo.getDomainType()); + if(null != repoMapping) { + return repoMapping.getNameForPath(orderParam); + } + return orderParam; + } + } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 0fec964e6..d60ace3cf 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -229,6 +229,15 @@ public class RepositoryRestMvcConfiguration { return new BaseUriMethodArgumentResolver(); } + /** + * Resolves the {@link org.springframework.data.repository.core.RepositoryInformation} for this request. + * + * @return + */ + @Bean public RepositoryInformationHandlerMethodArgumentResolver repoInfoMethodArgumentResolver() { + return new RepositoryInformationHandlerMethodArgumentResolver(); + } + /** * Resolves the paging and sorting information from the query parameters based on the current configuration settings. * @@ -247,15 +256,6 @@ public class RepositoryRestMvcConfiguration { return new ServerHttpRequestMethodArgumentResolver(); } - /** - * Resolves the {@link org.springframework.data.repository.core.RepositoryInformation} for this request. - * - * @return - */ - @Bean public RepositoryInformationHandlerMethodArgumentResolver repoInfoMethodArgumentResolver() { - return new RepositoryInformationHandlerMethodArgumentResolver(); - } - /** * A convenience resolver that pulls together all the information needed to service a request. *