Skip to content

Spring Data Couchbase with @Query and Pageable / Sort throwing ArrayIndexOutOfBoundsException - PagingAndSortingRepository, CouchbaseRepository #1155

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
Ravikumar-R opened this issue Jul 8, 2021 · 2 comments · Fixed by #1197
Assignees
Labels
type: bug A general bug

Comments

@Ravikumar-R
Copy link

I am trying to execute custom query with @query annotation which is connecting to couchbase and I am expecting paging response.

I am facing this issue in my project, I have only provided a sample application code

Controller

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

    @GetMapping("/getEmployees/{page}/{size}")
    public Page<Employee> getEmployees(@PathVariable int page, @PathVariable int size) {
        Pageable pageable = PageRequest.of(page, size);
        return employeeRepository.findEmployeeDetails(pageable);
    } 

Repository

import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface EmployeeRepository extends CouchbaseRepository<Employee, Integer> {

    Employee findByEmpName(String empName);

    Page<Employee> findByEmpName(String empName, Pageable pageable); 

    @Query("#{#n1ql.selectEntity} WHERE empName='Ravi'")
    Page<Employee> findEmployeeDetails(Pageable pageable);
}

Get Call

"http://localhost:8080/getEmployees/1/2"

I am getting

java.lang.ArrayIndexOutOfBoundsException: 0
org.springframework.data.repository.query.ExtensionAwareQueryMethodEvaluationContextProvider.lambda$collectVariables$0(ExtensionAwareQueryMethodEvaluationContextProvider.java:116)
java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)\r\n\tat java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)\r\n\tat java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:899)
.
.

And I am able to get response for this method call

Page<Employee> findByEmpName(String empName, Pageable pageable); 

So problem comes when I have @query annotated with pageable like below

 @Query("#{#n1ql.selectEntity} WHERE empName='Ravi'")
        Page<Employee> findEmployeeDetails(Pageable pageable);

This issue is occurs with Sort as well.

Class ExtensionAwareQueryMethodEvaluationContextProvider Method collectVariables

 parameters.stream().filter(Parameter::isSpecialParameter).forEach((it) -> {
            variables.put(StringUtils.uncapitalize(it.getType().getSimpleName()), arguments[it.getIndex()]);
        });

My dependency tree

[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ couchbase ---
[INFO] com.ravi:couchbase:jar:0.0.1-SNAPSHOT
[INFO] +- org.springdoc:springdoc-openapi-ui:jar:1.5.2:compile
[INFO] |  +- org.springdoc:springdoc-openapi-webmvc-core:jar:1.5.2:compile
[INFO] |  |  \- org.springdoc:springdoc-openapi-common:jar:1.5.2:compile
[INFO] |  |     +- io.swagger.core.v3:swagger-models:jar:2.1.6:compile
[INFO] |  |     +- io.swagger.core.v3:swagger-annotations:jar:2.1.6:compile
[INFO] |  |     +- io.swagger.core.v3:swagger-integration:jar:2.1.6:compile
[INFO] |  |     |  \- io.swagger.core.v3:swagger-core:jar:2.1.6:compile
[INFO] |  |     |     +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
[INFO] |  |     |     |  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
[INFO] |  |     |     +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.12.3:compile
[INFO] |  |     |     \- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
[INFO] |  |     +- io.github.classgraph:classgraph:jar:4.8.69:compile
[INFO] |  |     \- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] |  +- org.webjars:swagger-ui:jar:3.38.0:compile
[INFO] |  \- org.webjars:webjars-locator-core:jar:0.46:compile
[INFO] |     +- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] |     \- com.fasterxml.jackson.core:jackson-core:jar:2.12.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.5.0:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.5.0:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.5.0:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  |  +- org.springframework:spring-core:jar:5.3.7:compile
[INFO] |  |  |  \- org.springframework:spring-jcl:jar:5.3.7:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.28:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.5.0:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.3:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.3:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.12.3:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.12.3:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.12.3:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.5.0:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.46:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.46:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.46:compile
[INFO] |  +- org.springframework:spring-web:jar:5.3.7:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.3.7:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.3.7:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.3.7:compile
[INFO] |     +- org.springframework:spring-context:jar:5.3.7:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.3.7:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-couchbase:jar:2.5.0:compile
[INFO] |  \- org.springframework.data:spring-data-couchbase:jar:4.2.1:compile
[INFO] |     +- org.springframework:spring-context-support:jar:5.3.7:compile
[INFO] |     +- org.springframework:spring-tx:jar:5.3.7:compile
[INFO] |     +- org.springframework.data:spring-data-commons:jar:2.5.1:compile
[INFO] |     \- com.couchbase.client:java-client:jar:3.1.5:compile
[INFO] |        \- com.couchbase.client:core-io:jar:2.1.5:compile
[INFO] |           \- io.projectreactor:reactor-core:jar:3.4.6:compile
[INFO] |              \- org.reactivestreams:reactive-streams:jar:1.0.3:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.5.0:runtime (optional) 
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.5.0:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:2.5.0:compile
[INFO] \- org.projectlombok:lombok:jar:1.18.20:compile (optional) 

Is this an issue or am I missing something here ?

Stack overflow

https://stackoverflow.com/questions/68284671/couchbase-spring-jpa-with-query-and-pageable-throwing-arrayindexoutofboundsexce
https://stackoverflow.com/questions/66151373/spring-data-couchbase-getting-java-lang-arrayindexoutofboundsexception-index

Thanks.

@schauder schauder changed the title Couchbase Spring JPA with @Query and Pageable / Sort throwing ArrayIndexOutOfBoundsException - PagingAndSortingRepository, CouchbaseRepository Spring Data Couchbase with @Query and Pageable / Sort throwing ArrayIndexOutOfBoundsException - PagingAndSortingRepository, CouchbaseRepository Jul 12, 2021
@mp911de mp911de transferred this issue from spring-projects/spring-data-commons Jul 12, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 12, 2021
@mikereiche
Copy link
Collaborator

StringBasedN1qlQueryParser needs to also include Pageable and Sort as parameters (the iterator only provides bindable parameters).

private static Object[] getParameters(ParameterAccessor accessor) {
	ArrayList<Object> params = new ArrayList<>();
	for (Object o : accessor) {
		params.add(o);
	}
	_if(accessor.getPageable() != null){
		params.add(accessor.getPageable());
	}
	if(accessor.getSort() != null){
		params.add(accessor.getSort());
	}_
	return params.toArray();
}

The execution of pageable string-n1ql queries needs additional work.

@mikereiche mikereiche added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 12, 2021
@mikereiche mikereiche self-assigned this Jul 12, 2021
@vals-productions
Copy link

Same issue exists for ReactiveSortingRepository (tested with Couchbase) .

The Issue did not exist until I upgraded spring libraries to work with Couchbase SDK 3 (Spring Data Couchbase 4). Upon upgrade existing code stopped working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants