From 52c7c33519add757d3520cc4ba7d91cf79e3b33e Mon Sep 17 00:00:00 2001 From: Alhuda Khan Date: Fri, 26 Jun 2026 19:59:22 +0530 Subject: [PATCH] honour response charset in StringDecoder --- .../main/java/feign/codec/StringDecoder.java | 2 +- .../java/feign/codec/DefaultDecoderTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/feign/codec/StringDecoder.java b/core/src/main/java/feign/codec/StringDecoder.java index 3127508f48..0982110b45 100644 --- a/core/src/main/java/feign/codec/StringDecoder.java +++ b/core/src/main/java/feign/codec/StringDecoder.java @@ -31,7 +31,7 @@ public Object decode(Response response, Type type) throws IOException { return null; } if (String.class.equals(type)) { - return Util.toString(body.asReader(Util.UTF_8)); + return Util.toString(body.asReader(response.charset())); } throw new DecodeException( response.status(), diff --git a/core/src/test/java/feign/codec/DefaultDecoderTest.java b/core/src/test/java/feign/codec/DefaultDecoderTest.java index 23ba1d4d06..a88ff4dd85 100644 --- a/core/src/test/java/feign/codec/DefaultDecoderTest.java +++ b/core/src/test/java/feign/codec/DefaultDecoderTest.java @@ -25,6 +25,7 @@ import feign.Util; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -45,6 +46,24 @@ void decodesToString() throws Exception { assertThat(decodedObject.toString()).isEqualTo("response body"); } + @Test + void decodesToStringHonouringContentTypeCharset() throws Exception { + String content = "él pingüino"; + byte[] body = content.getBytes(StandardCharsets.ISO_8859_1); + Map> headers = new HashMap<>(); + headers.put("Content-Type", Collections.singleton("text/plain; charset=ISO-8859-1")); + Response response = + Response.builder() + .status(200) + .reason("OK") + .headers(headers) + .request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, UTF_8)) + .body(new ByteArrayInputStream(body), body.length) + .build(); + Object decodedObject = decoder.decode(response, String.class); + assertThat(decodedObject).isEqualTo(content); + } + @Test void decodesToByteArray() throws Exception { Response response = knownResponse();