26
26
import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
27
27
28
28
import javax .servlet .http .HttpServletRequest ;
29
- import java .util .*;
30
-
31
- import static org .springdoc .core .Constants .*;
29
+ import java .util .HashSet ;
30
+ import java .util .LinkedHashMap ;
31
+ import java .util .List ;
32
+ import java .util .Map ;
33
+ import java .util .Optional ;
34
+ import java .util .Set ;
35
+
36
+ import static org .springdoc .core .Constants .API_DOCS_URL ;
37
+ import static org .springdoc .core .Constants .APPLICATION_OPENAPI_YAML ;
38
+ import static org .springdoc .core .Constants .DEFAULT_API_DOCS_URL_YAML ;
32
39
import static org .springframework .util .AntPathMatcher .DEFAULT_PATH_SEPARATOR ;
33
40
34
41
@ RestController
@@ -38,9 +45,6 @@ public class OpenApiResource extends AbstractOpenApiResource {
38
45
39
46
private final Optional <ActuatorProvider > servletContextProvider ;
40
47
41
- @ Value (SPRINGDOC_SHOW_ACTUATOR_VALUE )
42
- private boolean showActuator ;
43
-
44
48
public OpenApiResource (OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
45
49
AbstractResponseBuilder responseBuilder , OperationBuilder operationParser ,
46
50
RequestMappingInfoHandlerMapping requestMappingHandlerMapping , Optional <ActuatorProvider > servletContextProvider ,
@@ -53,12 +57,10 @@ public OpenApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder req
53
57
public OpenApiResource (OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
54
58
AbstractResponseBuilder responseBuilder , OperationBuilder operationParser ,
55
59
RequestMappingInfoHandlerMapping requestMappingHandlerMapping , Optional <ActuatorProvider > servletContextProvider ,
56
- Optional <List <OpenApiCustomiser >> openApiCustomisers , List <String > pathsToMatch , List <String > packagesToScan ,
57
- boolean showActuator ) {
60
+ Optional <List <OpenApiCustomiser >> openApiCustomisers , List <String > pathsToMatch , List <String > packagesToScan ) {
58
61
super (openAPIBuilder , requestBuilder , responseBuilder , operationParser , openApiCustomisers , pathsToMatch , packagesToScan );
59
62
this .requestMappingHandlerMapping = requestMappingHandlerMapping ;
60
63
this .servletContextProvider = servletContextProvider ;
61
- this .showActuator = showActuator ;
62
64
}
63
65
64
66
@ Operation (hidden = true )
@@ -82,16 +84,16 @@ public String openapiYaml(HttpServletRequest request, @Value(DEFAULT_API_DOCS_UR
82
84
@ Override
83
85
protected void getPaths (Map <String , Object > restControllers ) {
84
86
Map <RequestMappingInfo , HandlerMethod > map = requestMappingHandlerMapping .getHandlerMethods ();
85
- calculatePath (restControllers , map );
86
- if ( showActuator && servletContextProvider . isPresent ()) {
87
- map = servletContextProvider .get (). getWebMvcHandlerMapping (). getHandlerMethods ();
88
- Set < HandlerMethod > handlerMethods = new HashSet <>( map . values () );
89
- this .openAPIBuilder .addTag (handlerMethods , SPRINGDOC_ACTUATOR_TAG );
90
- calculatePath (restControllers , map );
87
+ calculatePath (restControllers , map , Optional . empty () );
88
+
89
+ if ( servletContextProvider .isPresent ()){
90
+ map = servletContextProvider . get (). getMethods ( );
91
+ this .openAPIBuilder .addTag (new HashSet <>( map . values ()), servletContextProvider . get (). getTag () );
92
+ calculatePath (restControllers , map , servletContextProvider );
91
93
}
92
94
}
93
95
94
- private void calculatePath (Map <String , Object > restControllers , Map <RequestMappingInfo , HandlerMethod > map ) {
96
+ private void calculatePath (Map <String , Object > restControllers , Map <RequestMappingInfo , HandlerMethod > map , Optional < ActuatorProvider > actuatorProvider ) {
95
97
for (Map .Entry <RequestMappingInfo , HandlerMethod > entry : map .entrySet ()) {
96
98
RequestMappingInfo requestMappingInfo = entry .getKey ();
97
99
HandlerMethod handlerMethod = entry .getValue ();
@@ -100,7 +102,10 @@ private void calculatePath(Map<String, Object> restControllers, Map<RequestMappi
100
102
Map <String , String > regexMap = new LinkedHashMap <>();
101
103
for (String pattern : patterns ) {
102
104
String operationPath = PathUtils .parsePath (pattern , regexMap );
103
- if (isRestController (restControllers , handlerMethod , operationPath ) && isPackageToScan (handlerMethod .getBeanType ().getPackage ().getName ()) && isPathToMatch (operationPath )) {
105
+ if ( ((actuatorProvider .isPresent () && actuatorProvider .get ().isRestController (restControllers , handlerMethod , operationPath ))
106
+ || isRestController (restControllers , handlerMethod , operationPath ))
107
+ && isPackageToScan (handlerMethod .getBeanType ().getPackage ().getName ())
108
+ && isPathToMatch (operationPath )) {
104
109
Set <RequestMethod > requestMethods = requestMappingInfo .getMethodsCondition ().getMethods ();
105
110
calculatePath (openAPIBuilder , handlerMethod , operationPath , requestMethods );
106
111
}
@@ -110,21 +115,13 @@ private void calculatePath(Map<String, Object> restControllers, Map<RequestMappi
110
115
111
116
private boolean isRestController (Map <String , Object > restControllers , HandlerMethod handlerMethod ,
112
117
String operationPath ) {
113
- boolean result ;
114
- if (showActuator )
115
- result = operationPath .startsWith (DEFAULT_PATH_SEPARATOR );
116
- else {
117
- ResponseBody responseBodyAnnotation = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (),
118
- ResponseBody .class );
119
-
120
- if (responseBodyAnnotation == null )
121
- responseBodyAnnotation = ReflectionUtils .getAnnotation (handlerMethod .getMethod (), ResponseBody .class );
122
- result = operationPath .startsWith (DEFAULT_PATH_SEPARATOR )
123
- && (restControllers .containsKey (handlerMethod .getBean ().toString ())
124
- || (responseBodyAnnotation != null && AnnotationUtils .findAnnotation (handlerMethod .getBeanType (), Hidden .class ) == null ));
125
- }
118
+ ResponseBody responseBodyAnnotation = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (), ResponseBody .class );
126
119
127
- return result ;
120
+ if (responseBodyAnnotation == null )
121
+ responseBodyAnnotation = ReflectionUtils .getAnnotation (handlerMethod .getMethod (), ResponseBody .class );
122
+ return operationPath .startsWith (DEFAULT_PATH_SEPARATOR )
123
+ && (restControllers .containsKey (handlerMethod .getBean ().toString ())
124
+ || (responseBodyAnnotation != null && AnnotationUtils .findAnnotation (handlerMethod .getBeanType (), Hidden .class ) == null ));
128
125
}
129
126
130
127
private void calculateServerUrl (HttpServletRequest request , String apiDocsUrl ) {
0 commit comments