Add support for feign OptionalDecoder

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