Skip to content

Commit 21c53dd

Browse files
committed
Fix #1324: Add @JsonAlias support to SimplePageable for SNAKE_CASE compatibility
Signed-off-by: weslyvinicius <weslyvinicius@hotmail.com>
1 parent a91d8f5 commit 21c53dd

6 files changed

Lines changed: 387 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ static class SimplePageable implements Pageable {
242242

243243
private final PageRequest delegate;
244244

245-
SimplePageable(@JsonProperty("pageNumber") int number, @JsonProperty("pageSize") int size,
246-
@JsonProperty("sort") Sort sort) {
245+
SimplePageable(
246+
@JsonProperty("pageNumber") @JsonAlias({"page-number", "page_number", "pagenumber", "PageNumber"}) int number,
247+
@JsonProperty("pageSize") @JsonAlias({"page-size", "page_size", "pagesize", "PageSize"}) int size,
248+
@JsonProperty("sort") Sort sort) {
247249
delegate = buildPageRequest(number, size, sort);
248250
}
249251

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.fasterxml.jackson.core.JsonProcessingException;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
2526
import org.junit.jupiter.api.BeforeAll;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.params.ParameterizedTest;
@@ -188,4 +189,83 @@ void serializeAndDeserializeFilledMultipleCascade() throws JsonProcessingExcepti
188189
assertThat(cascadedResult.getPageable().getPageNumber()).isEqualTo(6);
189190
}
190191

192+
@Test
193+
void deserializePageableWithHyphenatedAlias() throws IOException {
194+
// Given
195+
ObjectMapper snakeCaseMapper = objectMapper.copy();
196+
snakeCaseMapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
197+
File file = new File("./src/test/resources/withPageableAliasHyphen.json");
198+
// When
199+
Page<?> result = objectMapper.readValue(file, Page.class);
200+
// Then
201+
assertThat(result).isNotNull();
202+
assertThat(result.getTotalElements()).isEqualTo(15);
203+
assertThat(result.getContent()).hasSize(10);
204+
assertThat(result.getPageable()).isNotNull();
205+
assertThat(result.getPageable().getPageNumber()).isEqualTo(2);
206+
assertThat(result.getPageable().getPageSize()).isEqualTo(3);
207+
assertThat(result.getPageable().getSort().getOrderFor("firstName").getDirection())
208+
.isEqualTo(Sort.Direction.ASC);
209+
}
210+
211+
@Test
212+
void deserializePageableWithUnderscoreAlias() throws IOException {
213+
// Given
214+
ObjectMapper snakeCaseMapper = objectMapper.copy();
215+
snakeCaseMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
216+
File file = new File("./src/test/resources/withPageableAliasUnderscore.json");
217+
218+
// When
219+
Page<?> result = snakeCaseMapper.readValue(file, Page.class);
220+
// Then
221+
assertThat(result).isNotNull();
222+
assertThat(result.getTotalElements()).isEqualTo(10);
223+
assertThat(result.getContent()).hasSize(10);
224+
assertThat(result.getPageable()).isNotNull();
225+
assertThat(result.getPageable().getPageNumber()).isEqualTo(1);
226+
assertThat(result.getPageable().getPageSize()).isEqualTo(2);
227+
assertThat(result.getPageable().getSort().getOrderFor("lastName").getDirection())
228+
.isEqualTo(Sort.Direction.DESC);
229+
}
230+
231+
@Test
232+
void deserializePageableWithLowercaseAlias() throws IOException {
233+
// Given
234+
ObjectMapper snakeCaseMapper = objectMapper.copy();
235+
snakeCaseMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE);
236+
237+
File file = new File("./src/test/resources/withPageableAliasLowercase.json");
238+
239+
// When
240+
Page<?> result = objectMapper.readValue(file, Page.class);
241+
// Then
242+
assertThat(result).isNotNull();
243+
assertThat(result.getTotalElements()).isEqualTo(8);
244+
assertThat(result.getContent()).hasSize(10);
245+
assertThat(result.getPageable()).isNotNull();
246+
assertThat(result.getPageable().getPageNumber()).isEqualTo(0);
247+
assertThat(result.getPageable().getPageSize()).isEqualTo(4);
248+
assertThat(result.getPageable().getSort()).isEqualTo(Sort.unsorted());
249+
}
250+
251+
@Test
252+
void deserializePageableWithPascalCaseAlias() throws IOException {
253+
// Given
254+
ObjectMapper snakeCaseMapper = objectMapper.copy();
255+
snakeCaseMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
256+
File file = new File("./src/test/resources/withPageableAliasPascalCase.json");
257+
258+
// When
259+
Page<?> result = objectMapper.readValue(file, Page.class);
260+
// Then
261+
assertThat(result).isNotNull();
262+
assertThat(result.getTotalElements()).isEqualTo(20);
263+
assertThat(result.getContent()).hasSize(10);
264+
assertThat(result.getPageable()).isNotNull();
265+
assertThat(result.getPageable().getPageNumber()).isEqualTo(3);
266+
assertThat(result.getPageable().getPageSize()).isEqualTo(2);
267+
assertThat(result.getPageable().getSort().getOrderFor("firstName").getDirection())
268+
.isEqualTo(Sort.Direction.ASC);
269+
}
270+
191271
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"content": [
3+
{
4+
"id": 3,
5+
"lastName": "Williams",
6+
"firstName": "Thomas",
7+
"email": "w.t@my.domain.com"
8+
},
9+
{
10+
"id": 1,
11+
"lastName": "Smith",
12+
"firstName": "James",
13+
"email": "s.j@my.domain.com"
14+
},
15+
{
16+
"id": 11,
17+
"lastName": "Scott",
18+
"firstName": "Steven",
19+
"email": "s.s@my.domain.com"
20+
},
21+
{
22+
"id": 8,
23+
"lastName": "Rodriguez",
24+
"firstName": "Daniel",
25+
"email": "r.d@my.domain.com"
26+
},
27+
{
28+
"id": 9,
29+
"lastName": "Martinez",
30+
"firstName": "Robert",
31+
"email": "m.r@my.domain.com"
32+
},
33+
{
34+
"id": 5,
35+
"lastName": "Jones",
36+
"firstName": "James",
37+
"email": "j.j@my.domain.com"
38+
},
39+
{
40+
"id": 2,
41+
"lastName": "Johnson",
42+
"firstName": "Robert",
43+
"email": "j.r@my.domain.com"
44+
},
45+
{
46+
"id": 6,
47+
"lastName": "Garcia",
48+
"firstName": "William",
49+
"email": "g.w@my.domain.com"
50+
},
51+
{
52+
"id": 7,
53+
"lastName": "Davis",
54+
"firstName": "Richard",
55+
"email": "d.r@my.domain.com"
56+
},
57+
{
58+
"id": 4,
59+
"lastName": "Brown",
60+
"firstName": "Paul",
61+
"email": "b.p@my.domain.com"
62+
}
63+
],
64+
"pageable": {
65+
"page-number": 2,
66+
"page-size": 3,
67+
"sort": {
68+
"orders": [
69+
{
70+
"direction": "ASC",
71+
"property": "firstName"
72+
}
73+
]
74+
}
75+
},
76+
"totalElements": 15
77+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"content": [
3+
{
4+
"id": 3,
5+
"lastName": "Williams",
6+
"firstName": "Thomas",
7+
"email": "w.t@my.domain.com"
8+
},
9+
{
10+
"id": 1,
11+
"lastName": "Smith",
12+
"firstName": "James",
13+
"email": "s.j@my.domain.com"
14+
},
15+
{
16+
"id": 11,
17+
"lastName": "Scott",
18+
"firstName": "Steven",
19+
"email": "s.s@my.domain.com"
20+
},
21+
{
22+
"id": 8,
23+
"lastName": "Rodriguez",
24+
"firstName": "Daniel",
25+
"email": "r.d@my.domain.com"
26+
},
27+
{
28+
"id": 9,
29+
"lastName": "Martinez",
30+
"firstName": "Robert",
31+
"email": "m.r@my.domain.com"
32+
},
33+
{
34+
"id": 5,
35+
"lastName": "Jones",
36+
"firstName": "James",
37+
"email": "j.j@my.domain.com"
38+
},
39+
{
40+
"id": 2,
41+
"lastName": "Johnson",
42+
"firstName": "Robert",
43+
"email": "j.r@my.domain.com"
44+
},
45+
{
46+
"id": 6,
47+
"lastName": "Garcia",
48+
"firstName": "William",
49+
"email": "g.w@my.domain.com"
50+
},
51+
{
52+
"id": 7,
53+
"lastName": "Davis",
54+
"firstName": "Richard",
55+
"email": "d.r@my.domain.com"
56+
},
57+
{
58+
"id": 4,
59+
"lastName": "Brown",
60+
"firstName": "Paul",
61+
"email": "b.p@my.domain.com"
62+
}
63+
],
64+
"pageable": {
65+
"pagenumber": 0,
66+
"pagesize": 4,
67+
"sort": {
68+
"orders": []
69+
}
70+
},
71+
"totalElements": 8
72+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"content": [
3+
{
4+
"id": 3,
5+
"lastName": "Williams",
6+
"firstName": "Thomas",
7+
"email": "w.t@my.domain.com"
8+
},
9+
{
10+
"id": 1,
11+
"lastName": "Smith",
12+
"firstName": "James",
13+
"email": "s.j@my.domain.com"
14+
},
15+
{
16+
"id": 11,
17+
"lastName": "Scott",
18+
"firstName": "Steven",
19+
"email": "s.s@my.domain.com"
20+
},
21+
{
22+
"id": 8,
23+
"lastName": "Rodriguez",
24+
"firstName": "Daniel",
25+
"email": "r.d@my.domain.com"
26+
},
27+
{
28+
"id": 9,
29+
"lastName": "Martinez",
30+
"firstName": "Robert",
31+
"email": "m.r@my.domain.com"
32+
},
33+
{
34+
"id": 5,
35+
"lastName": "Jones",
36+
"firstName": "James",
37+
"email": "j.j@my.domain.com"
38+
},
39+
{
40+
"id": 2,
41+
"lastName": "Johnson",
42+
"firstName": "Robert",
43+
"email": "j.r@my.domain.com"
44+
},
45+
{
46+
"id": 6,
47+
"lastName": "Garcia",
48+
"firstName": "William",
49+
"email": "g.w@my.domain.com"
50+
},
51+
{
52+
"id": 7,
53+
"lastName": "Davis",
54+
"firstName": "Richard",
55+
"email": "d.r@my.domain.com"
56+
},
57+
{
58+
"id": 4,
59+
"lastName": "Brown",
60+
"firstName": "Paul",
61+
"email": "b.p@my.domain.com"
62+
}
63+
],
64+
"pageable": {
65+
"PageNumber": 3,
66+
"PageSize": 2,
67+
"sort": {
68+
"orders": [
69+
{
70+
"direction": "ASC",
71+
"property": "firstName"
72+
}
73+
]
74+
}
75+
},
76+
"totalElements": 20
77+
}

0 commit comments

Comments
 (0)