Browse Source

Add AOT parameter name detection for search queries.

See: #3265
pull/3321/head
Christoph Strobl 6 months ago
parent
commit
26709fd075
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 44
      src/main/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContext.java
  2. 21
      src/test/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContextUnitTests.java

44
src/main/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContext.java

@ -21,7 +21,6 @@ import java.util.ArrayList; @@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotationSelectors;
@ -50,8 +49,8 @@ public class AotQueryMethodGenerationContext { @@ -50,8 +49,8 @@ public class AotQueryMethodGenerationContext {
private final MethodMetadata targetMethodMetadata;
private final VariableNameFactory variableNameFactory;
protected AotQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method, QueryMethod queryMethod,
AotRepositoryFragmentMetadata targetTypeMetadata) {
protected AotQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method,
QueryMethod queryMethod, AotRepositoryFragmentMetadata targetTypeMetadata) {
this.method = method;
this.annotations = MergedAnnotations.from(method);
@ -270,8 +269,7 @@ public class AotQueryMethodGenerationContext { @@ -270,8 +269,7 @@ public class AotQueryMethodGenerationContext {
* @return the parameter name for the {@link org.springframework.data.domain.Sort sort parameter} or {@code null} if
* the method does not declare a sort parameter.
*/
@Nullable
public String getSortParameterName() {
public @Nullable String getSortParameterName() {
return getParameterName(queryMethod.getParameters().getSortIndex());
}
@ -279,8 +277,7 @@ public class AotQueryMethodGenerationContext { @@ -279,8 +277,7 @@ public class AotQueryMethodGenerationContext {
* @return the parameter name for the {@link org.springframework.data.domain.Pageable pageable parameter} or
* {@code null} if the method does not declare a pageable parameter.
*/
@Nullable
public String getPageableParameterName() {
public @Nullable String getPageableParameterName() {
return getParameterName(queryMethod.getParameters().getPageableIndex());
}
@ -288,8 +285,7 @@ public class AotQueryMethodGenerationContext { @@ -288,8 +285,7 @@ public class AotQueryMethodGenerationContext {
* @return the parameter name for the {@link org.springframework.data.domain.Limit limit parameter} or {@code null} if
* the method does not declare a limit parameter.
*/
@Nullable
public String getLimitParameterName() {
public @Nullable String getLimitParameterName() {
return getParameterName(queryMethod.getParameters().getLimitIndex());
}
@ -297,8 +293,7 @@ public class AotQueryMethodGenerationContext { @@ -297,8 +293,7 @@ public class AotQueryMethodGenerationContext {
* @return the parameter name for the {@link org.springframework.data.domain.ScrollPosition scroll position parameter}
* or {@code null} if the method does not declare a scroll position parameter.
*/
@Nullable
public String getScrollPositionParameterName() {
public @Nullable String getScrollPositionParameterName() {
return getParameterName(queryMethod.getParameters().getScrollPositionIndex());
}
@ -306,9 +301,32 @@ public class AotQueryMethodGenerationContext { @@ -306,9 +301,32 @@ public class AotQueryMethodGenerationContext {
* @return the parameter name for the {@link Class dynamic projection parameter} or {@code null} if the method does
* not declare a dynamic projection parameter.
*/
@Nullable
public String getDynamicProjectionParameterName() {
public @Nullable String getDynamicProjectionParameterName() {
return getParameterName(queryMethod.getParameters().getDynamicProjectionIndex());
}
/**
* @return the parameter name for the {@link org.springframework.data.domain.Vector vector parameter} or {@code null}
* if the method does not declare a vector type parameter.
*/
public @Nullable String getVectorParameterName() {
return getParameterName(queryMethod.getParameters().getVectorIndex());
}
/**
* @return the parameter name for the {@link org.springframework.data.domain.Score score parameter} or {@code null} if
* the method does not declare a score type parameter.
*/
public @Nullable String getScoreParameterName() {
return getParameterName(queryMethod.getParameters().getScoreIndex());
}
/**
* @return the parameter name for the {@link org.springframework.data.domain.Range score range parameter} or
* {@code null} if the method does not declare a score range type parameter.
*/
public @Nullable String getScoreRangeParameterName() {
return getParameterName(queryMethod.getParameters().getScoreRangeIndex());
}
}

21
src/test/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContextUnitTests.java

@ -25,7 +25,11 @@ import org.mockito.Mockito; @@ -25,7 +25,11 @@ import org.mockito.Mockito;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Score;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.SearchResults;
import org.springframework.data.domain.Vector;
import org.springframework.data.domain.Window;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.Repository;
@ -74,6 +78,19 @@ class AotQueryMethodGenerationContextUnitTests { @@ -74,6 +78,19 @@ class AotQueryMethodGenerationContextUnitTests {
assertThat(ctx.getDynamicProjectionParameterName()).isNull();
}
@Test // GH-3265
void returnsCorrectParameterNamesForVectorSearch() throws NoSuchMethodException {
AotQueryMethodGenerationContext ctx = ctxFor("searchAllNearWithScore");
assertThat(ctx.getVectorParameterName()).isEqualTo("vEctOR");
assertThat(ctx.getScoreParameterName()).isEqualTo("distance");
ctx = ctxFor("searchAllNearInRange");
assertThat(ctx.getScoreRangeParameterName()).isEqualTo("rDistance");
}
AotQueryMethodGenerationContext ctxFor(String methodName) throws NoSuchMethodException {
Method target = null;
@ -105,5 +122,9 @@ class AotQueryMethodGenerationContextUnitTests { @@ -105,5 +122,9 @@ class AotQueryMethodGenerationContextUnitTests {
<T> Window<T> limitScrollPositionDynamicProjection(Limit l, ScrollPosition sp, Class<T> projection);
Page<String> pageable(Pageable p);
SearchResults<String> searchAllNearWithScore(Vector vEctOR, Score distance, Limit limit);
SearchResults<String> searchAllNearInRange(Vector vEctOR, Range<Score> rDistance, Limit limit);
}
}

Loading…
Cancel
Save