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

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

Open
cmsong111 opened this issue Apr 2, 2025 · 0 comments
Assignees
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@cmsong111
Copy link

cmsong111 commented Apr 2, 2025

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 2, 2025
@mp911de mp911de assigned mp911de and odrotbohm and unassigned mp911de Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants