20
20
21
21
package org .springdoc .webmvc .ui ;
22
22
23
- import java .util .ArrayList ;
24
- import java .util .List ;
25
- import java .util .Map ;
26
- import java .util .Map .Entry ;
27
- import java .util .Optional ;
28
- import java .util .Set ;
29
-
30
- import javax .annotation .PostConstruct ;
31
23
import javax .servlet .http .HttpServletRequest ;
32
24
33
25
import io .swagger .v3 .oas .annotations .Operation ;
34
26
import org .apache .commons .lang3 .StringUtils ;
27
+ import org .springdoc .core .OpenAPIService ;
35
28
import org .springdoc .core .SpringDocConfigProperties ;
36
29
import org .springdoc .core .SwaggerUiConfigParameters ;
37
30
import org .springdoc .core .SwaggerUiConfigProperties ;
38
- import org .springdoc .webmvc .api .OpenApiResource ;
39
31
40
32
import org .springframework .beans .factory .annotation .Value ;
41
33
import org .springframework .http .ResponseEntity ;
42
34
import org .springframework .stereotype .Controller ;
43
- import org .springframework .util .CollectionUtils ;
44
35
import org .springframework .web .bind .annotation .GetMapping ;
45
- import org .springframework .web .method .HandlerMethod ;
46
- import org .springframework .web .servlet .mvc .method .RequestMappingInfo ;
47
- import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
48
36
49
37
import static org .springdoc .core .Constants .MVC_SERVLET_PATH ;
50
38
import static org .springdoc .core .Constants .SWAGGER_UI_PATH ;
51
39
import static org .springdoc .core .Constants .SWAGGGER_CONFIG_FILE ;
40
+ import static org .springdoc .webmvc .api .OpenApiWebMvcResource .findPathPrefix ;
52
41
import static org .springframework .util .AntPathMatcher .DEFAULT_PATH_SEPARATOR ;
53
42
54
43
/**
@@ -71,46 +60,20 @@ public class SwaggerWelcomeWebMvc extends SwaggerWelcomeCommon {
71
60
private String pathPrefix ;
72
61
73
62
/**
74
- * The Request mapping handler mapping .
63
+ * The Open api service .
75
64
*/
76
- private final Optional <List <RequestMappingInfoHandlerMapping >> requestMappingInfoHandlerMappingOptionalList ;
77
-
65
+ private final OpenAPIService openAPIService ;
78
66
79
67
/**
80
68
* Instantiates a new Swagger welcome web mvc.
81
- *
82
69
* @param swaggerUiConfig the swagger ui config
83
70
* @param springDocConfigProperties the spring doc config properties
84
71
* @param swaggerUiConfigParameters the swagger ui config parameters
85
- * @param requestMappingInfoHandlerMappingOptionalList the request mapping info handler mapping optional list
72
+ * @param openAPIService the open api service
86
73
*/
87
- public SwaggerWelcomeWebMvc (SwaggerUiConfigProperties swaggerUiConfig , SpringDocConfigProperties springDocConfigProperties , SwaggerUiConfigParameters swaggerUiConfigParameters , Optional < List < RequestMappingInfoHandlerMapping >> requestMappingInfoHandlerMappingOptionalList ) {
74
+ public SwaggerWelcomeWebMvc (SwaggerUiConfigProperties swaggerUiConfig , SpringDocConfigProperties springDocConfigProperties , SwaggerUiConfigParameters swaggerUiConfigParameters , OpenAPIService openAPIService ) {
88
75
super (swaggerUiConfig , springDocConfigProperties , swaggerUiConfigParameters );
89
- this .requestMappingInfoHandlerMappingOptionalList = requestMappingInfoHandlerMappingOptionalList ;
90
- }
91
-
92
- /**
93
- * Init.
94
- */
95
- @ PostConstruct
96
- private void init () {
97
- requestMappingInfoHandlerMappingOptionalList .ifPresent (requestMappingInfoHandlerMappingList ->
98
- requestMappingInfoHandlerMappingList .forEach (requestMappingHandlerMapping ->
99
- {
100
- Map <RequestMappingInfo , HandlerMethod > map = requestMappingHandlerMapping .getHandlerMethods ();
101
- List <Entry <RequestMappingInfo , HandlerMethod >> entries = new ArrayList <>(map .entrySet ());
102
- for (Map .Entry <RequestMappingInfo , HandlerMethod > entry : entries ) {
103
- RequestMappingInfo requestMappingInfo = entry .getKey ();
104
- Set <String > patterns = OpenApiResource .getActivePatterns (requestMappingInfo );
105
- if (!CollectionUtils .isEmpty (patterns )) {
106
- for (String operationPath : patterns ) {
107
- if (operationPath .endsWith (springDocConfigProperties .getApiDocs ().getPath ()))
108
- pathPrefix = operationPath .replace (springDocConfigProperties .getApiDocs ().getPath (), StringUtils .EMPTY );
109
- }
110
- }
111
- }
112
- }
113
- ));
76
+ this .openAPIService = openAPIService ;
114
77
}
115
78
116
79
/**
@@ -160,6 +123,8 @@ protected String buildUrl(String contextPath, final String docsUrl) {
160
123
*/
161
124
@ Override
162
125
protected String buildApiDocUrl () {
126
+ if (this .pathPrefix == null )
127
+ this .pathPrefix = findPathPrefix (this .openAPIService , this .springDocConfigProperties );
163
128
return buildUrl (contextPath + pathPrefix , springDocConfigProperties .getApiDocs ().getPath ());
164
129
}
165
130
@@ -172,4 +137,5 @@ protected String buildApiDocUrl() {
172
137
protected String buildSwaggerConfigUrl () {
173
138
return apiDocsUrl + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE ;
174
139
}
140
+
175
141
}
0 commit comments