From 28e4a6b0f5fa9ff7ea001e4895a1a9445fdbadb9 Mon Sep 17 00:00:00 2001 From: Witalij Berdinskich Date: Thu, 23 Mar 2023 03:23:56 +0200 Subject: [PATCH] JSON Decoder can decode String too (#1989) --- .../src/main/java/feign/json/JsonDecoder.java | 7 +++-- .../test/java/feign/json/JsonDecoderTest.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/json/src/main/java/feign/json/JsonDecoder.java b/json/src/main/java/feign/json/JsonDecoder.java index 7b0799a1f..53f90f531 100644 --- a/json/src/main/java/feign/json/JsonDecoder.java +++ b/json/src/main/java/feign/json/JsonDecoder.java @@ -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; @@ -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(), @@ -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(); @@ -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); diff --git a/json/src/test/java/feign/json/JsonDecoderTest.java b/json/src/test/java/feign/json/JsonDecoderTest.java index 22328bde9..693c9811e 100644 --- a/json/src/test/java/feign/json/JsonDecoderTest.java +++ b/json/src/test/java/feign/json/JsonDecoderTest.java @@ -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; @@ -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 = @@ -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 =