Skip to content

Commit f5446de

Browse files
Add support for PageSerializationMode.VIA_DTO (PagedModel) in PageJacksonModule (#1193)
Signed-off-by: Bruce Stewart <[email protected]>
1 parent ad9cc3b commit f5446de

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.
@@ -41,6 +42,7 @@
4142
* @author Olga Maciaszek-Sharma
4243
* @author Pedro Mendes
4344
* @author Nikita Konev
45+
* @author Bruce Stewart
4446
*/
4547
public class PageJacksonModule extends Module {
4648

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

9092
SimplePageImpl(@JsonProperty("content") List<T> content, @JsonProperty("pageable") Pageable pageable,
93+
@JsonProperty("page") PagedModel.PageMetadata pageMetadata,
9194
@JsonProperty("number") @JsonAlias("pageNumber") int number,
9295
@JsonProperty("size") @JsonAlias("pageSize") int size,
9396
@JsonProperty("totalElements") @JsonAlias({ "total-elements", "total_elements", "totalelements",
@@ -100,6 +103,11 @@ static class SimplePageImpl<T> implements Page<T> {
100103
else if (pageable != null && pageable.getPageSize() > 0) {
101104
delegate = new PageImpl<>(content, pageable, totalElements);
102105
}
106+
else if (pageMetadata != null && pageMetadata.size() > 0) {
107+
PageRequest pageRequest = buildPageRequest((int) pageMetadata.number(), (int) pageMetadata.size(),
108+
null);
109+
delegate = new PageImpl<>(content, pageRequest, pageMetadata.totalElements());
110+
}
103111
else {
104112
delegate = new PageImpl<>(content);
105113
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
* @author Olga Maciaszek-Sharma
4343
* @author Pedro Mendes
4444
* @author Nikita Konev
45+
* @author Bruce Stewart
4546
*/
4647
class PageJacksonModuleTests {
4748

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

89+
@ParameterizedTest
90+
@ValueSource(strings = { "./src/test/resources/withPage.json" })
91+
void deserializePageFromFileWithPage(String filePath) throws IOException {
92+
File file = new File(filePath);
93+
94+
Page<?> result = objectMapper.readValue(file, Page.class);
95+
96+
assertThat(result.getTotalElements()).isEqualTo(11);
97+
assertThat(result.getContent()).hasSize(10);
98+
assertThat(result.getPageable().getPageNumber()).isEqualTo(0);
99+
assertThat(result.getPageable().getSort()).isEqualTo(Sort.unsorted());
100+
}
101+
88102
@Test
89103
void serializeAndDeserializeEmpty() throws JsonProcessingException {
90104
// 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)