Skip to content

Commit 749e917

Browse files
Add support for PageSerializationMode.VIA_DTO (PagedModel) in PageJacksonModule
Signed-off-by: bstewart <[email protected]>
1 parent 6ddb081 commit 749e917

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageJacksonModule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.domain.PageRequest;
3434
import org.springframework.data.domain.Pageable;
3535
import org.springframework.data.domain.Sort;
36+
import org.springframework.data.web.PagedModel;
3637

3738
/**
3839
* This Jackson module provides support to deserialize Spring {@link Page} objects.
@@ -88,6 +89,7 @@ static class SimplePageImpl<T> implements Page<T> {
8889
private final Page<T> delegate;
8990

9091
SimplePageImpl(@JsonProperty("content") List<T> content, @JsonProperty("pageable") Pageable pageable,
92+
@JsonProperty("page") PagedModel.PageMetadata pageMetadata,
9193
@JsonProperty("number") @JsonAlias("pageNumber") int number,
9294
@JsonProperty("size") @JsonAlias("pageSize") int size,
9395
@JsonProperty("totalElements") @JsonAlias({ "total-elements", "total_elements", "totalelements",
@@ -100,6 +102,11 @@ static class SimplePageImpl<T> implements Page<T> {
100102
else if (pageable != null && pageable.getPageSize() > 0) {
101103
delegate = new PageImpl<>(content, pageable, totalElements);
102104
}
105+
else if (pageMetadata != null && pageMetadata.size() > 0) {
106+
PageRequest pageRequest = buildPageRequest((int) pageMetadata.number(), (int) pageMetadata.size(),
107+
null);
108+
delegate = new PageImpl<>(content, pageRequest, pageMetadata.totalElements());
109+
}
103110
else {
104111
delegate = new PageImpl<>(content);
105112
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageJacksonModuleTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ void deserializePageFromFileWithPageable(String filePath) throws IOException {
8585
.isEqualTo(Sort.Direction.DESC);
8686
}
8787

88+
@ParameterizedTest
89+
@ValueSource(strings = { "./src/test/resources/withPage.json" })
90+
void deserializePageFromFileWithPage(String filePath) throws IOException {
91+
File file = new File(filePath);
92+
93+
Page<?> result = objectMapper.readValue(file, Page.class);
94+
95+
assertThat(result.getTotalElements()).isEqualTo(11);
96+
assertThat(result.getContent()).hasSize(10);
97+
assertThat(result.getPageable().getPageNumber()).isEqualTo(0);
98+
assertThat(result.getPageable().getSort()).isEqualTo(Sort.unsorted());
99+
}
100+
88101
@Test
89102
void serializeAndDeserializeEmpty() throws JsonProcessingException {
90103
// Given
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"content": [
3+
{
4+
"id": 3,
5+
"lastName": "Williams",
6+
"firstName": "Thomas",
7+
"email": "[email protected]"
8+
},
9+
{
10+
"id": 1,
11+
"lastName": "Smith",
12+
"firstName": "James",
13+
"email": "[email protected]"
14+
},
15+
{
16+
"id": 11,
17+
"lastName": "Scott",
18+
"firstName": "Steven",
19+
"email": "[email protected]"
20+
},
21+
{
22+
"id": 8,
23+
"lastName": "Rodriguez",
24+
"firstName": "Daniel",
25+
"email": "[email protected]"
26+
},
27+
{
28+
"id": 9,
29+
"lastName": "Martinez",
30+
"firstName": "Robert",
31+
"email": "[email protected]"
32+
},
33+
{
34+
"id": 5,
35+
"lastName": "Jones",
36+
"firstName": "James",
37+
"email": "[email protected]"
38+
},
39+
{
40+
"id": 2,
41+
"lastName": "Johnson",
42+
"firstName": "Robert",
43+
"email": "[email protected]"
44+
},
45+
{
46+
"id": 6,
47+
"lastName": "Garcia",
48+
"firstName": "William",
49+
"email": "[email protected]"
50+
},
51+
{
52+
"id": 7,
53+
"lastName": "Davis",
54+
"firstName": "Richard",
55+
"email": "[email protected]"
56+
},
57+
{
58+
"id": 4,
59+
"lastName": "Brown",
60+
"firstName": "Paul",
61+
"email": "[email protected]"
62+
}
63+
],
64+
"page": {
65+
"number": 0,
66+
"size": 2,
67+
"totalElements": 11,
68+
"totalPages": 6
69+
}
70+
}

0 commit comments

Comments
 (0)