Skip to content

Commit 924b9de

Browse files
committed
Class-level headers from inherited class when method is defined in declaring class
Relates to OpenFeign#266, adds support to have @Header on the parent interface too
1 parent 11bdf20 commit 924b9de

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +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);
85+
if(targetType.getInterfaces().length == 1) {
86+
processAnnotationOnClass(data, targetType.getInterfaces()[0]);
8887
}
88+
processAnnotationOnClass(data, targetType);
89+
8990

9091
for (Annotation methodAnnotation : method.getAnnotations()) {
9192
processAnnotationOnMethod(data, methodAnnotation, method);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,50 @@ public void parameterizedHeaderExpandApi() throws Exception {
528528
.isEmpty();
529529
}
530530

531+
@Headers("Authorization: {authHdr}")
532+
interface ParameterizedHeaderBase {
533+
}
534+
535+
interface ParameterizedHeaderExpandInheritedApi extends ParameterizedHeaderBase {
536+
@RequestLine("GET /api/{zoneId}")
537+
@Headers("Accept: application/json")
538+
String getZoneAccept(@Param("zoneId") String vhost, @Param("authHdr") String authHdr);
539+
540+
@RequestLine("GET /api/{zoneId}")
541+
String getZone(@Param("zoneId") String vhost, @Param("authHdr") String authHdr);
542+
}
543+
544+
@Test
545+
public void parameterizedHeaderExpandApiBaseClass() throws Exception {
546+
List<MethodMetadata> mds = contract.parseAndValidatateMetadata(ParameterizedHeaderExpandInheritedApi.class);
547+
548+
Map<String, MethodMetadata> byConfigKey = new LinkedHashMap<String, MethodMetadata>();
549+
for (MethodMetadata m : mds) {
550+
byConfigKey.put(m.configKey(), m);
551+
}
552+
553+
assertThat(byConfigKey)
554+
.containsOnlyKeys("ParameterizedHeaderExpandInheritedApi#getZoneAccept(String,String)",
555+
"ParameterizedHeaderExpandInheritedApi#getZone(String,String)");
556+
557+
MethodMetadata md = byConfigKey.get("ParameterizedHeaderExpandInheritedApi#getZoneAccept(String,String)");
558+
assertThat(md.returnType())
559+
.isEqualTo(String.class);
560+
assertThat(md.template())
561+
.hasHeaders(entry("Authorization", asList("{authHdr}")), entry("Accept", asList("application/json")));
562+
// Ensure that the authHdr expansion was properly detected and did not create a formParam
563+
assertThat(md.formParams())
564+
.isEmpty();
565+
566+
md = byConfigKey.get("ParameterizedHeaderExpandInheritedApi#getZone(String,String)");
567+
assertThat(md.returnType())
568+
.isEqualTo(String.class);
569+
assertThat(md.template())
570+
.hasHeaders(entry("Authorization", asList("{authHdr}")));
571+
assertThat(md.formParams())
572+
.isEmpty();
573+
}
574+
531575
private MethodMetadata parseAndValidateMetadata(Class<?> targetType, String method,
532576
Class<?>... parameterTypes)
533577
throws NoSuchMethodException {

0 commit comments

Comments
 (0)