Skip to content

Commit

Permalink
Adopt CassandraParameters and CassandraParameter to reflect the a…
Browse files Browse the repository at this point in the history
…ctual parameter type when using generics.

Closes #1456
  • Loading branch information
mp911de committed Dec 8, 2023
1 parent 8338715 commit f9f1d2e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
Expand Up @@ -29,9 +29,11 @@
import org.springframework.data.cassandra.repository.query.CassandraParameters.CassandraParameter;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersSource;
import org.springframework.data.repository.util.QueryExecutionConverters;
import org.springframework.data.repository.util.ReactiveWrapperConverters;
import org.springframework.data.repository.util.ReactiveWrappers;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;

/**
Expand All @@ -45,15 +47,16 @@ public class CassandraParameters extends Parameters<CassandraParameters, Cassand
private final @Nullable Integer queryOptionsIndex;

/**
* Create a new {@link CassandraParameters} instance from the given {@link Method}
* Create a new {@link CassandraParameters} instance from the given {@link Method}.
*
* @param method must not be {@literal null}.
* @param parametersSource must not be {@literal null}.
*/
public CassandraParameters(Method method) {
public CassandraParameters(ParametersSource parametersSource) {
super(parametersSource,
methodParameter -> new CassandraParameter(methodParameter, parametersSource.getDomainTypeInformation()));

super(method);

this.queryOptionsIndex = Arrays.asList(method.getParameterTypes()).indexOf(QueryOptions.class);
this.queryOptionsIndex = Arrays.asList(parametersSource.getMethod().getParameterTypes())
.indexOf(QueryOptions.class);
}

private CassandraParameters(List<CassandraParameter> originals, @Nullable Integer queryOptionsIndex) {
Expand All @@ -63,11 +66,6 @@ private CassandraParameters(List<CassandraParameter> originals, @Nullable Intege
this.queryOptionsIndex = queryOptionsIndex;
}

@Override
protected CassandraParameter createParameter(MethodParameter parameter) {
return new CassandraParameter(parameter);
}

@Override
protected CassandraParameters createFrom(List<CassandraParameter> parameters) {
return new CassandraParameters(parameters, queryOptionsIndex);
Expand All @@ -93,9 +91,9 @@ static class CassandraParameter extends Parameter {
private final @Nullable CassandraType cassandraType;
private final Class<?> parameterType;

CassandraParameter(MethodParameter parameter) {
CassandraParameter(MethodParameter parameter, TypeInformation<?> domainType) {

super(parameter);
super(parameter, domainType);

AnnotatedParameter annotatedParameter = new AnnotatedParameter(parameter);

Expand Down
Expand Up @@ -29,6 +29,8 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersSource;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -128,8 +130,8 @@ public CassandraParameters getParameters() {
}

@Override
protected CassandraParameters createParameters(Method method) {
return new CassandraParameters(method);
protected Parameters<?, ?> createParameters(ParametersSource parametersSource) {
return new CassandraParameters(parametersSource);
}

/**
Expand Down
Expand Up @@ -80,7 +80,8 @@ class QueryDerivationIntegrationTests extends AbstractSpringDataEmbeddedCassandr

@Configuration
@EnableCassandraRepositories(considerNestedRepositories = true,
includeFilters = @Filter(pattern = ".*PersonRepository", type = FilterType.REGEX))
includeFilters = @Filter(classes = { PersonRepository.class, EmbeddedPersonRepository.class },
type = FilterType.ASSIGNABLE_TYPE))
public static class Config extends IntegrationTestConfig {

@Override
Expand Down
Expand Up @@ -28,6 +28,9 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.domain.Person;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.query.ParametersSource;

/**
* Unit tests for {@link CassandraParameters}.
Expand All @@ -43,7 +46,8 @@ class CassandraParametersUnitTests {
void shouldReturnUnknownDataTypeForSimpleType() throws Exception {

Method method = PersonRepository.class.getMethod("findByFirstname", String.class);
CassandraParameters cassandraParameters = new CassandraParameters(method);
CassandraParameters cassandraParameters = new CassandraParameters(
ParametersSource.of(method));

assertThat(cassandraParameters.getParameter(0).getCassandraType()).isNull();
}
Expand All @@ -52,17 +56,17 @@ void shouldReturnUnknownDataTypeForSimpleType() throws Exception {
void shouldReturnDataTypeForAnnotatedSimpleType() throws Exception {

Method method = PersonRepository.class.getMethod("findByFirstTime", String.class);
CassandraParameters cassandraParameters = new CassandraParameters(method);
CassandraParameters cassandraParameters = new CassandraParameters(
ParametersSource.of(method));

assertThat(cassandraParameters.getParameter(0).getCassandraType().type())
.isEqualTo(Name.TIME);
assertThat(cassandraParameters.getParameter(0).getCassandraType().type()).isEqualTo(Name.TIME);
}

@Test // DATACASS-296
void shouldReturnNoTypeForComplexType() throws Exception {

Method method = PersonRepository.class.getMethod("findByObject", Object.class);
CassandraParameters cassandraParameters = new CassandraParameters(method);
CassandraParameters cassandraParameters = new CassandraParameters(ParametersSource.of(method));

assertThat(cassandraParameters.getParameter(0).getCassandraType()).isNull();
}
Expand All @@ -71,23 +75,21 @@ void shouldReturnNoTypeForComplexType() throws Exception {
void shouldReturnTypeForAnnotatedType() throws Exception {

Method method = PersonRepository.class.getMethod("findByAnnotatedObject", Object.class);
CassandraParameters cassandraParameters = new CassandraParameters(method);
CassandraParameters cassandraParameters = new CassandraParameters(ParametersSource.of(method));

assertThat(cassandraParameters.getParameter(0).getCassandraType().type())
.isEqualTo(Name.TIME);
assertThat(cassandraParameters.getParameter(0).getCassandraType().type()).isEqualTo(Name.TIME);
}

@Test // DATACASS-296
void shouldReturnTypeForComposedAnnotationType() throws Exception {

Method method = PersonRepository.class.getMethod("findByComposedAnnotationObject", Object.class);
CassandraParameters cassandraParameters = new CassandraParameters(method);
CassandraParameters cassandraParameters = new CassandraParameters(ParametersSource.of(method));

assertThat(cassandraParameters.getParameter(0).getCassandraType().type())
.isEqualTo(Name.BOOLEAN);
assertThat(cassandraParameters.getParameter(0).getCassandraType().type()).isEqualTo(Name.BOOLEAN);
}

interface PersonRepository {
interface PersonRepository extends Repository<Person, String> {

Person findByFirstname(String firstname);

Expand Down

0 comments on commit f9f1d2e

Please sign in to comment.