Skip to content

Commit

Permalink
Merge branch 'master' into OpenFeigngh-1284
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Feb 17, 2021
2 parents e7f4689 + ee8d517 commit f7b2504
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apt-test-generator/pom.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2020 The Feign Authors
Copyright 2012-2021 The Feign Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -140,6 +140,7 @@
<!-- used to create docker images -->
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker-maven-plugin.version}</version>
<configuration>
<!-- docker file copied here after maven replaces properties -->
<dockerDirectory>${project.build.directory}/classes/docker/</dockerDirectory>
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/feign/Response.java
Expand Up @@ -175,6 +175,25 @@ public Request request() {
return request;
}

public Charset charset() {

Collection<String> contentTypeHeaders = headers().get("Content-Type");

if (contentTypeHeaders != null) {
for (String contentTypeHeader : contentTypeHeaders) {
String[] contentTypeParmeters = contentTypeHeader.split(";");
if (contentTypeParmeters.length > 1) {
String[] charsetParts = contentTypeParmeters[1].split("=");
if (charsetParts.length == 2 && "charset".equalsIgnoreCase(charsetParts[0].trim())) {
return Charset.forName(charsetParts[1]);
}
}
}
}

return Util.UTF_8;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("HTTP/1.1 ").append(status);
Expand Down
15 changes: 9 additions & 6 deletions jackson/src/main/java/feign/jackson/JacksonDecoder.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -13,15 +13,17 @@
*/
package feign.jackson;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import feign.Response;
import feign.Util;
import feign.codec.Decoder;
Expand All @@ -47,7 +49,7 @@ public JacksonDecoder(ObjectMapper mapper) {
public Object decode(Response response, Type type) throws IOException {
if (response.body() == null)
return null;
Reader reader = response.body().asReader(Util.UTF_8);
Reader reader = response.body().asReader(response.charset());
if (!reader.markSupported()) {
reader = new BufferedReader(reader, 1);
}
Expand All @@ -66,4 +68,5 @@ public Object decode(Response response, Type type) throws IOException {
throw e;
}
}

}
28 changes: 27 additions & 1 deletion jackson/src/test/java/feign/jackson/JacksonCodecTest.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -28,9 +28,12 @@
import org.junit.Test;
import java.io.Closeable;
import java.io.IOException;
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.LinkedList;
Expand Down Expand Up @@ -169,6 +172,29 @@ public void customEncoder() {
+ "} ]");
}

@Test
public void decoderCharset() throws IOException {
Zone zone = new Zone("denominator.io.", "ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÑ");

Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
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();
assertEquals(zone.get("id"),
((Zone) new JacksonDecoder().decode(response, new TypeReference<Zone>() {}.getType()))
.get("id"));
}

@Test
public void decodesIterator() throws Exception {
List<Zone> zones = new LinkedList<Zone>();
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2020 The Feign Authors
Copyright 2012-2021 The Feign Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -99,6 +99,7 @@
<maven-versions-plugin.version>2.7</maven-versions-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<docker-maven-plugin.version>1.2.2</docker-maven-plugin.version>
</properties>
<url>https://github.com/openfeign/feign</url>
<inceptionYear>2012</inceptionYear>
Expand Down

0 comments on commit f7b2504

Please sign in to comment.