Skip to content
Open
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
3 changes: 1 addition & 2 deletions gson/src/main/java/feign/gson/GsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package feign.gson;

import static feign.Util.UTF_8;
import static feign.Util.ensureClosed;

import com.google.gson.Gson;
Expand Down Expand Up @@ -50,7 +49,7 @@ public GsonDecoder(Gson gson) {
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader(UTF_8);
Reader reader = response.body().asReader(response.charset());
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
Expand Down
24 changes: 24 additions & 0 deletions gson/src/test/java/feign/gson/GsonCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import feign.Response;
import feign.Util;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -76,6 +78,28 @@ void decodesMapObjectNumericalValuesAsInteger() throws Exception {
new GsonDecoder().decode(response, new TypeToken<Map<String, Object>>() {}.getType()));
}

@Test
void decodesUsingResponseCharset() throws Exception {
Map<String, Object> expected = new LinkedHashMap<>();
expected.put("name", "ÁÉÍÓÚ");

Map<String, Collection<String>> headers = new LinkedHashMap<>();
headers.put("Content-Type", Arrays.asList("application/json;charset=ISO-8859-1"));

Response response =
Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(headers)
.body("{\"name\":\"ÁÉÍÓÚ\"}".getBytes(StandardCharsets.ISO_8859_1))
.build();
assertThat(expected)
.isEqualTo(
new GsonDecoder().decode(response, new TypeToken<Map<String, Object>>() {}.getType()));
}

@Test
void encodesFormParams() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package feign.jackson;

import static feign.Util.UTF_8;
import static feign.Util.ensureClosed;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -72,7 +71,7 @@ public final class JacksonIteratorDecoder implements Decoder {
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader(UTF_8);
Reader reader = response.body().asReader(response.charset());
if (!reader.markSupported()) {
reader = new BufferedReader(reader, 1);
}
Expand Down
32 changes: 32 additions & 0 deletions jackson/src/test/java/feign/jackson/JacksonIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Request;
import feign.Request.HttpMethod;
Expand All @@ -31,8 +32,16 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -163,6 +172,29 @@ public void close() throws IOException {
assertThat(closed.get()).isTrue();
}

@Test
void decodeUsesResponseCharset() throws IOException {
Map<String, Collection<String>> headers = new HashMap<>();
headers.put("Content-Type", Arrays.asList("application/json;charset=ISO-8859-1"));

Response response =
Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(headers)
.body("[{\"login\":\"ÁÉÍÓÚ\"}]".getBytes(StandardCharsets.ISO_8859_1))
.build();
Object decoded =
JacksonIteratorDecoder.create()
.decode(response, new TypeReference<Iterator<User>>() {}.getType());
List<Object> users = new ArrayList<>();
((Iterator<?>) decoded).forEachRemaining(users::add);
assertThat(users).hasSize(1);
assertThat((User) users.get(0)).containsEntry("login", "ÁÉÍÓÚ");
}

static class User extends LinkedHashMap<String, Object> {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package feign.jackson3;

import static feign.Util.UTF_8;
import static feign.Util.ensureClosed;

import feign.Response;
Expand Down Expand Up @@ -75,7 +74,7 @@ public final class Jackson3IteratorDecoder implements Decoder {
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader(UTF_8);
Reader reader = response.body().asReader(response.charset());
if (!reader.markSupported()) {
reader = new BufferedReader(reader, 1);
}
Expand Down
32 changes: 32 additions & 0 deletions jackson3/src/test/java/feign/jackson3/Jackson3CodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,38 @@ void decodesIterator() throws Exception {
assertThat(asList((Iterator<?>) decoded)).isEqualTo(zones);
}

@Test
void decodesIteratorUsingResponseCharset() throws Exception {
Map<String, Collection<String>> headers = new HashMap<>();
headers.put("Content-Type", Arrays.asList("application/json;charset=ISO-8859-1"));

Response response =
Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(headers)
.body(
new String(
"" //
+ "[ {"
+ System.lineSeparator()
+ " \"name\" : \"denominator.io.\","
+ System.lineSeparator()
+ " \"id\" : \"ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÑ\""
+ System.lineSeparator()
+ "} ]")
.getBytes(StandardCharsets.ISO_8859_1))
.build();
Object decoded =
Jackson3IteratorDecoder.create()
.decode(response, new TypeReference<Iterator<Zone>>() {}.getType());
List<?> zones = asList((Iterator<?>) decoded);
assertThat(zones).hasSize(1);
assertThat((Zone) zones.get(0)).containsEntry("id", "ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÑ");
}

private <T> List<T> asList(Iterator<T> iter) {
final List<T> copy = new ArrayList<>();
while (iter.hasNext()) {
Expand Down