Skip to content

Commit 12d32c0

Browse files
committed
webflux integration
1 parent 26e177d commit 12d32c0

File tree

37 files changed

+774
-311
lines changed

37 files changed

+774
-311
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.io.UnsupportedEncodingException;
2424
import java.lang.reflect.Method;
25+
import java.net.URI;
26+
import java.net.URISyntaxException;
2527
import java.net.URLDecoder;
2628
import java.nio.charset.StandardCharsets;
2729
import java.time.Duration;
@@ -86,14 +88,17 @@
8688
import org.springframework.context.ApplicationContext;
8789
import org.springframework.core.annotation.AnnotatedElementUtils;
8890
import org.springframework.core.annotation.AnnotationUtils;
91+
import org.springframework.core.env.Environment;
8992
import org.springframework.util.AntPathMatcher;
9093
import org.springframework.util.CollectionUtils;
9194
import org.springframework.web.bind.annotation.RequestBody;
9295
import org.springframework.web.bind.annotation.RequestMapping;
9396
import org.springframework.web.bind.annotation.RequestMethod;
9497
import org.springframework.web.method.HandlerMethod;
9598

99+
import static org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP;
96100
import static org.springdoc.core.Constants.OPERATION_ATTRIBUTE;
101+
import static org.springdoc.core.Constants.SPRING_MVC_SERVLET_PATH;
97102
import static org.springdoc.core.converters.SchemaPropertyDeprecatingConverter.isDeprecated;
98103

99104
/**
@@ -1027,6 +1032,50 @@ protected String writeYamlValue(OpenAPI openAPI) throws JsonProcessingException
10271032
return result;
10281033
}
10291034

1035+
/**
1036+
* Gets actuator uri.
1037+
*
1038+
* @param scheme the scheme
1039+
* @param host the host
1040+
* @return the actuator uri
1041+
*/
1042+
protected URI getActuatorURI(String scheme, String host) {
1043+
int port;
1044+
String path;
1045+
URI uri = null;
1046+
if (optionalActuatorProvider.isPresent()) {
1047+
ActuatorProvider actuatorProvider = optionalActuatorProvider.get();
1048+
if (ACTUATOR_DEFAULT_GROUP.equals(this.groupName)) {
1049+
port = actuatorProvider.getActuatorPort();
1050+
path = actuatorProvider.getActuatorPath();
1051+
}
1052+
else {
1053+
port = actuatorProvider.getApplicationPort();
1054+
path = actuatorProvider.getContextPath();
1055+
String mvcServletPath = this.openAPIService.getContext().getBean(Environment.class).getProperty(SPRING_MVC_SERVLET_PATH);
1056+
if (StringUtils.isNotEmpty(mvcServletPath))
1057+
path = path + mvcServletPath;
1058+
}
1059+
try {
1060+
uri = new URI(StringUtils.defaultIfEmpty(scheme, "http"), null, StringUtils.defaultIfEmpty(host, "localhost"), port, path, null, null);
1061+
}
1062+
catch (URISyntaxException e) {
1063+
LOGGER.error("Unable to parse the URL: scheme {}, host {}, port {}, path {}", scheme, host, port, path);
1064+
}
1065+
}
1066+
1067+
return uri;
1068+
}
1069+
1070+
/**
1071+
* Is show actuator boolean.
1072+
*
1073+
* @return the boolean
1074+
*/
1075+
protected boolean isShowActuator() {
1076+
return springDocConfigProperties.isShowActuator() && optionalActuatorProvider.isPresent();
1077+
}
1078+
10301079
/**
10311080
* Write json value string.
10321081
*

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

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,100 @@
2121
package org.springdoc.core;
2222

2323
import java.util.Map;
24+
import java.util.Optional;
2425

2526
import io.swagger.v3.oas.models.ExternalDocumentation;
2627
import io.swagger.v3.oas.models.tags.Tag;
2728
import org.springdoc.api.AbstractOpenApiResource;
2829

30+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
31+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
32+
import org.springframework.boot.autoconfigure.web.ServerProperties;
33+
import org.springframework.boot.web.context.WebServerInitializedEvent;
34+
import org.springframework.boot.web.server.WebServer;
35+
import org.springframework.context.ApplicationContext;
36+
import org.springframework.context.ApplicationListener;
2937
import org.springframework.util.AntPathMatcher;
3038

3139

3240
/**
33-
* The interface Actuator provider.
41+
* The type Actuator provider.
3442
* @author bnasslahsen
3543
*/
36-
public interface ActuatorProvider {
44+
public abstract class ActuatorProvider implements ApplicationListener<WebServerInitializedEvent> {
3745

3846
/**
39-
* Gets methods.
47+
* The Management server properties.
48+
*/
49+
protected ManagementServerProperties managementServerProperties;
50+
51+
/**
52+
* The Web endpoint properties.
53+
*/
54+
protected WebEndpointProperties webEndpointProperties;
55+
56+
/**
57+
* The Server properties.
58+
*/
59+
protected ServerProperties serverProperties;
60+
61+
/**
62+
* The Spring doc config properties.
63+
*/
64+
protected SpringDocConfigProperties springDocConfigProperties;
65+
66+
/**
67+
* The Actuator web server.
68+
*/
69+
protected WebServer actuatorWebServer;
70+
71+
/**
72+
* The Application web server.
73+
*/
74+
protected WebServer applicationWebServer;
75+
76+
/**
77+
* The Management application context.
78+
*/
79+
protected ApplicationContext managementApplicationContext;
80+
81+
/**
82+
* Instantiates a new Actuator provider.
4083
*
41-
* @return the methods
84+
* @param managementServerProperties the management server properties
85+
* @param webEndpointProperties the web endpoint properties
86+
* @param serverProperties the server properties
87+
* @param springDocConfigProperties the spring doc config properties
4288
*/
43-
Map getMethods();
89+
public ActuatorProvider(Optional<ManagementServerProperties> managementServerProperties,
90+
Optional<WebEndpointProperties> webEndpointProperties,
91+
ServerProperties serverProperties,
92+
SpringDocConfigProperties springDocConfigProperties) {
93+
94+
managementServerProperties.ifPresent(managementServerProperties1 -> this.managementServerProperties = managementServerProperties1);
95+
webEndpointProperties.ifPresent(webEndpointProperties1 -> this.webEndpointProperties = webEndpointProperties1);
96+
97+
this.serverProperties = serverProperties;
98+
this.springDocConfigProperties = springDocConfigProperties;
99+
}
100+
101+
@Override
102+
public void onApplicationEvent(WebServerInitializedEvent event) {
103+
if ("application".equals(event.getApplicationContext().getId())) {
104+
applicationWebServer = event.getWebServer();
105+
}
106+
else if ("application:management".equals(event.getApplicationContext().getId())) {
107+
managementApplicationContext = event.getApplicationContext();
108+
actuatorWebServer = event.getWebServer();
109+
}
110+
}
44111

45112
/**
46113
* Gets tag.
47114
*
48115
* @return the tag
49116
*/
50-
default Tag getTag() {
117+
public Tag getTag() {
51118
Tag actuatorTag = new Tag();
52119
actuatorTag.setName(Constants.SPRINGDOC_ACTUATOR_TAG);
53120
actuatorTag.setDescription(Constants.SPRINGDOC_ACTUATOR_DESCRIPTION);
@@ -66,21 +133,70 @@ default Tag getTag() {
66133
* @param controllerClass the controller class
67134
* @return the boolean
68135
*/
69-
default boolean isRestController(String operationPath, Class<?> controllerClass) {
136+
public boolean isRestController(String operationPath, Class<?> controllerClass) {
70137
return operationPath.startsWith(AntPathMatcher.DEFAULT_PATH_SEPARATOR)
71138
&& !AbstractOpenApiResource.isHiddenRestControllers(controllerClass);
72139
}
73140

74-
int getApplicationPort();
141+
/**
142+
* Is use management port boolean.
143+
*
144+
* @return the boolean
145+
*/
146+
public boolean isUseManagementPort() {
147+
return springDocConfigProperties.isUseManagementPort();
148+
}
75149

76-
int getActuatorPort();
150+
/**
151+
* Gets base path.
152+
*
153+
* @return the base path
154+
*/
155+
public String getBasePath() {
156+
return webEndpointProperties.getBasePath();
157+
}
77158

78-
String getActuatorPath();
159+
/**
160+
* Gets context path.
161+
*
162+
* @return the context path
163+
*/
164+
public String getContextPath() {
165+
return "";
166+
}
79167

80-
boolean isUseManagementPort();
168+
/**
169+
* Gets actuator path.
170+
*
171+
* @return the actuator path
172+
*/
173+
public String getActuatorPath() {
174+
return managementServerProperties.getBasePath();
175+
}
81176

82-
String getBasePath();
177+
/**
178+
* Gets application port.
179+
*
180+
* @return the application port
181+
*/
182+
public int getApplicationPort() {
183+
return applicationWebServer.getPort();
184+
}
185+
186+
/**
187+
* Gets actuator port.
188+
*
189+
* @return the actuator port
190+
*/
191+
public int getActuatorPort() {
192+
return actuatorWebServer.getPort();
193+
}
83194

84-
String getContextPath();
195+
/**
196+
* Gets methods.
197+
*
198+
* @return the methods
199+
*/
200+
public abstract Map getMethods();
85201

86202
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class CacheOrGroupedOpenApiCondition extends AnyNestedCondition {
4444
* The type On multiple open api support condition.
4545
* @author bnasslahsen
4646
*/
47-
@Conditional(MultipleOpenApiGroupsCondition.class)
47+
@Conditional(MultipleOpenApiSupportCondition.class)
4848
static class OnMultipleOpenApiSupportCondition {}
4949

5050
/**

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

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ public final class Constants {
6060
*/
6161
public static final String SWAGGER_CONFIG_URL = API_DOCS_URL + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
6262

63+
/**
64+
* The constant YAML.
65+
*/
66+
public static final String YAML = "yaml";
67+
6368
/**
6469
* The constant DEFAULT_API_DOCS_URL_YAML.
6570
*/
66-
public static final String DEFAULT_API_DOCS_URL_YAML = API_DOCS_URL + ".yaml";
71+
public static final String DEFAULT_API_DOCS_URL_YAML = API_DOCS_URL + "." + YAML;
6772

6873
/**
6974
* The constant SPRINGDOC_ENABLED.
@@ -96,28 +101,19 @@ public final class Constants {
96101
public static final String SPRINGDOC_SWAGGER_UI_ENABLED = "springdoc.swagger-ui.enabled";
97102

98103
/**
99-
* The constant SPRINGDOC_USE_MANAGEMENT_PORT.
104+
* The constant NULL.
100105
*/
101-
public static final String SPRINGDOC_USE_MANAGEMENT_PORT = "springdoc.use-management-port";
106+
public static final String NULL = ":#{null}";
102107

103108
/**
104-
* The constant DEFAULT_SWAGGER_UI_ACTUATOR_PATH.
109+
* The constant SPRING_MVC_SERVLET_PATH.
105110
*/
106-
public static final String DEFAULT_SWAGGER_UI_ACTUATOR_PATH= "swagger-ui";
107-
108-
public static final String DEFAULT_API_DOCS_ACTUATOR_URL = "openapi";
109-
110-
/**
111-
* The constant NULL.
112-
*/
113-
public static final String NULL = ":#{null}";
111+
public static final String SPRING_MVC_SERVLET_PATH = "spring.mvc.servlet.path";
114112

115113
/**
116114
* The constant MVC_SERVLET_PATH.
117115
*/
118-
public static final String MVC_SERVLET_PATH = "${spring.mvc.servlet.path" + NULL + "}";
119-
120-
public static final String ACTUATOR_DEFAULT_GROUP = "x-actuator";
116+
public static final String MVC_SERVLET_PATH = "${" + SPRING_MVC_SERVLET_PATH + NULL + "}";
121117

122118
/**
123119
* The constant SPRINGDOC_SHOW_ACTUATOR.
@@ -154,15 +150,20 @@ public final class Constants {
154150
*/
155151
public static final String CLASSPATH_RESOURCE_LOCATION = ResourceUtils.CLASSPATH_URL_PREFIX + "/META-INF/resources";
156152

153+
/**
154+
* The constant SWAGGER_UI_PREFIX.
155+
*/
156+
public static final String SWAGGER_UI_PREFIX = "/swagger-ui";
157+
157158
/**
158159
* The constant SWAGGER_UI_URL.
159160
*/
160-
public static final String SWAGGER_UI_URL = "/swagger-ui/index.html";
161+
public static final String SWAGGER_UI_URL = SWAGGER_UI_PREFIX + "/index.html";
161162

162163
/**
163164
* The constant SWAGGER_UI_OAUTH_REDIRECT_URL.
164165
*/
165-
public static final String SWAGGER_UI_OAUTH_REDIRECT_URL = "/swagger-ui/oauth2-redirect.html";
166+
public static final String SWAGGER_UI_OAUTH_REDIRECT_URL = SWAGGER_UI_PREFIX + "/oauth2-redirect.html";
166167

167168
/**
168169
* The constant APPLICATION_OPENAPI_YAML.
@@ -289,6 +290,51 @@ public final class Constants {
289290
*/
290291
public static final String OPERATION_ATTRIBUTE = Constants.class.getName() + ".operation";
291292

293+
/**
294+
* The constant MANAGEMENT_ENDPOINTS_WEB.
295+
*/
296+
public static final String MANAGEMENT_ENDPOINTS_WEB = "management.endpoints.web";
297+
298+
/**
299+
* The constant ALL_PATTERN.
300+
*/
301+
public static final String ALL_PATTERN = "/**";
302+
303+
/**
304+
* The constant HEALTH_PATTERN.
305+
*/
306+
public static final String HEALTH_PATTERN = "/health/*";
307+
308+
/**
309+
* The constant SWAGGER_UI_PATTERN.
310+
*/
311+
public static final String SWAGGER_UI_PATTERN = SWAGGER_UI_PREFIX + ALL_PATTERN;
312+
313+
/**
314+
* The constant SPRINGDOC_USE_MANAGEMENT_PORT.
315+
*/
316+
public static final String SPRINGDOC_USE_MANAGEMENT_PORT = "springdoc.use-management-port";
317+
318+
/**
319+
* The constant DEFAULT_SWAGGER_UI_ACTUATOR_PATH.
320+
*/
321+
public static final String DEFAULT_SWAGGER_UI_ACTUATOR_PATH= "swagger-ui";
322+
323+
/**
324+
* The constant DEFAULT_API_DOCS_ACTUATOR_URL.
325+
*/
326+
public static final String DEFAULT_API_DOCS_ACTUATOR_URL = "openapi";
327+
328+
/**
329+
* The constant DEFAULT_YAML_API_DOCS_ACTUATOR_PATH.
330+
*/
331+
public static final String DEFAULT_YAML_API_DOCS_ACTUATOR_PATH = DEFAULT_PATH_SEPARATOR + YAML;
332+
333+
/**
334+
* The constant ACTUATOR_DEFAULT_GROUP.
335+
*/
336+
public static final String ACTUATOR_DEFAULT_GROUP = "x-actuator";
337+
292338
/**
293339
* Instantiates a new Constants.
294340
*/

0 commit comments

Comments
 (0)