Skip to content

Commit

Permalink
Consider enclosing class for dynamic projection parameter detection.
Browse files Browse the repository at this point in the history
We now consider the enclosing class to determine correct generic typing.

Closes #3020
  • Loading branch information
mp911de committed Jan 11, 2024
1 parent 6623bd8 commit 0093c90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private static boolean isDynamicProjectionParameter(MethodParameter parameter, T
throw new IllegalArgumentException("Parameter is not associated with any method");
}

var returnType = TypeInformation.fromReturnTypeOf(method);
var returnType = TypeInformation.fromReturnTypeOf(method, parameter.getContainingClass());
var unwrapped = QueryExecutionConverters.unwrapWrapperTypes(returnType);
var reactiveUnwrapped = ReactiveWrapperConverters.unwrapWrapperTypes(unwrapped);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static TypeInformation<?> detectDomainType(MethodParameter parameter) {
throw new IllegalArgumentException("Method parameter is not backed by a method");
}

return detectDomainType(TypeInformation.fromReturnTypeOf(method));
return detectDomainType(TypeInformation.fromReturnTypeOf(method, parameter.getContainingClass()));
}

private static TypeInformation<?> detectDomainType(TypeInformation<?> source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.util.function.Function;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import org.springframework.core.MethodParameter;
import org.springframework.data.repository.query.ParametersUnitTests.User;
import org.springframework.data.util.TypeInformation;
Expand Down Expand Up @@ -85,7 +85,6 @@ void doesNotConsiderAtParamAnnotatedClassParameterDynamicProjectionOne() throws
assertThat(parameter.isDynamicProjectionParameter()).isFalse();
}

@NotNull
private MethodParameter getMethodParameter(String methodName) throws NoSuchMethodException {
return new MethodParameter(this.getClass().getDeclaredMethod(methodName, Class.class), 0);
}
Expand Down Expand Up @@ -117,4 +116,5 @@ User staticReturnNonDynamicBindWildcardExtends(Class<? extends User> one) {
<T> T atParamOnClass(@Param("type") Class<T> type) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ void detectsDynamicProjectionParameter() throws Exception {
assertThat(parameters.getParameter(2).isDynamicProjectionParameter()).isFalse();
}

@Test // GH-3020
void detectsDynamicParametrizedProjectionParameter() throws Exception {

var method = ParametrizedRepository.class.getMethod("dynamicBind", Class.class);
var parameters = new DefaultParameters(
ParametersSource.of(new DefaultRepositoryMetadata(ParametrizedRepository.class), method));

assertThat(parameters.getParameter(0).isDynamicProjectionParameter()).isTrue();
}

@Test // DATACMNS-863
void unwrapsOptionals() throws Exception {

Expand Down Expand Up @@ -272,4 +282,12 @@ interface Intermediate<T, ID> extends Repository<T, ID> {

interface TypedInterface extends Intermediate<User, Long> {}

interface GenericRepository<T, ID> extends Repository<T, ID> {
<P extends Projection<T>> Optional<P> dynamicBind(Class<P> type);
}

interface ParametrizedRepository extends GenericRepository<User, Long> {}

interface Projection<T> {}

}

0 comments on commit 0093c90

Please sign in to comment.