Skip to content

Commit

Permalink
JSON Decoder can decode String too (OpenFeign#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 committed Mar 23, 2023
1 parent 8a8bbb9 commit 8ad08f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions json/src/main/java/feign/json/JsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package feign.json;

import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.json.JSONArray;
Expand Down Expand Up @@ -57,6 +58,8 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
return new JSONObject();
else if (JSONArray.class.isAssignableFrom((Class<?>) type))
return new JSONArray();
else if (String.class.equals(type))
return null;
else
throw new DecodeException(response.status(),
format("%s is not a type supported by this decoder.", type), response.request());
Expand All @@ -69,7 +72,7 @@ else if (JSONArray.class.isAssignableFrom((Class<?>) type))
return null; // Empty body
}
bodyReader.reset();
return decodeJSON(response, type, bodyReader);
return decodeBody(response, type, bodyReader);
} catch (JSONException jsonException) {
if (jsonException.getCause() != null && jsonException.getCause() instanceof IOException) {
throw (IOException) jsonException.getCause();
Expand All @@ -79,7 +82,9 @@ else if (JSONArray.class.isAssignableFrom((Class<?>) type))
}
}

private Object decodeJSON(Response response, Type type, Reader reader) {
private Object decodeBody(Response response, Type type, Reader reader) throws IOException {
if (String.class.equals(type))
return Util.toString(reader);
JSONTokener tokenizer = new JSONTokener(reader);
if (JSONObject.class.isAssignableFrom((Class<?>) type))
return new JSONObject(tokenizer);
Expand Down
24 changes: 24 additions & 0 deletions json/src/test/java/feign/json/JsonDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public void decodesObject() throws IOException {
assertTrue(jsonObject.similar(new JsonDecoder().decode(response, JSONObject.class)));
}

@Test
public void decodesString() throws IOException {
String json = "qwerty";
Response response = Response.builder()
.status(200)
.reason("OK")
.headers(Collections.emptyMap())
.body(json, UTF_8)
.request(request)
.build();
assertEquals("qwerty", new JsonDecoder().decode(response, String.class));
}

@Test
public void notFoundDecodesToEmpty() throws IOException {
Response response = Response.builder()
Expand All @@ -100,6 +113,17 @@ public void nullBodyDecodesToEmpty() throws IOException {
assertTrue(((JSONObject) new JsonDecoder().decode(response, JSONObject.class)).isEmpty());
}

@Test
public void nullBodyDecodesToNullString() throws IOException {
Response response = Response.builder()
.status(204)
.reason("OK")
.headers(Collections.emptyMap())
.request(request)
.build();
assertNull(new JsonDecoder().decode(response, String.class));
}

@Test
public void emptyBodyDecodesToEmpty() throws IOException {
Response response = Response.builder()
Expand Down

0 comments on commit 8ad08f8

Please sign in to comment.