Skip to content

Commit

Permalink
Fix serialization of Page.empty() (#3142)
Browse files Browse the repository at this point in the history
* Fix serialization of Page.empty()

* Add javadoc comment
  • Loading branch information
radovanradic authored Sep 24, 2024
1 parent af88d15 commit a608ef4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DefaultPage<T> extends DefaultSlice<T> implements Page<T> {

@Override
public boolean hasTotalSize() {
return totalSize != null;
return totalSize != null && totalSize != -1L;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion data-model/src/main/java/io/micronaut/data/model/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@DefaultImplementation(DefaultPage.class)
public interface Page<T> extends Slice<T> {

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
Expand All @@ -62,6 +62,7 @@ public interface Page<T> extends Slice<T> {
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dummy>) 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<Dummy>) mapper.readValue(json, mapper.typeFactory.constructParametricType(Page, Dummy))
!deserializedEmptyPage.hasTotalSize()
}

void "test serialization and deserialization of a page - serde"() {
Expand All @@ -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"() {
Expand Down

0 comments on commit a608ef4

Please sign in to comment.