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