Skip to content

Commit 37d4441

Browse files
stromnetAdrian Cole
authored andcommitted
Fix variable expansion in @Header class-annotation as discussed in OpenFeign#262
1 parent 7e2e090 commit 37d4441

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

‎core/src/main/java/feign/Contract.java‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me
8282
data.returnType(Types.resolve(targetType, targetType, method.getGenericReturnType()));
8383
data.configKey(Feign.configKey(targetType, method));
8484

85+
processAnnotationOnClass(data, method.getDeclaringClass());
86+
if (method.getDeclaringClass() != targetType) {
87+
processAnnotationOnClass(data, targetType);
88+
}
89+
8590
for (Annotation methodAnnotation : method.getAnnotations()) {
8691
processAnnotationOnMethod(data, methodAnnotation, method);
8792
}
@@ -110,6 +115,15 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me
110115
return data;
111116
}
112117

118+
/**
119+
* Called by parseAndValidateMetadata twice, first on the declaring class, then on the
120+
* target type (unless they are the same).
121+
*
122+
* @param data metadata collected so far relating to the current java method.
123+
* @param clz the class to process
124+
*/
125+
protected abstract void processAnnotationOnClass(MethodMetadata data, Class<?> clz);
126+
113127
/**
114128
* @param data metadata collected so far relating to the current java method.
115129
* @param annotation annotations present on the current method annotation.
@@ -152,18 +166,8 @@ protected void nameParam(MethodMetadata data, String name, int i) {
152166
}
153167

154168
class Default extends BaseContract {
155-
156169
@Override
157-
protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
158-
MethodMetadata data = super.parseAndValidateMetadata(targetType, method);
159-
headersFromAnnotation(method.getDeclaringClass(), data);
160-
if (method.getDeclaringClass() != targetType) {
161-
headersFromAnnotation(targetType, data);
162-
}
163-
return data;
164-
}
165-
166-
private void headersFromAnnotation(Class<?> targetType, MethodMetadata data) {
170+
protected void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) {
167171
if (targetType.isAnnotationPresent(Headers.class)) {
168172
String[] headersOnType = targetType.getAnnotation(Headers.class).value();
169173
checkState(headersOnType.length > 0, "Headers annotation was empty on type %s.",
@@ -196,9 +200,9 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
196200
data.template().append(
197201
requestLine.substring(requestLine.indexOf(' ') + 1, requestLine.lastIndexOf(' ')));
198202
}
199-
203+
200204
data.template().decodeSlash(RequestLine.class.cast(methodAnnotation).decodeSlash());
201-
205+
202206
} else if (annotationType == Body.class) {
203207
String body = Body.class.cast(methodAnnotation).value();
204208
checkState(emptyToNull(body) != null, "Body annotation was empty on method %s.",

‎core/src/test/java/feign/DefaultContractTest.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void queryParamsInPathExtract() throws Exception {
128128
.hasQueries(
129129
entry("flag", asList(new String[]{null})),
130130
entry("NoErrors", asList(new String[]{null}))
131-
);
131+
);
132132
}
133133

134134
@Test
@@ -504,6 +504,30 @@ public void parameterizedBaseApi() throws Exception {
504504
);
505505
}
506506

507+
@Headers("Authorization: {authHdr}")
508+
interface ParameterizedHeaderExpandApi {
509+
@RequestLine("GET /api/{zoneId}")
510+
@Headers("Accept: application/json")
511+
String getZone(@Param("zoneId") String vhost, @Param("authHdr") String authHdr);
512+
}
513+
514+
@Test
515+
public void parameterizedHeaderExpandApi() throws Exception {
516+
List<MethodMetadata> md = contract.parseAndValidatateMetadata(ParameterizedHeaderExpandApi.class);
517+
518+
assertThat(md).hasSize(1);
519+
520+
assertThat(md.get(0).configKey())
521+
.isEqualTo("ParameterizedHeaderExpandApi#getZone(String,String)");
522+
assertThat(md.get(0).returnType())
523+
.isEqualTo(String.class);
524+
assertThat(md.get(0).template())
525+
.hasHeaders(entry("Authorization", asList("{authHdr}")), entry("Accept", asList("application/json")));
526+
// Ensure that the authHdr expansion was properly detected and did not create a formParam
527+
assertThat(md.get(0).formParams())
528+
.isEmpty();
529+
}
530+
507531
private MethodMetadata parseAndValidateMetadata(Class<?> targetType, String method,
508532
Class<?>... parameterTypes)
509533
throws NoSuchMethodException {

‎jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,28 @@ public final class JAXRSContract extends Contract.BaseContract {
4343
static final String ACCEPT = "Accept";
4444
static final String CONTENT_TYPE = "Content-Type";
4545

46+
// Protected so unittest can call us
47+
// XXX: Should parseAndValidateMetadata(Class, Method) be public instead? The old deprecated parseAndValidateMetadata(Method) was public..
4648
@Override
4749
protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
48-
MethodMetadata md = super.parseAndValidateMetadata(targetType, method);
49-
Path path = targetType.getAnnotation(Path.class);
50+
return super.parseAndValidateMetadata(targetType, method);
51+
}
52+
53+
@Override
54+
protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
55+
Path path = clz.getAnnotation(Path.class);
5056
if (path != null) {
5157
String pathValue = emptyToNull(path.value());
52-
checkState(pathValue != null, "Path.value() was empty on type %s", targetType.getName());
58+
checkState(pathValue != null, "Path.value() was empty on type %s", clz.getName());
5359
if (!pathValue.startsWith("/")) {
5460
pathValue = "/" + pathValue;
5561
}
5662
if (pathValue.endsWith("/")) {
5763
// Strip off any trailing slashes, since the template has already had slashes appropriately added
5864
pathValue = pathValue.substring(0, pathValue.length()-1);
5965
}
60-
md.template().insert(0, pathValue);
66+
data.template().insert(0, pathValue);
6167
}
62-
return md;
6368
}
6469

6570
@Override

0 commit comments

Comments
 (0)