From dd91dcdf8d7eeeee4446b5a793a391ff7d058352 Mon Sep 17 00:00:00 2001 From: rvervaek Date: Mon, 13 Apr 2020 22:58:15 +0200 Subject: [PATCH] Use property 'number' in JSON Page representation instead of 'page' (#320) Fixes gh-237 Co-authored-by: rvvaeke --- .../openfeign/support/PageJacksonModule.java | 4 +- .../support/PageJacksonModuleTests.java | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageJacksonModuleTests.java diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageJacksonModule.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageJacksonModule.java index b3bbd6e7..2f7bc69e 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageJacksonModule.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageJacksonModule.java @@ -64,7 +64,7 @@ public class PageJacksonModule extends Module { private final Page delegate; SimplePageImpl(@JsonProperty("content") List content, - @JsonProperty("page") int number, @JsonProperty("size") int size, + @JsonProperty("number") int number, @JsonProperty("size") int size, @JsonProperty("totalElements") long totalElements) { delegate = new PageImpl<>(content, PageRequest.of(number, size), totalElements); @@ -82,7 +82,7 @@ public class PageJacksonModule extends Module { return delegate.getTotalElements(); } - @JsonProperty("page") + @JsonProperty @Override public int getNumber() { return delegate.getNumber(); diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageJacksonModuleTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageJacksonModuleTests.java new file mode 100644 index 00000000..465617fd --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageJacksonModuleTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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.springframework.data.domain.Page; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Ruben Vervaeke + */ +public class PageJacksonModuleTests { + + private static ObjectMapper objectMapper; + + @BeforeAll + public static void initialize() { + objectMapper = new ObjectMapper(); + objectMapper.registerModule(new PageJacksonModule()); + } + + @Test + public void deserializePage() throws JsonProcessingException { + // Given + String pageJson = "{\"content\":[\"A name\"], \"number\":1, \"size\":2, \"totalElements\": 3}"; + // When + Page result = objectMapper.readValue(pageJson, Page.class); + // Then + assertThat(result).isNotNull(); + assertThat(result.getTotalElements()).isEqualTo(3); + assertThat(result.getContent()).hasSize(1); + assertThat(result.getPageable()).isNotNull(); + assertThat(result.getPageable().getPageSize()).isEqualTo(2); + assertThat(result.getPageable().getPageNumber()).isEqualTo(1); + } + +}