Actually fix the feign client validation issue

We need to still assert that the FeignClient is valid (no name and
value specified together for instance). Added missing tests.
This commit is contained in:
Dave Syer
2016-07-21 10:36:18 +01:00
parent 85ef54a9a1
commit afb43c5883
2 changed files with 49 additions and 7 deletions
@@ -155,8 +155,6 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
Map<String, Object> attributes = annotationMetadata Map<String, Object> attributes = annotationMetadata
.getAnnotationAttributes( .getAnnotationAttributes(
FeignClient.class.getCanonicalName()); FeignClient.class.getCanonicalName());
// Spring 4.2 didn't do this for us. With 4.3 it's idempotent.
attributes = AnnotationAttributes.fromMap(attributes);
String name = getClientName(attributes); String name = getClientName(attributes);
registerClientConfiguration(registry, name, registerClientConfiguration(registry, name,
@@ -192,10 +190,9 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
} }
private void validate(Map<String, Object> attributes) { private void validate(Map<String, Object> attributes) {
if (StringUtils.hasText((String) attributes.get("value"))) { AnnotationAttributes annotation = AnnotationAttributes.fromMap(attributes);
Assert.isTrue(!StringUtils.hasText((String) attributes.get("serviceId")), // This blows up if an aliased property is overspecified
"Either name (serviceId) or value can be specified, but not both"); annotation.getAliasedString("name", FeignClient.class, null);
}
} }
private String getName(Map<String, Object> attributes) { private String getName(Map<String, Object> attributes) {
@@ -39,6 +39,50 @@ public class FeignClientValidationTests {
@Rule @Rule
public ExpectedException expected = ExpectedException.none(); public ExpectedException expected = ExpectedException.none();
@Test
public void testNameAndValue() {
this.expected.expectMessage("only one is permitted");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
NameAndValueConfiguration.class);
assertNotNull(context.getBean(NameAndValueConfiguration.Client.class));
context.close();
}
@Configuration
@Import(FeignAutoConfiguration.class)
@EnableFeignClients(clients = NameAndValueConfiguration.Client.class)
protected static class NameAndValueConfiguration {
@FeignClient(value = "foo", name = "bar")
interface Client {
@RequestMapping(method = RequestMethod.GET, value = "/")
String get();
}
}
@Test
public void testServiceIdAndValue() {
this.expected.expectMessage("only one is permitted");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
NameAndValueConfiguration.class);
assertNotNull(context.getBean(NameAndServiceIdConfiguration.Client.class));
context.close();
}
@Configuration
@Import(FeignAutoConfiguration.class)
@EnableFeignClients(clients = NameAndServiceIdConfiguration.Client.class)
protected static class NameAndServiceIdConfiguration {
@FeignClient(serviceId = "foo", name = "bar")
interface Client {
@RequestMapping(method = RequestMethod.GET, value = "/")
String get();
}
}
@Test @Test
public void testNotLegalHostname() { public void testNotLegalHostname() {
this.expected.expectMessage("not legal hostname (foo_bar)"); this.expected.expectMessage("not legal hostname (foo_bar)");
@@ -115,7 +159,8 @@ public class FeignClientValidationTests {
return new Dummy(); return new Dummy();
} }
class Dummy { } class Dummy {
}
} }
} }