Skip to content

Commit 4ba3bde

Browse files
committed
Feign clients that uses SpringMVCContract appears in OpenApi like controllers exposed if spring-boot-starter-actuator is in classpath. Fixes #1230.
1 parent 0324fbd commit 4ba3bde

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import org.springframework.web.bind.annotation.RequestBody;
100100
import org.springframework.web.bind.annotation.RequestMapping;
101101
import org.springframework.web.bind.annotation.RequestMethod;
102+
import org.springframework.web.bind.annotation.ResponseBody;
102103
import org.springframework.web.method.HandlerMethod;
103104

104105
import static org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP;
@@ -676,6 +677,20 @@ protected boolean isAdditionalRestController(Class<?> rawClass) {
676677
return ADDITIONAL_REST_CONTROLLERS.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
677678
}
678679

680+
/**
681+
* Contains response body boolean.
682+
*
683+
* @param handlerMethod the handler method
684+
* @return the boolean
685+
*/
686+
public static boolean containsResponseBody(HandlerMethod handlerMethod) {
687+
ResponseBody responseBodyAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), ResponseBody.class);
688+
if (responseBodyAnnotation == null)
689+
responseBodyAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), ResponseBody.class);
690+
return responseBodyAnnotation!=null;
691+
}
692+
693+
679694
/**
680695
* Is hidden rest controllers boolean.
681696
*

springdoc-openapi-common/src/main/java/org/springdoc/core/ActuatorProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.context.ApplicationContext;
3737
import org.springframework.context.ApplicationListener;
3838
import org.springframework.util.AntPathMatcher;
39+
import org.springframework.web.method.HandlerMethod;
3940

4041
import static org.apache.commons.lang3.StringUtils.EMPTY;
4142

@@ -132,12 +133,13 @@ public static Tag getTag() {
132133
* Is rest controller boolean.
133134
*
134135
* @param operationPath the operation path
135-
* @param controllerClass the controller class
136+
* @param handlerMethod the handler method
136137
* @return the boolean
137138
*/
138-
public boolean isRestController(String operationPath, Class<?> controllerClass) {
139+
public boolean isRestController(String operationPath, HandlerMethod handlerMethod) {
139140
return operationPath.startsWith(AntPathMatcher.DEFAULT_PATH_SEPARATOR)
140-
&& !AbstractOpenApiResource.isHiddenRestControllers(controllerClass);
141+
&& !AbstractOpenApiResource.isHiddenRestControllers(handlerMethod.getBeanType())
142+
&& AbstractOpenApiResource.containsResponseBody(handlerMethod);
141143
}
142144

143145
/**

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/OpenApiResource.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@
5252

5353
import org.springframework.beans.factory.ObjectFactory;
5454
import org.springframework.core.annotation.AnnotatedElementUtils;
55-
import org.springframework.core.annotation.AnnotationUtils;
5655
import org.springframework.util.CollectionUtils;
5756
import org.springframework.util.MimeType;
5857
import org.springframework.web.bind.annotation.RequestMethod;
59-
import org.springframework.web.bind.annotation.ResponseBody;
6058
import org.springframework.web.method.HandlerMethod;
6159
import org.springframework.web.servlet.ModelAndView;
6260
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
@@ -251,7 +249,7 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
251249
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
252250
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
253251
String[] headers = requestMappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString).toArray(String[]::new);
254-
if (((isShowActuator() && optionalActuatorProvider.get().isRestController(operationPath, handlerMethod.getBeanType()))
252+
if (((isShowActuator() && optionalActuatorProvider.get().isRestController(operationPath, handlerMethod))
255253
|| isRestController(restControllers, handlerMethod, operationPath))
256254
&& isFilterCondition(handlerMethod, operationPath, produces, consumes, headers)) {
257255
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
@@ -308,11 +306,8 @@ protected boolean isRestController(Map<String, Object> restControllers, HandlerM
308306
boolean hasOperationAnnotation = AnnotatedElementUtils.hasAnnotation(handlerMethod.getMethod(), Operation.class);
309307
if (hasOperationAnnotation)
310308
return true;
311-
ResponseBody responseBodyAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), ResponseBody.class);
312-
if (responseBodyAnnotation == null)
313-
responseBodyAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), ResponseBody.class);
314309

315-
return (responseBodyAnnotation != null && restControllers.containsKey(handlerMethod.getBean().toString()) || isAdditionalRestController(handlerMethod.getBeanType()))
310+
return (containsResponseBody(handlerMethod) & restControllers.containsKey(handlerMethod.getBean().toString()) || isAdditionalRestController(handlerMethod.getBeanType()))
316311
&& operationPath.startsWith(DEFAULT_PATH_SEPARATOR)
317312
&& (springDocConfigProperties.isModelAndViewAllowed() || !ModelAndView.class.isAssignableFrom(handlerMethod.getMethod().getReturnType()));
318313
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package test.org.springdoc.api.app68.api.user;
2+
3+
import test.org.springdoc.api.app68.model.User;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
@RequestMapping(value= "/users", consumes = "application/json")
11+
@Controller
12+
public class UserClient {
13+
14+
@GetMapping("/{id}")
15+
public User findById(@PathVariable Integer id)
16+
{
17+
return null;
18+
}
19+
}

0 commit comments

Comments
 (0)