From ff30485b403a9db9e2e67ff8580361aef867e56b Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 28 Jul 2021 17:36:54 +0800 Subject: [PATCH] Respect SpringDataWebProperties where possible (#583) --- .../openfeign/FeignClientsConfiguration.java | 9 +- .../support/PageableSpringEncoder.java | 5 +- .../PageableSpringQueryMapEncoder.java | 34 ++++- .../support/PageableEncoderTests.java | 27 +++- ...PageableEncoderWithSpringDataWebTests.java | 52 +++++++ .../PageableSpringQueryMapEncoderTests.java | 127 ++++++++++++++++++ ...QueryMapEncoderWithSpringDataWebTests.java | 52 +++++++ 7 files changed, 293 insertions(+), 13 deletions(-) create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderWithSpringDataWebTests.java create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderTests.java create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderWithSpringDataWebTests.java diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsConfiguration.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsConfiguration.java index c1e378a0..f25b0da0 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsConfiguration.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsConfiguration.java @@ -71,6 +71,7 @@ import static feign.form.ContentType.MULTIPART; * @author Jonatan Ivanov * @author Olga Maciaszek-Sharma * @author Hyeonmin Park + * @author Yanming Zhou */ @Configuration(proxyBeanMethods = false) public class FeignClientsConfiguration { @@ -130,7 +131,13 @@ public class FeignClientsConfiguration { @ConditionalOnClass(name = "org.springframework.data.domain.Pageable") @ConditionalOnMissingBean public QueryMapEncoder feignQueryMapEncoderPageable() { - return new PageableSpringQueryMapEncoder(); + PageableSpringQueryMapEncoder queryMapEncoder = new PageableSpringQueryMapEncoder(); + if (springDataWebProperties != null) { + queryMapEncoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter()); + queryMapEncoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter()); + queryMapEncoder.setSortParameter(springDataWebProperties.getSort().getSortParameter()); + } + return queryMapEncoder; } @Bean diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java index 916044f1..d0f66ac0 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java @@ -32,6 +32,7 @@ import org.springframework.data.domain.Sort; * Provides support for encoding spring Pageable via composition. * * @author Pascal Büttiker + * @author Yanming Zhou */ public class PageableSpringEncoder implements Encoder { @@ -82,8 +83,8 @@ public class PageableSpringEncoder implements Encoder { Pageable pageable = (Pageable) object; if (pageable.isPaged()) { - template.query(pageParameter, pageable.getPageNumber() + ""); - template.query(sizeParameter, pageable.getPageSize() + ""); + template.query(pageParameter, String.valueOf(pageable.getPageNumber())); + template.query(sizeParameter, String.valueOf(pageable.getPageSize())); } if (pageable.getSort() != null) { diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java index 0a2171b6..ab72411a 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java @@ -31,10 +31,38 @@ import org.springframework.data.domain.Sort; * {@link org.springframework.cloud.openfeign.SpringQueryMap}. * * @author Hyeonmin Park + * @author Yanming Zhou * @since 2.2.8 */ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder { + /** + * Page index parameter name. + */ + private String pageParameter = "page"; + + /** + * Page size parameter name. + */ + private String sizeParameter = "size"; + + /** + * Sort parameter name. + */ + private String sortParameter = "sort"; + + public void setPageParameter(String pageParameter) { + this.pageParameter = pageParameter; + } + + public void setSizeParameter(String sizeParameter) { + this.sizeParameter = sizeParameter; + } + + public void setSortParameter(String sortParameter) { + this.sortParameter = sortParameter; + } + @Override public Map encode(Object object) { if (supports(object)) { @@ -44,8 +72,8 @@ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder { Pageable pageable = (Pageable) object; if (pageable.isPaged()) { - queryMap.put("page", pageable.getPageNumber()); - queryMap.put("size", pageable.getPageSize()); + queryMap.put(pageParameter, pageable.getPageNumber()); + queryMap.put(sizeParameter, pageable.getPageSize()); } if (pageable.getSort() != null) { @@ -69,7 +97,7 @@ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder { sortQueries.add(order.getProperty() + "%2C" + order.getDirection()); } if (!sortQueries.isEmpty()) { - queryMap.put("sort", sortQueries); + queryMap.put(sortParameter, sortQueries); } } diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java index 15ff0c53..ad3f7579 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java @@ -37,6 +37,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * Tests the pagination encoding and sorting. * * @author Charlie Mordant. + * @author Yanming Zhou */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, @@ -55,6 +56,18 @@ public class PageableEncoderTests { @Autowired private FeignContext context; + protected String getPageParameter() { + return "page"; + } + + protected String getSizeParameter() { + return "size"; + } + + protected String getSortParameter() { + return "sort"; + } + @Test public void testPaginationAndSortingRequest() { Encoder encoder = this.context.getInstance("foo", Encoder.class); @@ -65,11 +78,11 @@ public class PageableEncoderTests { // Request queries shall contain three entries assertThat(request.queries()).hasSize(3); // Request page shall contain page - assertThat(request.queries().get("page")).contains(String.valueOf(PAGE)); + assertThat(request.queries().get(getPageParameter())).contains(String.valueOf(PAGE)); // Request size shall contain size - assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); + assertThat(request.queries().get(getSizeParameter())).contains(String.valueOf(SIZE)); // Request sort size shall contain sort entries - assertThat(request.queries().get("sort")).hasSize(2); + assertThat(request.queries().get(getSortParameter())).hasSize(2); } private Pageable createPageAndSortRequest() { @@ -84,11 +97,11 @@ public class PageableEncoderTests { encoder.encode(createPageAndRequest(), null, request); assertThat(request.queries().size()).isEqualTo(2); // Request page shall contain page - assertThat(request.queries().get("page")).contains(String.valueOf(PAGE)); + assertThat(request.queries().get(getPageParameter())).contains(String.valueOf(PAGE)); // Request size shall contain size - assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); + assertThat(request.queries().get(getSizeParameter())).contains(String.valueOf(SIZE)); // Request sort size shall contain sort entries - assertThat(request.queries()).doesNotContainKey("sort"); + assertThat(request.queries()).doesNotContainKey(getSortParameter()); } private Pageable createPageAndRequest() { @@ -105,7 +118,7 @@ public class PageableEncoderTests { // Request queries shall contain three entries assertThat(request.queries().size()).isEqualTo(1); // Request sort size shall contain sort entries - assertThat(request.queries().get("sort")).hasSize(2); + assertThat(request.queries().get(getSortParameter())).hasSize(2); } private Sort createSort() { diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderWithSpringDataWebTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderWithSpringDataWebTests.java new file mode 100644 index 00000000..a81c46b3 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderWithSpringDataWebTests.java @@ -0,0 +1,52 @@ +/* + * 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 org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Tests the pagination encoding and sorting. + * + * @author Yanming Zhou + */ +@EnableConfigurationProperties(SpringDataWebProperties.class) +@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, + value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false", + "spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize", + "spring.data.web.sort.sortParameter=orderBy" }) +public class PageableEncoderWithSpringDataWebTests extends PageableEncoderTests { + + @Override + protected String getPageParameter() { + return "pageNo"; + } + + @Override + protected String getSizeParameter() { + return "pageSize"; + } + + @Override + protected String getSortParameter() { + return "orderBy"; + } + +} diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderTests.java new file mode 100644 index 00000000..2ac911e2 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderTests.java @@ -0,0 +1,127 @@ +/* + * 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 java.util.List; +import java.util.Map; + +import feign.QueryMapEncoder; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.openfeign.FeignContext; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Tests the pagination encoding and sorting. + * + * @author Yanming Zhou + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, + value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false" }) +@DirtiesContext +public class PageableSpringQueryMapEncoderTests { + + public static final int PAGE = 1; + + public static final int SIZE = 10; + + public static final String SORT_2 = "sort2"; + + public static final String SORT_1 = "sort1"; + + @Autowired + private FeignContext context; + + protected String getPageParameter() { + return "page"; + } + + protected String getSizeParameter() { + return "size"; + } + + protected String getSortParameter() { + return "sort"; + } + + @Test + public void testPaginationAndSortingRequest() { + QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); + assertThat(encoder).isNotNull(); + + Map map = encoder.encode(createPageAndSortRequest()); + assertThat(map).hasSize(3); + assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE); + assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE); + assertThat((List) map.get(getSortParameter())).hasSize(2); + } + + private Pageable createPageAndSortRequest() { + return PageRequest.of(PAGE, SIZE, Sort.Direction.ASC, SORT_1, SORT_2); + } + + @Test + public void testPaginationRequest() { + QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); + assertThat(encoder).isNotNull(); + + Map map = encoder.encode(createPageAndRequest()); + assertThat(map).hasSize(2); + assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE); + assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE); + assertThat(map).doesNotContainKey(getSortParameter()); + } + + private Pageable createPageAndRequest() { + return PageRequest.of(PAGE, SIZE); + } + + @Test + public void testSortingRequest() { + QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); + assertThat(encoder).isNotNull(); + + Map map = encoder.encode(createSort()); + assertThat(map).hasSize(1); + assertThat((List) map.get(getSortParameter())).hasSize(2); + } + + private Sort createSort() { + return Sort.by(SORT_1, SORT_2).ascending(); + } + + @Test + public void testUnpagedRequest() { + QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); + assertThat(encoder).isNotNull(); + + Map map = encoder.encode(Pageable.unpaged()); + assertThat(map).isEmpty(); + } + +} diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderWithSpringDataWebTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderWithSpringDataWebTests.java new file mode 100644 index 00000000..914a0516 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderWithSpringDataWebTests.java @@ -0,0 +1,52 @@ +/* + * 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 org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Tests the pagination encoding and sorting. + * + * @author Yanming Zhou + */ +@EnableConfigurationProperties(SpringDataWebProperties.class) +@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, + value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false", + "spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize", + "spring.data.web.sort.sortParameter=orderBy" }) +public class PageableSpringQueryMapEncoderWithSpringDataWebTests extends PageableSpringQueryMapEncoderTests { + + @Override + protected String getPageParameter() { + return "pageNo"; + } + + @Override + protected String getSizeParameter() { + return "pageSize"; + } + + @Override + protected String getSortParameter() { + return "orderBy"; + } + +}