Skip to content

Commit a458372

Browse files
committed
Simplify Decoder.Default by extending StringDecoder
Previously, the default decoder had logic relating to responses that made it distinct from StringDecoder. Now that that's handled elsewhere, the body of the decode methods had less meaningful differences. I opted to maintain Decoder.Default for consistency with other default implementations. StringDecoder is maintained as a separate class for backwards compatibility, and because it may be useful in the future for clients to use a plain String decoder even if the default decoder starts having additional capabilities.
1 parent 9b31a80 commit a458372

4 files changed

Lines changed: 11 additions & 19 deletions

File tree

core/src/main/java/feign/Util.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ public static Type resolveLastTypeParameter(Type genericContext, Class<?> supert
168168

169169
private static final int BUF_SIZE = 0x800; // 2K chars (4K bytes)
170170

171+
/**
172+
* Adapted from {@code com.google.common.io.CharStreams.toString()}.
173+
*/
171174
public static String toString(Reader reader) throws IOException {
172175
if (reader == null) {
173176
return null;

core/src/main/java/feign/codec/Decoder.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717

1818
import feign.FeignException;
1919
import feign.Response;
20-
import feign.Util;
2120

2221
import java.io.IOException;
2322
import java.lang.reflect.Type;
2423

25-
import static java.lang.String.format;
26-
2724
/**
2825
* Decodes an HTTP response into a single object of the given {@code type}. Invoked when
2926
* {@link Response#status()} is in the 2xx range and the return type is neither {@code void} nor {@code Response}.
@@ -76,17 +73,8 @@ public interface Decoder {
7673
Object decode(Response response, Type type) throws IOException, DecodeException, FeignException;
7774

7875
/**
79-
* Default implementation of {@code Decoder} that supports {@code String} signatures.
76+
* Default implementation of {@code Decoder}.
8077
*/
81-
public class Default implements Decoder {
82-
@Override
83-
public Object decode(Response response, Type type) throws IOException {
84-
if (response.body() == null) {
85-
return null;
86-
} else if (String.class.equals(type)) {
87-
return Util.toString(response.body().asReader());
88-
}
89-
throw new DecodeException(format("%s is not a type supported by this decoder.", type));
90-
}
78+
public class Default extends StringDecoder {
9179
}
9280
}

core/src/main/java/feign/codec/StringDecoder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
import java.io.IOException;
2222
import java.lang.reflect.Type;
2323

24-
/**
25-
* Adapted from {@code com.google.common.io.CharStreams.toString()}.
26-
*/
24+
import static java.lang.String.format;
25+
2726
public class StringDecoder implements Decoder {
2827
@Override
2928
public Object decode(Response response, Type type) throws IOException {
3029
Response.Body body = response.body();
3130
if (body == null) {
3231
return null;
3332
}
34-
return Util.toString(body.asReader());
33+
if (String.class.equals(type)) {
34+
return Util.toString(body.asReader());
35+
}
36+
throw new DecodeException(format("%s is not a type supported by this decoder.", type));
3537
}
3638
}

core/src/test/java/feign/codec/DefaultDecoderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package feign.codec;
1717

1818
import feign.Response;
19-
import feign.Util;
2019
import org.testng.annotations.Test;
2120
import org.w3c.dom.Document;
2221

0 commit comments

Comments
 (0)