Skip to content

Commit

Permalink
Use "=" as separator in @RequestMapping(headers=...)
Browse files Browse the repository at this point in the history
Consistent with Spring MVC (and untested up to now apparently).

Fixes gh-874
  • Loading branch information
Dave Syer committed Mar 4, 2016
1 parent 718cafc commit 000fee9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Expand Up @@ -36,14 +36,14 @@
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;


import feign.Contract;
import feign.Feign;
import feign.MethodMetadata;

import static feign.Util.checkState; import static feign.Util.checkState;
import static feign.Util.emptyToNull; import static feign.Util.emptyToNull;
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;


import feign.Contract;
import feign.Feign;
import feign.MethodMetadata;

/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
Expand Down Expand Up @@ -79,10 +79,11 @@ public SpringMvcContract(


@Override @Override
public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) { public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
processedMethods.put(Feign.configKey(targetType, method), method); this.processedMethods.put(Feign.configKey(targetType, method), method);
MethodMetadata md = super.parseAndValidateMetadata(targetType, method); MethodMetadata md = super.parseAndValidateMetadata(targetType, method);


RequestMapping classAnnotation = findMergedAnnotation(targetType, RequestMapping.class); RequestMapping classAnnotation = findMergedAnnotation(targetType,
RequestMapping.class);
if (classAnnotation != null) { if (classAnnotation != null) {
// Prepend path from class annotation if specified // Prepend path from class annotation if specified
if (classAnnotation.value().length > 0) { if (classAnnotation.value().length > 0) {
Expand Down Expand Up @@ -169,7 +170,7 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data,


AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext( AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext(
data, paramIndex); data, paramIndex);
Method method = processedMethods.get(data.configKey()); Method method = this.processedMethods.get(data.configKey());
for (Annotation parameterAnnotation : annotations) { for (Annotation parameterAnnotation : annotations) {
AnnotatedParameterProcessor processor = this.annotatedArgumentProcessors AnnotatedParameterProcessor processor = this.annotatedArgumentProcessors
.get(parameterAnnotation.annotationType()); .get(parameterAnnotation.annotationType());
Expand Down Expand Up @@ -213,9 +214,9 @@ private void parseHeaders(MethodMetadata md, Method method,
// TODO: only supports one header value per key // TODO: only supports one header value per key
if (annotation.headers() != null && annotation.headers().length > 0) { if (annotation.headers() != null && annotation.headers().length > 0) {
for (String header : annotation.headers()) { for (String header : annotation.headers()) {
int colon = header.indexOf(':'); int index = header.indexOf('=');
md.template().header(header.substring(0, colon), md.template().header(header.substring(0, index),
header.substring(colon + 2)); header.substring(index + 1).trim());
} }
} }
} }
Expand Down
Expand Up @@ -182,6 +182,18 @@ public void testProcessAnnotations_Advanced3() throws Exception {
data.template().headers().get("Accept").iterator().next()); data.template().headers().get("Accept").iterator().next());
} }


@Test
public void testProcessHeaders() throws Exception {
Method method = TestTemplate_Headers.class.getDeclaredMethod("getTest",
String.class);
MethodMetadata data = this.contract
.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertEquals("/test/{id}", data.template().url());
assertEquals("GET", data.template().method());
assertEquals("bar", data.template().headers().get("X-Foo").iterator().next());
}

@Test @Test
public void testProcessAnnotations_Fallback() throws Exception { public void testProcessAnnotations_Fallback() throws Exception {
Method method = TestTemplate_Advanced.class.getDeclaredMethod("getTestFallback", Method method = TestTemplate_Advanced.class.getDeclaredMethod("getTestFallback",
Expand Down Expand Up @@ -219,10 +231,12 @@ private static boolean hasJava8ParameterNames(Method m) {
org.springframework.util.Assert.isTrue(m.getParameterTypes().length > 0, org.springframework.util.Assert.isTrue(m.getParameterTypes().length > 0,
"method has no parameters"); "method has no parameters");
if (EXECUTABLE_TYPE != null) { if (EXECUTABLE_TYPE != null) {
Method getParameters = ReflectionUtils.findMethod(EXECUTABLE_TYPE, "getParameters"); Method getParameters = ReflectionUtils.findMethod(EXECUTABLE_TYPE,
"getParameters");
try { try {
Object[] parameters = (Object[]) getParameters.invoke(m); Object[] parameters = (Object[]) getParameters.invoke(m);
Method isNamePresent = ReflectionUtils.findMethod(parameters[0].getClass(), "isNamePresent"); Method isNamePresent = ReflectionUtils
.findMethod(parameters[0].getClass(), "isNamePresent");
return Boolean.TRUE.equals(isNamePresent.invoke(parameters[0])); return Boolean.TRUE.equals(isNamePresent.invoke(parameters[0]));
} }
catch (IllegalAccessException | IllegalArgumentException catch (IllegalAccessException | IllegalArgumentException
Expand All @@ -243,6 +257,11 @@ public interface TestTemplate_Simple {
TestObject postTest(@RequestBody TestObject object); TestObject postTest(@RequestBody TestObject object);
} }


public interface TestTemplate_Headers {
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET, headers = "X-Foo=bar")
ResponseEntity<TestObject> getTest(@PathVariable("id") String id);
}

@JsonAutoDetect @JsonAutoDetect
@RequestMapping("/advanced") @RequestMapping("/advanced")
public interface TestTemplate_Advanced { public interface TestTemplate_Advanced {
Expand All @@ -253,7 +272,8 @@ ResponseEntity<TestObject> getTest(@RequestHeader("Authorization") String auth,
@PathVariable("id") String id, @RequestParam("amount") Integer amount); @PathVariable("id") String id, @RequestParam("amount") Integer amount);


@RequestMapping(path = "/test2", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(path = "/test2", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<TestObject> getTest2(@RequestHeader(name = "Authorization") String auth, ResponseEntity<TestObject> getTest2(
@RequestHeader(name = "Authorization") String auth,
@RequestParam(name = "amount") Integer amount); @RequestParam(name = "amount") Integer amount);


@ExceptionHandler @ExceptionHandler
Expand Down

0 comments on commit 000fee9

Please sign in to comment.