Repository: spring-cloud/spring-cloud-openfeign
Issue: Properly handle Pageable.unpaged() in PageableSpringEncoder

Version `2.1.1.RELEASE` introduced support for Spring's `Pageable` (#26). However the associated Encoder (`PageableSpringEncoder`) crashes when faced with `Pageable.unpaged()`, since the latter throws an `UnsupportedOperationException` on `Pageable.getPageNumber(int)`. This will lead to the very unspecific error:

`feign.codec.EncodeException: null`

Possible enhancements:
- Improve error handling in the above case or
- Translate `Pageable.unpaged()` to a query with page number 0 and page size `Integer.MAX_VALUE` since this is close to an "unpaged" result

Changed files:
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableEncoderTests.java
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringEncoder.java

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 ddf631bb..4461f36b 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
@@ -81,8 +81,12 @@ public class PageableSpringEncoder implements Encoder {
 		if (supports(object)) {
 			if (object instanceof Pageable) {
 				Pageable pageable = (Pageable) object;
-				template.query(pageParameter, pageable.getPageNumber() + "");
-				template.query(sizeParameter, pageable.getPageSize() + "");
+
+				if (pageable.isPaged()) {
+					template.query(pageParameter, pageable.getPageNumber() + "");
+					template.query(sizeParameter, pageable.getPageSize() + "");
+				}
+
 				if (pageable.getSort() != null) {
 					applySort(template, pageable.getSort());
 				}
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 c3916507..cf94ec7c 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
@@ -64,13 +64,12 @@ public class PageableEncoderTests {
 		encoder.encode(createPageAndSortRequest(), null, request);
 
 		// Request queries shall contain three entries
-		assertThat(request.queries().size()).isEqualTo(3);
+		assertThat(request.queries()).hasSize(3);
 		// Request page shall contain page
 		assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
 		// Request size shall contain size
-		assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
-		// Request sort size shall contain sort entries
-		assertThat(request.queries().get("sort").size()).isEqualTo(2);
+		assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));	// Request sort size shall contain sort entries
+		assertThat(request.queries().get("sort")).hasSize(2);
 	}
 
 	private Pageable createPageAndSortRequest() {
@@ -87,9 +86,8 @@ public class PageableEncoderTests {
 		// Request page shall contain page
 		assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
 		// Request size shall contain size
-		assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
-		// Request sort size shall contain sort entries
-		assertThat(request.queries().containsKey("sort")).isEqualTo(false);
+		assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));		// Request sort size shall contain sort entries
+		assertThat(request.queries()).doesNotContainKey("sort");
 	}
 
 	private Pageable createPageAndRequest() {
@@ -106,11 +104,22 @@ 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").size()).isEqualTo(2);
+		assertThat(request.queries().get("sort")).hasSize(2);
 	}
 
 	private Sort createSort() {
 		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();
+	}
+
 }