Skip to content

Add support for PageSerializationMode.VIA_DTO (PagedModel) in PageJacksonModule #1189

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

Closed
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PagedModel;

/**
* This Jackson module provides support to deserialize Spring {@link Page} objects.
Expand All @@ -41,6 +42,7 @@
* @author Olga Maciaszek-Sharma
* @author Pedro Mendes
* @author Nikita Konev
* @author Bruce Stewart
*/
public class PageJacksonModule extends Module {

Expand Down Expand Up @@ -88,6 +90,7 @@ static class SimplePageImpl<T> implements Page<T> {
private final Page<T> delegate;

SimplePageImpl(@JsonProperty("content") List<T> content, @JsonProperty("pageable") Pageable pageable,
@JsonProperty("page") PagedModel.PageMetadata pageMetadata,
@JsonProperty("number") @JsonAlias("pageNumber") int number,
@JsonProperty("size") @JsonAlias("pageSize") int size,
@JsonProperty("totalElements") @JsonAlias({ "total-elements", "total_elements", "totalelements",
Expand All @@ -100,6 +103,11 @@ static class SimplePageImpl<T> implements Page<T> {
else if (pageable != null && pageable.getPageSize() > 0) {
delegate = new PageImpl<>(content, pageable, totalElements);
}
else if (pageMetadata != null && pageMetadata.size() > 0) {
PageRequest pageRequest = buildPageRequest((int) pageMetadata.number(), (int) pageMetadata.size(),
null);
delegate = new PageImpl<>(content, pageRequest, pageMetadata.totalElements());
}
else {
delegate = new PageImpl<>(content);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,7 @@
* @author Olga Maciaszek-Sharma
* @author Pedro Mendes
* @author Nikita Konev
* @author Bruce Stewart
*/
class PageJacksonModuleTests {

Expand Down Expand Up @@ -85,6 +86,19 @@ void deserializePageFromFileWithPageable(String filePath) throws IOException {
.isEqualTo(Sort.Direction.DESC);
}

@ParameterizedTest
@ValueSource(strings = { "./src/test/resources/withPage.json" })
void deserializePageFromFileWithPage(String filePath) throws IOException {
File file = new File(filePath);

Page<?> result = objectMapper.readValue(file, Page.class);

assertThat(result.getTotalElements()).isEqualTo(11);
assertThat(result.getContent()).hasSize(10);
assertThat(result.getPageable().getPageNumber()).isEqualTo(0);
assertThat(result.getPageable().getSort()).isEqualTo(Sort.unsorted());
}

@Test
void serializeAndDeserializeEmpty() throws JsonProcessingException {
// Given
Expand Down
70 changes: 70 additions & 0 deletions spring-cloud-openfeign-core/src/test/resources/withPage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"content": [
{
"id": 3,
"lastName": "Williams",
"firstName": "Thomas",
"email": "[email protected]"
},
{
"id": 1,
"lastName": "Smith",
"firstName": "James",
"email": "[email protected]"
},
{
"id": 11,
"lastName": "Scott",
"firstName": "Steven",
"email": "[email protected]"
},
{
"id": 8,
"lastName": "Rodriguez",
"firstName": "Daniel",
"email": "[email protected]"
},
{
"id": 9,
"lastName": "Martinez",
"firstName": "Robert",
"email": "[email protected]"
},
{
"id": 5,
"lastName": "Jones",
"firstName": "James",
"email": "[email protected]"
},
{
"id": 2,
"lastName": "Johnson",
"firstName": "Robert",
"email": "[email protected]"
},
{
"id": 6,
"lastName": "Garcia",
"firstName": "William",
"email": "[email protected]"
},
{
"id": 7,
"lastName": "Davis",
"firstName": "Richard",
"email": "[email protected]"
},
{
"id": 4,
"lastName": "Brown",
"firstName": "Paul",
"email": "[email protected]"
}
],
"page": {
"number": 0,
"size": 2,
"totalElements": 11,
"totalPages": 6
}
}