-
Notifications
You must be signed in to change notification settings - Fork 808
Open
Labels
Description
Describe the bug
The configuration spring.cloud.openfeign.compression.request.content-encoding-types
is not being considered when compression is applied.
Sample
-
Generate a project at https://start.spring.io including
lombok
andOpenFeign
(direct link here). -
Include a client in
pom.xml
:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
- Include this in
application.properties
:
spring.cloud.openfeign.compression.response.enabled=true
spring.cloud.openfeign.compression.request.content-encoding-types=gzip
spring.cloud.openfeign.client.config.default.request-interceptors=com.example.demo.DemoApplication.MyInterceptor
- Replace the code on
DemoApplication.java
:
@EnableFeignClients
@SpringBootApplication
@RequiredArgsConstructor
public class DemoApplication implements CommandLineRunner {
// Only actual necessary code that matters: the request interceptor
public static class MyInterceptor implements RequestInterceptor {
@Setter
private Optional<FeignClientEncodingProperties> properties;
@Override
public void apply(RequestTemplate template) {
final var encodingAppliedRequest = template.headers().get(HttpEncoding.ACCEPT_ENCODING_HEADER);
final var encodingDesiredRequest = properties.map(e -> List.of(e.getContentEncodingTypes())).orElse(List.of());
System.out.println("Encoding in use: " + encodingAppliedRequest);
System.out.println("Encoding desired: " + encodingDesiredRequest);
}
}
// All other code to make things work
private final GithubClient client;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
client.getIssues();
}
@RequiredArgsConstructor
public static class MyCapability implements Capability {
private final Optional<FeignClientEncodingProperties> properties;
@Override
public RequestInterceptor enrich(RequestInterceptor requestInterceptor) {
if (requestInterceptor instanceof MyInterceptor myInterceptor)
myInterceptor.setProperties(properties);
return requestInterceptor;
}
}
}
@Configuration
class FeignConfiguration {
@Bean
Client client() {
return new ApacheHttpClient();
}
@Bean
Capability feignOAuthTokenCapability(Optional<FeignClientEncodingProperties> properties) {
return new DemoApplication.MyCapability(properties);
}
}
@FeignClient(name = "github", url = "https://github.com/spring-cloud/spring-cloud-openfeign")
interface GithubClient {
@GetMapping("/issues")
String getIssues();
}
Results
The terminal shows:
Encoding in use: [gzip, deflate]
Encoding desired: [gzip]
The expected results should be:
Encoding in use: [gzip]
Encoding desired: [gzip]
Testing
- You can also test replacing
DemoApplicationTests.java
with the code below:
@SpringBootTest
@ExtendWith(MockitoExtension.class)
class DemoApplicationTests {
@Autowired
private FeignClientEncodingProperties properties;
@Autowired
private FeignAcceptGzipEncodingInterceptor interceptor;
@Test
void terstInterceptorContainsEncodingFromProperties() {
final var encodingApplied = new AtomicReference<>(List.<String>of());
final var template = mock(RequestTemplate.class);
when(template.header(eq(HttpEncoding.ACCEPT_ENCODING_HEADER), any(String[].class))).thenAnswer(invocation -> {
encodingApplied.set(List.of((String[]) invocation.getRawArguments()[1]));
return null;
});
interceptor.apply(template);
final var encodingFromProperties = List.of(properties.getContentEncodingTypes());
assertEquals(encodingFromProperties.toString(), encodingApplied.get().toString());
}
}
Versions
- Spring Boot 3.5.5
- Spring Cloud OpenFeign 4.3.0