Fix query execution mode detection for aggregate types that implement Streamable.

We now short-circuit the QueryMethod.isCollectionQuery() algorithm in case we find the concrete domain type or any subclass of it.

Fixes #2869.
This commit is contained in:
Oliver Drotbohm
2023-07-01 00:06:15 +02:00
parent 05dd7aecfb
commit ca9f9bfdc8
5 changed files with 119 additions and 8 deletions

View File

@@ -96,12 +96,13 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
return returnType;
}
@Override
public Class<?> getReturnedDomainClass(Method method) {
TypeInformation<?> returnType = getReturnType(method);
returnType = ReactiveWrapperConverters.unwrapWrapperTypes(returnType);
return QueryExecutionConverters.unwrapWrapperTypes(ReactiveWrapperConverters.unwrapWrapperTypes(returnType))
.getType();
return QueryExecutionConverters.unwrapWrapperTypes(returnType, getDomainTypeInformation()).getType();
}
public Class<?> getRepositoryInterface() {

View File

@@ -24,16 +24,17 @@ import java.util.stream.Stream;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.EntityMetadata;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.util.QueryExecutionConverters;
import org.springframework.data.repository.util.ReactiveWrapperConverters;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.NullableWrapperConverters;
import org.springframework.data.util.ReactiveWrappers;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
@@ -296,7 +297,15 @@ public class QueryMethod {
return false;
}
Class<?> returnType = metadata.getReturnType(method).getType();
TypeInformation<?> returnTypeInformation = metadata.getReturnType(method);
// Check against simple wrapper types first
if (metadata.getDomainTypeInformation()
.isAssignableFrom(NullableWrapperConverters.unwrapActualType(returnTypeInformation))) {
return false;
}
Class<?> returnType = returnTypeInformation.getType();
if (QueryExecutionConverters.supports(returnType) && !QueryExecutionConverters.isSingleValue(returnType)) {
return true;

View File

@@ -36,8 +36,8 @@ import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Window;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.util.CustomCollections;
import org.springframework.data.util.NullableWrapper;
@@ -85,6 +85,7 @@ public abstract class QueryExecutionConverters {
private static final Set<Class<?>> ALLOWED_PAGEABLE_TYPES = new HashSet<>();
private static final Map<Class<?>, ExecutionAdapter> EXECUTION_ADAPTER = new HashMap<>();
private static final Map<Class<?>, Boolean> supportsCache = new ConcurrentReferenceHashMap<>();
private static final TypeInformation<Void> VOID_INFORMATION = TypeInformation.of(Void.class);
static {
@@ -235,15 +236,21 @@ public abstract class QueryExecutionConverters {
}
/**
* Recursively unwraps well known wrapper types from the given {@link TypeInformation}.
* Recursively unwraps well known wrapper types from the given {@link TypeInformation} but aborts at the given
* reference type.
*
* @param type must not be {@literal null}.
* @param reference must not be {@literal null}.
* @return will never be {@literal null}.
*/
public static TypeInformation<?> unwrapWrapperTypes(TypeInformation<?> type) {
public static TypeInformation<?> unwrapWrapperTypes(TypeInformation<?> type, TypeInformation<?> reference) {
Assert.notNull(type, "type must not be null");
if (reference.isAssignableFrom(type)) {
return type;
}
Class<?> rawType = type.getType();
boolean needToUnwrap = type.isCollectionLike() //
@@ -253,7 +260,17 @@ public abstract class QueryExecutionConverters {
|| supports(rawType) //
|| Stream.class.isAssignableFrom(rawType);
return needToUnwrap ? unwrapWrapperTypes(type.getRequiredComponentType()) : type;
return needToUnwrap ? unwrapWrapperTypes(type.getRequiredComponentType(), reference) : type;
}
/**
* Recursively unwraps well known wrapper types from the given {@link TypeInformation}.
*
* @param type must not be {@literal null}.
* @return will never be {@literal null}.
*/
public static TypeInformation<?> unwrapWrapperTypes(TypeInformation<?> type) {
return unwrapWrapperTypes(type, VOID_INFORMATION);
}
/**