Skip to content

Commit 757de66

Browse files
authored
Allow to ignore methods on provided interface (#2218)
* Allow to ignore methods on provided interface * Throw an UnsupportedOperationException if an ignored method was called
1 parent bc8b4b1 commit 757de66

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
5959
for (final Method method : targetType.getMethods()) {
6060
if (method.getDeclaringClass() == Object.class ||
6161
(method.getModifiers() & Modifier.STATIC) != 0 ||
62-
Util.isDefault(method)) {
62+
Util.isDefault(method) || method.isAnnotationPresent(FeignIgnore.class)) {
6363
continue;
6464
}
6565
final MethodMetadata metadata = parseAndValidateMetadata(targetType, method);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2023 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign;
15+
16+
import java.lang.annotation.Retention;
17+
18+
import static java.lang.annotation.ElementType.*;
19+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
20+
21+
/**
22+
* Indicates that method will be ignored
23+
*/
24+
@Retention(RUNTIME)
25+
@java.lang.annotation.Target({METHOD})
26+
public @interface FeignIgnore {
27+
}

core/src/main/java/feign/ReflectiveFeign.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
9191
return hashCode();
9292
} else if ("toString".equals(method.getName())) {
9393
return toString();
94+
} else if (!dispatch.containsKey(method)) {
95+
throw new UnsupportedOperationException(String.format("Method \"%s\" should not be called", method.getName()));
9496
}
9597

9698
return dispatch.get(method).invoke(args);

core/src/test/java/feign/FeignTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import okio.Buffer;
3434
import org.assertj.core.data.MapEntry;
3535
import org.assertj.core.util.Maps;
36+
import org.junit.Assert;
3637
import org.junit.Rule;
3738
import org.junit.Test;
3839
import org.junit.rules.ExpectedException;
@@ -1167,6 +1168,20 @@ public void responseInterceptorChainOrder() throws Exception {
11671168
assertEquals("ResponseInterceptor did not extract the response body", body, api.post());
11681169
}
11691170

1171+
@Test
1172+
public void testCallIgnoredMethod() throws Exception {
1173+
TestInterface api = new TestInterfaceBuilder()
1174+
.target("http://localhost:" + server.getPort());
1175+
1176+
try {
1177+
api.ignore();
1178+
Assert.fail("No exception thrown");
1179+
} catch (Exception e) {
1180+
assertThat(e.getClass()).isEqualTo(UnsupportedOperationException.class);
1181+
assertThat(e.getMessage()).isEqualTo("Method \"ignore\" should not be called");
1182+
}
1183+
}
1184+
11701185
interface TestInterface {
11711186

11721187
@RequestLine("POST /")
@@ -1272,6 +1287,9 @@ void queryMapPropertyInheritenceWithBeanMapEncoder(@QueryMap(
12721287
@Headers("Custom: {complex}")
12731288
void supportComplexHttpHeaders(@Param("complex") String complex);
12741289

1290+
@FeignIgnore
1291+
String ignore();
1292+
12751293
class ClockToMillis implements Param.Expander {
12761294

12771295
@Override

reactive/src/main/java/feign/reactive/ReactiveInvocationHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4848
return hashCode();
4949
} else if ("toString".equals(method.getName())) {
5050
return toString();
51+
} else if (!dispatch.containsKey(method)) {
52+
throw new UnsupportedOperationException(String.format("Method \"%s\" should not be called", method.getName()));
5153
}
5254
return this.invoke(method, this.dispatch.get(method), args);
5355
}

reactive/src/test/java/feign/reactive/ReactiveFeignIntegrationTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,10 @@
2222
import static org.mockito.Mockito.times;
2323
import static org.mockito.Mockito.verify;
2424
import static org.mockito.Mockito.when;
25-
import feign.Client;
26-
import feign.Logger;
25+
26+
import feign.*;
2727
import feign.Logger.Level;
28-
import feign.Param;
29-
import feign.QueryMap;
30-
import feign.QueryMapEncoder;
31-
import feign.Request;
3228
import feign.Request.Options;
33-
import feign.RequestInterceptor;
34-
import feign.RequestLine;
35-
import feign.RequestTemplate;
36-
import feign.Response;
37-
import feign.ResponseMapper;
38-
import feign.RetryableException;
39-
import feign.Retryer;
4029
import feign.codec.Decoder;
4130
import feign.codec.ErrorDecoder;
4231
import feign.jackson.JacksonDecoder;
@@ -52,6 +41,7 @@
5241
import javax.ws.rs.Path;
5342
import okhttp3.mockwebserver.MockResponse;
5443
import okhttp3.mockwebserver.MockWebServer;
44+
import org.junit.Assert;
5545
import org.junit.Rule;
5646
import org.junit.Test;
5747
import org.junit.rules.ExpectedException;
@@ -73,6 +63,20 @@ private String getServerUrl() {
7363
return "http://localhost:" + this.webServer.getPort();
7464
}
7565

66+
@Test
67+
public void testCallIgnoredMethod() throws Exception {
68+
TestReactorService service = ReactorFeign.builder()
69+
.target(TestReactorService.class, this.getServerUrl());
70+
71+
try {
72+
service.ignore().subscribe();
73+
Assert.fail("No exception thrown");
74+
} catch (Exception e) {
75+
assertThat(e.getClass()).isEqualTo(UnsupportedOperationException.class);
76+
assertThat(e.getMessage()).isEqualTo("Method \"ignore\" should not be called");
77+
}
78+
}
79+
7680
@Test
7781
public void testDefaultMethodsNotProxied() {
7882
TestReactorService service = ReactorFeign.builder()
@@ -336,6 +340,9 @@ interface TestReactorService {
336340

337341
@RequestLine("GET /users")
338342
Mono<List<User>> usersMono();
343+
344+
@FeignIgnore
345+
Mono<String> ignore();
339346
}
340347

341348

0 commit comments

Comments
 (0)