Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
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 Down Expand Up @@ -88,6 +89,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 +102,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
Expand Up @@ -85,6 +85,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
}
}