Skip to content

DATACMNS-1005 - Support for Javaslang types in paginating query methods #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import static org.springframework.data.repository.util.ClassUtils.*;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Arrays;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -30,6 +31,7 @@
import org.springframework.data.repository.util.QueryExecutionConverters;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -78,7 +80,8 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory
if (hasParameterOfType(method, Pageable.class)) {

if (!isStreamQuery()) {
assertReturnTypeAssignable(method, Slice.class, Page.class, List.class);
final Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
assertReturnTypeAssignable(method, allowedPageableTypes.toArray(new Class<?>[allowedPageableTypes.size()]));
}

if (hasParameterOfType(method, Sort.class)) {
Expand Down Expand Up @@ -275,4 +278,21 @@ private static Class<? extends Object> potentiallyUnwrapReturnTypeFor(Method met

return method.getReturnType();
}
}

private static void assertReturnTypeAssignable(Method method, Class<?>... types) {

Assert.notNull(method, "Method must not be null!");
Assert.notEmpty(types, "Types must not be null or empty!");

TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method);
returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) ? returnType.getComponentType() : returnType;

for (Class<?> type : types) {
if (type.isAssignableFrom(returnType.getType())) {
return;
}
}

throw new IllegalStateException("Method has to have one of the following return types! " + Arrays.toString(types));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.springframework.data.repository.util;

import javaslang.collection.Seq;
import javaslang.collection.Traversable;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import scala.Function0;
import scala.Option;
import scala.runtime.AbstractFunction0;
Expand All @@ -27,6 +30,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -75,12 +79,17 @@ public abstract class QueryExecutionConverters {

private static final Set<WrapperType> WRAPPER_TYPES = new HashSet<WrapperType>();
private static final Set<Converter<Object, Object>> UNWRAPPERS = new HashSet<Converter<Object, Object>>();
private static final Set<Class<?>> ALLOWED_PAGEABLE_TYPES = new HashSet<Class<?>>();

static {

WRAPPER_TYPES.add(WrapperType.singleValue(Future.class));
WRAPPER_TYPES.add(WrapperType.singleValue(ListenableFuture.class));

ALLOWED_PAGEABLE_TYPES.add(Slice.class);
ALLOWED_PAGEABLE_TYPES.add(Page.class);
ALLOWED_PAGEABLE_TYPES.add(List.class);

if (GUAVA_PRESENT) {
WRAPPER_TYPES.add(NullableWrapperToGuavaOptionalConverter.getWrapperType());
UNWRAPPERS.add(GuavaOptionalUnwrapper.INSTANCE);
Expand All @@ -106,6 +115,8 @@ public abstract class QueryExecutionConverters {
WRAPPER_TYPES.add(JavaslangCollections.ToJavaConverter.INSTANCE.getWrapperType());

UNWRAPPERS.add(JavaslangOptionUnwrapper.INSTANCE);

ALLOWED_PAGEABLE_TYPES.add(Seq.class);
}
}

Expand Down Expand Up @@ -141,6 +152,10 @@ public static boolean isSingleValue(Class<?> type) {
return false;
}

public static Set<Class<?>> getAllowedPageableTypes() {
return Collections.unmodifiableSet(ALLOWED_PAGEABLE_TYPES);
}

/**
* Registers converters for wrapper types found on the classpath.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ public void detectsSinglValueWrapperWithinWrapper() throws Exception {
assertThat(new QueryMethod(method, repositoryMetadata, factory).isCollectionQuery(), is(false));
}

@Test // DATACMNS-1005
public void doesNotRejectSeqForPagination() throws Exception {

RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class);
Method method = SampleRepository.class.getMethod("returnsSeq", Pageable.class);

assertThat(new QueryMethod(method, repositoryMetadata, factory).isCollectionQuery(), is(true));
}

interface SampleRepository extends Repository<User, Serializable> {

String pagingMethodWithInvalidReturnType(Pageable pageable);
Expand Down Expand Up @@ -249,6 +258,9 @@ interface SampleRepository extends Repository<User, Serializable> {

Seq<User> returnsSeq();

// DATACMNS-1005
Seq<User> returnsSeq(Pageable pageable);

Future<Seq<User>> returnsFutureOfSeq();

Future<Option<User>> returnsFutureOfOption();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import javaslang.collection.HashMap;
import javaslang.collection.HashSet;
import javaslang.collection.Seq;
import javaslang.collection.Traversable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import scala.Option;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -219,6 +222,16 @@ public void unwrapsJavaslangCollectionsToJavaOnes() {
assertThat(unwrap(javaslangMap("key", "value")), is(instanceOf(Map.class)));
}

@Test // DATACMNS-1005
public void registersAllowedPageabletypes() {

final Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
assertThat(allowedPageableTypes, hasItem(Page.class));
assertThat(allowedPageableTypes, hasItem(Slice.class));
assertThat(allowedPageableTypes, hasItem(List.class));
assertThat(allowedPageableTypes, hasItem(Seq.class));
}

@SuppressWarnings("unchecked")
private static javaslang.control.Option<Object> optionNone() {

Expand Down