From 3a6b9ea3ff3e751d4e7a8ccfc317f578ec32d4ec Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 6 Nov 2015 16:31:43 +0100 Subject: [PATCH] DATAREST-699 - Fixed pageable parameters showing up as is in search resource links on Java 8. We now explicitly drop parameters of type Pageable and Sort from being exposed as search resource parameters as they're added via the UriComponentsContributors anyway. This prevents "pageable" from showing up as parameter on Java 8 code compiled with -parameters. The latter adds naming information for the Pageable parameter and caused it showing up as is despite the fact that the UriComponentsContributor for Pageables exposes a dedicated syntax. --- .../mapping/RepositoryMethodResourceMapping.java | 12 ++++++++---- .../RepositoryMethodResourceMappingUnitTests.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMapping.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMapping.java index 1986007b7..dd931149c 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMapping.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.data.rest.core.mapping; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; @@ -41,6 +42,8 @@ import org.springframework.util.StringUtils; */ class RepositoryMethodResourceMapping implements MethodResourceMapping { + @SuppressWarnings("unchecked") // + private static final Collection> IMPLICIT_PARAMETER_TYPES = Arrays.asList(Pageable.class, Sort.class); private static final AnnotationAttribute PARAM_VALUE = new AnnotationAttribute(Param.class); private final boolean isExported; @@ -69,8 +72,8 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping { this.isExported = annotation != null ? annotation.exported() : true; this.rel = annotation == null || !StringUtils.hasText(annotation.rel()) ? method.getName() : annotation.rel(); - this.path = annotation == null || !StringUtils.hasText(annotation.path()) ? new Path(method.getName()) : new Path( - annotation.path()); + this.path = annotation == null || !StringUtils.hasText(annotation.path()) ? new Path(method.getName()) + : new Path(annotation.path()); this.method = method; this.parameterMetadata = discoverParameterMetadata(method, resourceRel.concat(".").concat(rel)); @@ -86,7 +89,8 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping { List result = new ArrayList(); for (MethodParameter parameter : new MethodParameters(method, PARAM_VALUE).getParameters()) { - if (StringUtils.hasText(parameter.getParameterName())) { + if (!IMPLICIT_PARAMETER_TYPES.contains(parameter.getParameterType()) + && StringUtils.hasText(parameter.getParameterName())) { result.add(new ParameterMetadata(parameter, baseRel)); } } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java index e3f766004..02de91e20 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java @@ -135,6 +135,18 @@ public class RepositoryMethodResourceMappingUnitTests { assertThat(mapping.getReturnedDomainType(), is(equalTo((Class) Person.class))); } + /** + * @see DATAREST-699 + */ + @Test + public void doesNotIncludePageableAsParameter() throws Exception { + + Method method = PersonRepository.class.getMethod("findByLastname", String.class, Pageable.class); + RepositoryMethodResourceMapping mapping = getMappingFor(method); + + assertThat(mapping.getParametersMetadata().getParameterNames(), not(hasItem("pageable"))); + } + private RepositoryMethodResourceMapping getMappingFor(Method method) { return new RepositoryMethodResourceMapping(method, resourceMapping, metadata); } @@ -157,5 +169,8 @@ public class RepositoryMethodResourceMappingUnitTests { Page findByEmailAddress(String email, Sort pageable); int countByLastname(String lastname); + + // Simulate pageable detected as name on Java 8 + Page findByLastname(@Param("lastname") String lastname, @Param("pageable") Pageable pageable); } }