Skip to content
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

Fix serialization of Page.empty() #3142

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading