Use case-insensitive ResponseEntity headers. Fixes gh-456. (#472)

This commit is contained in:
Olga Maciaszek-Sharma
2021-01-27 16:52:29 +01:00
committed by GitHub
parent 6b29a1f7df
commit c672d205d7
3 changed files with 30 additions and 7 deletions

View File

@@ -94,7 +94,8 @@ public class FeignAutoConfiguration {
@Bean
@ConditionalOnMissingBean(PageJacksonModule.class)
@ConditionalOnClass(name = "org.springframework.data.domain.Page")
@ConditionalOnProperty(value = "feign.autoconfiguration.jackson.enabled", havingValue = "true")
@ConditionalOnProperty(value = "feign.autoconfiguration.jackson.enabled",
havingValue = "true")
public Module pageJacksonModule() {
return new PageJacksonModule();
}
@@ -102,7 +103,8 @@ public class FeignAutoConfiguration {
@Bean
@ConditionalOnMissingBean(SortJacksonModule.class)
@ConditionalOnClass(name = "org.springframework.data.domain.Sort")
@ConditionalOnProperty(value = "feign.autoconfiguration.jackson.enabled", havingValue = "true")
@ConditionalOnProperty(value = "feign.autoconfiguration.jackson.enabled",
havingValue = "true")
public Module sortModule() {
return new SortJacksonModule();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,16 +26,16 @@ import feign.Response;
import feign.codec.Decoder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* Decoder adds compatibility for Spring MVC's ResponseEntity to any other decoder via
* composition.
*
* @author chad jaros
* @author Olga Maciaszek-Sharma
*/
public class ResponseEntityDecoder implements Decoder {
@@ -81,7 +81,7 @@ public class ResponseEntityDecoder implements Decoder {
@SuppressWarnings("unchecked")
private <T> ResponseEntity<T> createResponse(Object instance, Response response) {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpHeaders headers = new HttpHeaders();
for (String key : response.headers().keySet()) {
headers.put(key, new LinkedList<>(response.headers().get(key)));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,7 +32,9 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.openfeign.test.NoSecurityConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -45,6 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDecoderTests.Application.class,
@@ -152,6 +155,14 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
assertThat(response.getBody()).as("response body was not null").isNull();
}
@Test
// Issue: https://github.com/spring-cloud/spring-cloud-openfeign/issues/456
public void testResponseEntityHeaders() {
ResponseEntity<String> response = testClient().getContentType();
assertThat(response.getHeaders().getContentType())
.isEqualTo(MediaType.APPLICATION_JSON);
}
protected interface TestClient {
@RequestMapping(method = RequestMethod.GET, value = "/helloresponse")
@@ -175,6 +186,9 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
@GetMapping("/helloWildcard")
ResponseEntity<?> getWildcard();
@GetMapping(path = "/contentType", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> getContentType();
}
public static class Hello {
@@ -262,6 +276,13 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
return ResponseEntity.ok(new Hello("wildcard"));
}
@Override
public ResponseEntity<String> getContentType() {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body("test");
}
}
}