Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java-JSON Decoder can decode (return) String too #1989

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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