Backport bugfix and resolve conflicts.

This commit is contained in:
Olga MaciaszekSharma
2022-01-17 12:41:20 +01:00
committed by Olga Maciaszek-Sharma
parent d4832788f6
commit 24d9b08d5c
3 changed files with 61 additions and 1 deletions

View File

@@ -443,6 +443,9 @@ public class FeignClientFactoryBean
}
private String cleanPath() {
if (path == null) {
return "";
}
String path = this.path.trim();
if (StringUtils.hasLength(path)) {
if (!path.startsWith("/")) {

View File

@@ -302,7 +302,11 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLo
if (resolver == null) {
return resolved;
}
return String.valueOf(resolver.evaluate(resolved, new BeanExpressionContext(beanFactory, null)));
Object evaluateValue = resolver.evaluate(resolved, new BeanExpressionContext(beanFactory, null));
if (evaluateValue != null) {
return String.valueOf(evaluateValue);
}
return null;
}
return value;
}

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.openfeign;
import java.util.Collections;
import feign.Target;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -25,15 +26,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* @author Spencer Gibb
* @author Gang Li
* @author Michal Domagala
* @author Szymon Linowski
* @author Olga Maciaszek-Sharma
*/
public class FeignClientsRegistrarTests {
@@ -101,6 +106,30 @@ public class FeignClientsRegistrarTests {
.doesNotThrowAnyException();
}
@Test
public void shouldResolveNullUrl() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(NullUrlFeignClientTestConfig.class);
context.refresh();
Object feignClientBean = context.getBean(NullUrlFeignClient.class);
Object invocationHandlerLambda = ReflectionTestUtils.getField(feignClientBean, "h");
Target.HardCodedTarget<NullUrlFeignClient> target = (Target.HardCodedTarget<NullUrlFeignClient>) ReflectionTestUtils
.getField(invocationHandlerLambda, "arg$4");
assertThat(target.name()).isEqualTo("nullUrlFeignClient");
assertThat(target.url()).isEqualTo("http://nullUrlFeignClient");
}
@Test
public void shouldResolveAndValidateNullName() {
assertThatIllegalStateException().isThrownBy(() -> {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(NullExpressionNameFeignClientTestConfig.class);
context.refresh();
});
}
@FeignClient(name = "fallbackTestClient", url = "http://localhost:8080/", fallback = FallbackClient.class)
protected interface FallbackClient {
@@ -118,6 +147,16 @@ public class FeignClientsRegistrarTests {
}
@FeignClient(name = "nullUrlFeignClient", url = "${test.url:#{null}}", path = "${test.path:#{null}}")
protected interface NullUrlFeignClient {
}
@FeignClient(name = "${test.name:#{null}}")
protected interface NullExpressionNameFeignClient {
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableFeignClients(clients = { FeignClientsRegistrarTests.FallbackClient.class })
@@ -138,4 +177,18 @@ public class FeignClientsRegistrarTests {
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableFeignClients(clients = NullUrlFeignClient.class)
protected static class NullUrlFeignClientTestConfig {
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableFeignClients(clients = NullExpressionNameFeignClient.class)
protected static class NullExpressionNameFeignClientTestConfig {
}
}