DATAREST-384 - Fixed affordances and execution of sortable search resources.
Search resources are now considered sortable if they contain a Sort parameter. This is now reflected in MethodResourceMapping.isSortableResource(). Building on top of that, the RepositorySearchController now appends the sort template variable to links generated when listing search resources. It also now accepts resolved Sort instances to forward them to the query method execution. The controller now also uses DefaultedPageable so that request missing pagination information use the defaults configured for the PageableHandlerMethodArgumentResolver.
This commit is contained in:
@@ -37,4 +37,11 @@ public interface MethodResourceMapping extends ResourceMapping {
|
||||
* @return
|
||||
*/
|
||||
ParametersMetadata getParametersMetadata();
|
||||
|
||||
/**
|
||||
* Returns whether the resource is sortable.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean isSortableResource();
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.rest.core.Path;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
@@ -46,6 +47,7 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
|
||||
private final Path path;
|
||||
private final Method method;
|
||||
private final boolean paging;
|
||||
private final boolean sorting;
|
||||
|
||||
private final List<ParameterMetadata> parameterMetadata;
|
||||
|
||||
@@ -69,7 +71,11 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
|
||||
annotation.path());
|
||||
this.method = method;
|
||||
this.parameterMetadata = discoverParameterMetadata(method, resourceRel.concat(".").concat(rel));
|
||||
this.paging = Arrays.asList(method.getParameterTypes()).contains(Pageable.class);
|
||||
|
||||
List<Class<?>> parameterTypes = Arrays.asList(method.getParameterTypes());
|
||||
|
||||
this.paging = parameterTypes.contains(Pageable.class);
|
||||
this.sorting = parameterTypes.contains(Sort.class);
|
||||
}
|
||||
|
||||
private static final List<ParameterMetadata> discoverParameterMetadata(Method method, String baseRel) {
|
||||
@@ -139,6 +145,15 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
|
||||
return paging;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.rest.core.mapping.MethodResourceMapping#isSortableResource()
|
||||
*/
|
||||
@Override
|
||||
public boolean isSortableResource() {
|
||||
return sorting;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getDescription()
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.lang.reflect.Method;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
@@ -104,6 +105,23 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
assertThat(mapping.getRel(), is("findByEmailAddress"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-384
|
||||
*/
|
||||
@Test
|
||||
public void considersResourceSortableIfSortParameterIsPresent() throws Exception {
|
||||
|
||||
Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Sort.class);
|
||||
RepositoryMethodResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
|
||||
|
||||
assertThat(mapping.isSortableResource(), is(true));
|
||||
|
||||
method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class);
|
||||
mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
|
||||
|
||||
assertThat(mapping.isSortableResource(), is(false));
|
||||
}
|
||||
|
||||
static class Person {}
|
||||
|
||||
interface PersonRepository extends Repository<Person, Long> {
|
||||
@@ -118,5 +136,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
|
||||
@RestResource(path = "fooPaged")
|
||||
Page<Person> findByEmailAddress(String email, Pageable pageable);
|
||||
|
||||
Page<Person> findByEmailAddress(String email, Sort pageable);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user