Support all non-deprecated Jackson naming strategies for page deserialization. Fixes gh-170. (#477)

This commit is contained in:
Olga Maciaszek-Sharma
2021-02-02 14:10:46 +01:00
committed by GitHub
parent 0c4c19bfcd
commit 27a3967975
2 changed files with 14 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.Version;
@@ -36,6 +37,7 @@ import org.springframework.data.domain.Sort;
* This Jackson module provides support to deserialize Spring {@link Page} objects.
*
* @author Pascal Büttiker
* @author Olga Maciaszek-Sharma
*/
public class PageJacksonModule extends Module {
@@ -65,7 +67,9 @@ public class PageJacksonModule extends Module {
SimplePageImpl(@JsonProperty("content") List<T> content,
@JsonProperty("number") int number, @JsonProperty("size") int size,
@JsonProperty("totalElements") long totalElements,
@JsonProperty("totalElements") @JsonAlias({ "total-elements",
"total_elements", "totalelements",
"TotalElements" }) long totalElements,
@JsonProperty("sort") Sort sort) {
PageRequest pageRequest;
if (sort != null) {

View File

@@ -19,14 +19,18 @@ package org.springframework.cloud.openfeign.support;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.data.domain.Page;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PageJacksonModule}.
*
* @author Ruben Vervaeke
* @author Olga Maciaszek-Sharma
*/
public class PageJacksonModuleTests {
@@ -38,10 +42,11 @@ public class PageJacksonModuleTests {
objectMapper.registerModule(new PageJacksonModule());
}
@Test
public void deserializePage() throws JsonProcessingException {
@ParameterizedTest
@ValueSource(strings = {"totalElements", "total-elements", "total_elements", "totalelements", "TotalElements"})
public void deserializePage(String totalElements) throws JsonProcessingException {
// Given
String pageJson = "{\"content\":[\"A name\"], \"number\":1, \"size\":2, \"totalElements\": 3}";
String pageJson = "{\"content\":[\"A name\"], \"number\":1, \"size\":2, \"" + totalElements + "\": 3}";
// When
Page<?> result = objectMapper.readValue(pageJson, Page.class);
// Then