diff --git a/data-model/src/main/java/io/micronaut/data/model/DefaultPage.java b/data-model/src/main/java/io/micronaut/data/model/DefaultPage.java index 1845a899b6c..9d33f2270eb 100644 --- a/data-model/src/main/java/io/micronaut/data/model/DefaultPage.java +++ b/data-model/src/main/java/io/micronaut/data/model/DefaultPage.java @@ -58,7 +58,7 @@ class DefaultPage extends DefaultSlice implements Page { @Override public boolean hasTotalSize() { - return totalSize != null; + return totalSize != null && totalSize != -1L; } @Override diff --git a/data-model/src/main/java/io/micronaut/data/model/Page.java b/data-model/src/main/java/io/micronaut/data/model/Page.java index 9b9032f10d7..12efe238402 100644 --- a/data-model/src/main/java/io/micronaut/data/model/Page.java +++ b/data-model/src/main/java/io/micronaut/data/model/Page.java @@ -50,7 +50,7 @@ @DefaultImplementation(DefaultPage.class) public interface Page extends Slice { - Page EMPTY = new DefaultPage<>(Collections.emptyList(), Pageable.unpaged(), null); + Page EMPTY = new DefaultPage<>(Collections.emptyList(), Pageable.unpaged(), -1L); /** * @return Whether this {@link Page} contains the total count of the records @@ -62,6 +62,7 @@ public interface Page extends Slice { * Get the total count of all the records that can be given by this query. * The method may produce a {@link IllegalStateException} if the {@link Pageable} request * did not ask for total size. + * For {@link #EMPTY} page the value is -1. * * @return The total size of the all records. */ diff --git a/data-model/src/test/groovy/io/micronaut/data/model/PageSpec.groovy b/data-model/src/test/groovy/io/micronaut/data/model/PageSpec.groovy index a69cedb38af..a471fa99eec 100644 --- a/data-model/src/test/groovy/io/micronaut/data/model/PageSpec.groovy +++ b/data-model/src/test/groovy/io/micronaut/data/model/PageSpec.groovy @@ -86,9 +86,16 @@ class PageSpec extends Specification { def json = mapper.writeValueAsString(page) then: - def deserializedPage = mapper.readValue(json, mapper.typeFactory.constructParametricType(Page, Dummy)) + def deserializedPage = (Page) mapper.readValue(json, mapper.typeFactory.constructParametricType(Page, Dummy)) deserializedPage.content.every { it instanceof Dummy } deserializedPage == page + + when:"Serialize and deserialize empty page" + json = mapper.writeValueAsString(Page.empty()) + + then:"It is done without errors" + def deserializedEmptyPage = (Page) mapper.readValue(json, mapper.typeFactory.constructParametricType(Page, Dummy)) + !deserializedEmptyPage.hasTotalSize() } void "test serialization and deserialization of a page - serde"() { @@ -113,6 +120,13 @@ class PageSpec extends Specification { def deserializedPage = serdeMapper.readValue(json, Argument.of(Page, Dummy)) deserializedPage.content.every { it instanceof Dummy } deserializedPage == page + + when:"Serialize and deserialize empty page" + json = serdeMapper.writeValueAsString(Page.empty()) + + then:"It is done without errors" + def deserializedEmptyPage = serdeMapper.readValue(json, Argument.of(Page, Dummy)) + !deserializedEmptyPage.hasTotalSize() } void "test serialization and deserialization of a pageable - serde"() {