Add support for feign OptionalDecoder

fixes gh-1982
parent 25337dd4
......@@ -115,6 +115,11 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<optional>true</optional>
......
......@@ -46,6 +46,7 @@ import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;
/**
* @author Dave Syer
......@@ -69,7 +70,7 @@ public class FeignClientsConfiguration {
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
}
@Bean
......
......@@ -23,7 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Configuration;
......@@ -36,6 +35,7 @@ import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.optionals.OptionalDecoder;
import feign.slf4j.Slf4jLogger;
/**
......@@ -51,7 +51,7 @@ public class EnableFeignClientsTests {
@Test
public void decoderDefaultCorrect() {
ResponseEntityDecoder.class
OptionalDecoder.class
.cast(this.feignContext.getInstance("foo", Decoder.class));
}
......
......@@ -17,17 +17,12 @@
package org.springframework.cloud.netflix.feign;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
......@@ -38,6 +33,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import feign.Contract;
import feign.Feign;
import feign.Logger;
......@@ -51,6 +50,7 @@ import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;
import feign.slf4j.Slf4jLogger;
/**
......@@ -79,7 +79,7 @@ public class FeignClientOverrideDefaultsTests {
@Test
public void overrideDecoder() {
Decoder.Default.class.cast(this.context.getInstance("foo", Decoder.class));
ResponseEntityDecoder.class.cast(this.context.getInstance("bar", Decoder.class));
OptionalDecoder.class.cast(this.context.getInstance("bar", Decoder.class));
}
@Test
......
......@@ -16,14 +16,6 @@
package org.springframework.cloud.netflix.feign.valid;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
......@@ -34,6 +26,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
......@@ -76,6 +69,15 @@ import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import feign.Client;
import feign.Feign;
import feign.Logger;
......@@ -161,6 +163,9 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, path = "/hello")
Hello getHello();
@RequestMapping(method = RequestMethod.GET, path = "/hello")
Optional<Hello> getOptionalHello();
@RequestMapping(method = RequestMethod.GET, path = "${feignClient.methodLevelRequestMappingPath}")
Hello getHelloUsingPropertyPlaceHolder();
......@@ -240,6 +245,9 @@ public class FeignClientTests {
protected interface DecodingTestClient {
@RequestMapping(method = RequestMethod.GET, path = "/notFound")
ResponseEntity<String> notFound();
@RequestMapping(method = RequestMethod.GET, path = "/notFound")
Optional<String> optional();
}
@FeignClient(name = "localapp3", fallback = HystrixClientFallback.class)
......@@ -519,6 +527,15 @@ public class FeignClientTests {
}
@Test
public void testOptional() {
Optional<Hello> hello = this.testClient.getOptionalHello();
assertThat(hello)
.isNotNull()
.isPresent()
.contains(new Hello(HELLO_WORLD_1));
}
@Test
public void testGenericType() {
List<Hello> hellos = this.testClient.getHellos();
assertNotNull("hellos was null", hellos);
......@@ -633,6 +650,12 @@ public class FeignClientTests {
}
@Test
public void testOptionalNotFound() {
Optional<String> s = decodingTestClient.optional();
assertThat(s).isNotPresent();
}
@Test
public void testConvertingExpander() {
assertEquals(Arg.A.toString(), testClient.getToString(Arg.A));
assertEquals(Arg.B.toString(), testClient.getToString(Arg.B));
......
......@@ -16,7 +16,7 @@
<properties>
<archaius.version>0.7.5</archaius.version>
<eureka.version>1.8.2</eureka.version>
<feign.version>9.5.0</feign.version>
<feign.version>9.5.1</feign.version>
<hystrix.version>1.5.12</hystrix.version>
<ribbon.version>2.2.2</ribbon.version>
<servo.version>0.10.1</servo.version>
......@@ -283,6 +283,11 @@
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>${feign.version}</version>
</dependency>
......
......@@ -47,6 +47,10 @@
<artifactId>feign-hystrix</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment