spring-projects / spring-projects/spring-data-commons

Refactor `PagedModel<T>`: Remove `Page<T>` properties and improve JSON serialization stability

Open
#3,264 0 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

Currently, PagedModel<T> directly holds a Page<T> instance, which introduces unnecessary complexity during JSON serialization and deserialization.

Therefore, it is suggested to remove the Page<T> attribute and leave only content and pageMetaData

Proposed Solution

  • Remove the direct dependency on Page and instead manage only the necessary data.
  • Simplify the internal structure by keeping only List content and PageMetadata page.
  • Ensure proper deserialization support using a @JsonCreator constructor.
// Partial implementation highlighting the proposed changes
public class PagedModel<T> {

    private final List<T> content;
    private final PageMetadata page;

    public PagedModel(Page<T> page) {
        Assert.notNull(page, "Page must not be null");
        this.page = new PageMetadata(page.getSize(), page.getNumber(),
                page.getTotalElements(), page.getTotalPages());
        this.content = page.getContent();
    }

    @JsonCreator
    private PagedModel(
            @JsonProperty("content") List<T> content,
            @JsonProperty("page") PageMetadata metadata) {
        this.content = content;
        this.page = metadata;
    }
}
Improved Usability in Tests

By applying this change, writing test cases for API responses becomes more convenient, especially when using WebTestClient in a Spring Boot environment.

val result = webTestClient
    .get()
    .uri("/api/v1/test")
    .exchange()
    .expectStatus().isOk
    .expectBody(object : ParameterizedTypeReference<PagedModel<TestData>>() {})
    .returnResult()

result.responseBody.shouldNotBeNull()
result.responseBody.data?.content?.size shouldBe 5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.