From b43cbb42d8dd1cf053fd628b67de245065a9d4ef Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 29 Apr 2014 12:29:48 +0200 Subject: [PATCH] DATAREST-298 - Fixed rel lookup in RepositoryMethodResourceMapping. In case @RestResource was used to customize the path a method resource was mapped to, we didn't correctly fall back to the method name as rel in case no rel was configured explicitly. We now check for a rel being configured and fall back to the method name if we don't discover manual configuration. --- .../core/mapping/RepositoryMethodResourceMapping.java | 2 +- .../RepositoryMethodResourceMappingUnitTests.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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 b75efb524..eb875f79b 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 @@ -63,7 +63,7 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping { RestResource annotation = AnnotationUtils.findAnnotation(method, RestResource.class); this.isExported = annotation != null ? annotation.exported() : true; - this.rel = annotation != null ? annotation.rel() : method.getName(); + 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.method = method; 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 3593cc67d..9837fdad1 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 @@ -95,6 +95,15 @@ public class RepositoryMethodResourceMappingUnitTests { assertThat(mapping.isPagingResource(), is(true)); } + @Test + public void usesMethodNameAsRelFallbackEvenIfPathIsConfigured() throws Exception { + + Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class); + MethodResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping); + + assertThat(mapping.getRel(), is("findByEmailAddress")); + } + static class Person {} interface PersonRepository extends Repository {