Browse Source

Fix serialization of Pageable.unpaged

pull/247/head
Glosur 5 years ago committed by Marcin Grzejszczak
parent
commit
2cdc1aceeb
  1. 31
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java
  2. 37
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java

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

@ -37,6 +37,21 @@ public class PageableSpringEncoder implements Encoder {
private final Encoder delegate; private final Encoder delegate;
/**
* Page index parameter name.
*/
private String pageParameter = "page";
/**
* Page size parameter name.
*/
private String sizeParameter = "size";
/**
* Sort parameter name.
*/
private String sortParameter = "sort";
/** /**
* Creates a new PageableSpringEncoder with the given delegate for fallback. If no * Creates a new PageableSpringEncoder with the given delegate for fallback. If no
* delegate is provided and this encoder cant handle the request, an EncodeException * delegate is provided and this encoder cant handle the request, an EncodeException
@ -54,8 +69,12 @@ public class PageableSpringEncoder implements Encoder {
if (supports(object)) { if (supports(object)) {
if (object instanceof Pageable) { if (object instanceof Pageable) {
Pageable pageable = (Pageable) object; Pageable pageable = (Pageable) object;
template.query("page", pageable.getPageNumber() + "");
template.query("size", pageable.getPageSize() + ""); if (pageable.isPaged()) {
template.query(pageParameter, pageable.getPageNumber() + "");
template.query(sizeParameter, pageable.getPageSize() + "");
}
if (pageable.getSort() != null) { if (pageable.getSort() != null) {
applySort(template, pageable.getSort()); applySort(template, pageable.getSort());
} }
@ -82,11 +101,17 @@ public class PageableSpringEncoder implements Encoder {
Collection<String> existingSorts = template.queries().get("sort"); Collection<String> existingSorts = template.queries().get("sort");
List<String> sortQueries = existingSorts != null ? new ArrayList<>(existingSorts) List<String> sortQueries = existingSorts != null ? new ArrayList<>(existingSorts)
: new ArrayList<>(); : new ArrayList<>();
if (!sortParameter.equals("sort")) {
existingSorts = template.queries().get(sortParameter);
if (existingSorts != null) {
sortQueries.addAll(existingSorts);
}
}
for (Sort.Order order : sort) { for (Sort.Order order : sort) {
sortQueries.add(order.getProperty() + "," + order.getDirection()); sortQueries.add(order.getProperty() + "," + order.getDirection());
} }
if (!sortQueries.isEmpty()) { if (!sortQueries.isEmpty()) {
template.query("sort", sortQueries); template.query(sortParameter, sortQueries);
} }
} }

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

@ -63,13 +63,18 @@ public class PageableEncoderTests {
encoder.encode(createPageAndSortRequest(), null, request); encoder.encode(createPageAndSortRequest(), null, request);
// Request queries shall contain three entries // Request queries shall contain three entries
assertThat(request.queries().size()).isEqualTo(3); assertThat(request.queries()).hasSize(3);
// Request page shall contain page // Request page shall contain page
assertThat(request.queries().get("page")).contains(String.valueOf(PAGE)); assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
// Request size shall contain size // Request size shall contain size
assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); // Request
// Request sort size shall contain sort entries // sort
assertThat(request.queries().get("sort").size()).isEqualTo(2); // size
// shall
// contain
// sort
// entries
assertThat(request.queries().get("sort")).hasSize(2);
} }
private Pageable createPageAndSortRequest() { private Pageable createPageAndSortRequest() {
@ -86,9 +91,14 @@ public class PageableEncoderTests {
// Request page shall contain page // Request page shall contain page
assertThat(request.queries().get("page")).contains(String.valueOf(PAGE)); assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
// Request size shall contain size // Request size shall contain size
assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); assertThat(request.queries().get("size")).contains(String.valueOf(SIZE)); // Request
// Request sort size shall contain sort entries // sort
assertThat(request.queries().containsKey("sort")).isEqualTo(false); // size
// shall
// contain
// sort
// entries
assertThat(request.queries()).doesNotContainKey("sort");
} }
private Pageable createPageAndRequest() { private Pageable createPageAndRequest() {
@ -105,11 +115,22 @@ public class PageableEncoderTests {
// Request queries shall contain three entries // Request queries shall contain three entries
assertThat(request.queries().size()).isEqualTo(1); assertThat(request.queries().size()).isEqualTo(1);
// Request sort size shall contain sort entries // Request sort size shall contain sort entries
assertThat(request.queries().get("sort").size()).isEqualTo(2); assertThat(request.queries().get("sort")).hasSize(2);
} }
private Sort createSort() { private Sort createSort() {
return Sort.by(SORT_1, SORT_2).ascending(); return Sort.by(SORT_1, SORT_2).ascending();
} }
@Test
public void testUnpagedRequest() {
Encoder encoder = this.context.getInstance("foo", Encoder.class);
assertThat(encoder).isNotNull();
RequestTemplate request = new RequestTemplate();
encoder.encode(Pageable.unpaged(), null, request);
// Request queries shall contain three entries
assertThat(request.queries()).isEmpty();
}
} }

Loading…
Cancel
Save