Skip to content

Commit

Permalink
Fix query with predication specification and cursored pageable (#3057)
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanradic authored Aug 8, 2024
1 parent 9fdf0e9 commit d819df0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,8 @@ public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<O
}

Pageable pageable = getPageable(context);
if (pageable.isUnpaged()) {
Iterable<?> iterable = findAll(methodKey, context, Type.FIND_PAGE);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);
return Page.of(
resultList,
pageable,
(long) resultList.size()
);
}

Iterable<?> iterable = findAll(methodKey, context, Type.FIND_PAGE);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);

Long count = null;
if (pageable.requestTotal()) {
count = count(methodKey, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,29 @@ abstract class AbstractCursoredPageSpec extends Specification {
cleanup:
bookRepository.deleteAll()
}

void "test cursored pageable without page size"() {
when: "People are searched for"
def pageable = CursoredPageable.from(Sort.of(Sort.Order.desc("name")))
def page = personRepository.findAll(PersonRepository.Specifications.nameLike("BBBB%"), pageable)

then: "The page is correct"
page.offset == 0
page.pageNumber == 0
page.totalSize == 30
page.content
!page.content.empty
page.content.forEach { it -> it instanceof Person}

when: "The next page is retrieved"
page = personRepository.findAll(PersonRepository.Specifications.nameLike("BBBB%"), page.nextPageable())

then: "it is correct"
page.offset == 0
page.pageNumber == 1
page.totalSize == 30
page.nextPageable().offset == 0
page.nextPageable().number == 2
page.content.empty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.micronaut.data.annotation.ParameterExpression;
import io.micronaut.data.annotation.Query;
import io.micronaut.data.model.CursoredPage;
import io.micronaut.data.model.CursoredPageable;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.Slice;
Expand Down Expand Up @@ -177,6 +178,9 @@ public interface PersonRepository extends CrudRepository<Person, Long>, Pageable

CursoredPage<Person> retrieve(@NonNull Pageable pageable);

@NonNull
CursoredPage<Person> findAll(@Nullable PredicateSpecification<Person> spec, CursoredPageable pageable);

final class Specifications {

private Specifications() {
Expand Down

0 comments on commit d819df0

Please sign in to comment.