Skip to content

Commit

Permalink
JSON Decoder can decode String too (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 authored Mar 23, 2023
1 parent 4f8d513 commit 28e4a6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions json/src/main/java/feign/json/JsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static java.lang.String.format;

import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import java.io.BufferedReader;
Expand Down Expand Up @@ -56,6 +57,7 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
if (response.status() == 404 || response.status() == 204)
if (JSONObject.class.isAssignableFrom((Class<?>) type)) 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(),
Expand All @@ -69,7 +71,7 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
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 +81,8 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
}
}

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);
else if (JSONArray.class.isAssignableFrom((Class<?>) type)) return new JSONArray(tokenizer);
Expand Down
27 changes: 27 additions & 0 deletions json/src/test/java/feign/json/JsonDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static feign.Util.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -86,6 +87,20 @@ 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 =
Expand All @@ -110,6 +125,18 @@ 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 =
Expand Down

0 comments on commit 28e4a6b

Please sign in to comment.