Skip to content

Commit

Permalink
Fix mapping empty page (#3180)
Browse files Browse the repository at this point in the history
* Fix mapping empty page

* Just found wrong reference in docs and will push fix in this unrelated PR
  • Loading branch information
radovanradic authored Oct 16, 2024
1 parent 2edd115 commit 379e4e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@DefaultImplementation(DefaultCursoredPage.class)
public interface CursoredPage<T> extends Page<T> {

CursoredPage<?> EMPTY = new DefaultCursoredPage<>(Collections.emptyList(), Pageable.unpaged(), Collections.emptyList(), null);
CursoredPage<?> EMPTY = new DefaultCursoredPage<>(Collections.emptyList(), Pageable.unpaged(), Collections.emptyList(), -1L);

/**
* @return Whether this {@link CursoredPage} contains the total count of the records
Expand Down Expand Up @@ -125,6 +125,9 @@ default CursoredPageable previousPageable() {
*/
@Override
default @NonNull <T2> CursoredPage<T2> map(Function<T, T2> function) {
if (this == EMPTY) {
return (CursoredPage<T2>) EMPTY;
}
List<T2> content = getContent().stream().map(function).collect(Collectors.toList());
return new DefaultCursoredPage<>(content, getPageable(), getCursors(), hasTotalSize() ? getTotalSize() : null);
}
Expand Down
3 changes: 3 additions & 0 deletions data-model/src/main/java/io/micronaut/data/model/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ default boolean hasNext() {
*/
@Override
default @NonNull <T2> Page<T2> map(Function<T, T2> function) {
if (this == EMPTY) {
return (Page<T2>) EMPTY;
}
List<T2> content = getContent().stream().map(function).toList();
return new DefaultPage<>(content, getPageable(), hasTotalSize() ? getTotalSize() : null);
}
Expand Down
18 changes: 18 additions & 0 deletions data-model/src/test/groovy/io/micronaut/data/model/PageSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ class PageSpec extends Specification {
!mappedCursoredPage.hasTotalSize()
}

void "empty page can be mapped "() {
when:
Page<String> page = Page.empty()
def copy = page.map { it -> it }
copy.getTotalSize()

then:
notThrown(Throwable)

when:
CursoredPage<String> cursoredPage = CursoredPage.empty()
def newCopy = cursoredPage.map {it -> it }
newCopy.getTotalSize()

then:
notThrown(Throwable)
}

@EqualsAndHashCode
@ToString
@Serdeable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dependency:jakarta.persistence:jakarta.persistence-api[]

To implement queries that cannot be defined at the compile-time Micronaut Data introduces api:data.repository.JpaSpecificationExecutor[] repository interface that can be used to extend your repository interface:

snippet::example.PersonRepository[project-base="doc-examples/mongo-example",source="main" tags="repository",indent="0"]
snippet::example.PersonRepository[project-base="doc-examples/azure-cosmos-example",source="main" tags="repository",indent="0"]

Each method expects a "specification" which is a functional interface with a set of Criteria API objects intended to build a query programmatically.

Expand Down

0 comments on commit 379e4e3

Please sign in to comment.