Skip to content

Commit

Permalink
Allow to ignore methods on provided interface (#2218)
Browse files Browse the repository at this point in the history
* Allow to ignore methods on provided interface

* Throw an UnsupportedOperationException if an ignored method was called
  • Loading branch information
gromspys authored Oct 28, 2023
1 parent e37d7d5 commit b294a4c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 14 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/feign/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
for (final Method method : targetType.getMethods()) {
if (method.getDeclaringClass() == Object.class
|| (method.getModifiers() & Modifier.STATIC) != 0
|| Util.isDefault(method)) {
|| Util.isDefault(method)
|| method.isAnnotationPresent(FeignIgnore.class)) {
continue;
}
final MethodMetadata metadata = parseAndValidateMetadata(targetType, method);
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/feign/FeignIgnore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2012-2023 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;

/** Indicates that method will be ignored */
@Retention(RUNTIME)
@java.lang.annotation.Target({METHOD})
public @interface FeignIgnore {}
3 changes: 3 additions & 0 deletions core/src/main/java/feign/ReflectiveFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return hashCode();
} else if ("toString".equals(method.getName())) {
return toString();
} else if (!dispatch.containsKey(method)) {
throw new UnsupportedOperationException(
String.format("Method \"%s\" should not be called", method.getName()));
}

return dispatch.get(method).invoke(args);
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import okio.Buffer;
import org.assertj.core.data.MapEntry;
import org.assertj.core.util.Maps;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -1224,6 +1225,19 @@ public void responseInterceptorChainOrder() throws Exception {
assertEquals("ResponseInterceptor did not extract the response body", body, api.post());
}

@Test
public void testCallIgnoredMethod() throws Exception {
TestInterface api = new TestInterfaceBuilder().target("http://localhost:" + server.getPort());

try {
api.ignore();
Assert.fail("No exception thrown");
} catch (Exception e) {
assertThat(e.getClass()).isEqualTo(UnsupportedOperationException.class);
assertThat(e.getMessage()).isEqualTo("Method \"ignore\" should not be called");
}
}

interface TestInterface {

@RequestLine("POST /")
Expand Down Expand Up @@ -1331,6 +1345,9 @@ void queryMapPropertyInheritenceWithBeanMapEncoder(
@Headers("Custom: {complex}")
void supportComplexHttpHeaders(@Param("complex") String complex);

@FeignIgnore
String ignore();

class ClockToMillis implements Param.Expander {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return hashCode();
} else if ("toString".equals(method.getName())) {
return toString();
} else if (!dispatch.containsKey(method)) {
throw new UnsupportedOperationException(
String.format("Method \"%s\" should not be called", method.getName()));
}
return this.invoke(method, this.dispatch.get(method), args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import feign.Client;
import feign.Logger;
import feign.*;
import feign.Logger.Level;
import feign.Param;
import feign.QueryMap;
import feign.QueryMapEncoder;
import feign.Request;
import feign.Request.Options;
import feign.RequestInterceptor;
import feign.RequestLine;
import feign.RequestTemplate;
import feign.Response;
import feign.ResponseMapper;
import feign.RetryableException;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonDecoder;
Expand All @@ -53,6 +41,7 @@
import javax.ws.rs.Path;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -72,6 +61,20 @@ private String getServerUrl() {
return "http://localhost:" + this.webServer.getPort();
}

@Test
public void testCallIgnoredMethod() throws Exception {
TestReactorService service =
ReactorFeign.builder().target(TestReactorService.class, this.getServerUrl());

try {
service.ignore().subscribe();
Assert.fail("No exception thrown");
} catch (Exception e) {
assertThat(e.getClass()).isEqualTo(UnsupportedOperationException.class);
assertThat(e.getMessage()).isEqualTo("Method \"ignore\" should not be called");
}
}

@Test
public void testDefaultMethodsNotProxied() {
TestReactorService service =
Expand Down Expand Up @@ -323,6 +326,9 @@ interface TestReactorService {

@RequestLine("GET /users")
Mono<List<User>> usersMono();

@FeignIgnore
Mono<String> ignore();
}

interface TestReactiveXService {
Expand Down

0 comments on commit b294a4c

Please sign in to comment.