Skip to content

Commit 737ecca

Browse files
committed
Option to disable root api-docs path when using groups Fixes #2510
1 parent 8ece255 commit 737ecca

File tree

12 files changed

+219
-6
lines changed

12 files changed

+219
-6
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SpringDocConfigProperties.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ public class SpringDocConfigProperties {
232232
*/
233233
private boolean enableDataRest = true;
234234

235+
/**
236+
* The Enable default api docs.
237+
*/
238+
private boolean enableDefaultApiDocs = true;
239+
235240
/**
236241
* convert query param to form data when consumes is multipart/form-data
237242
*/
@@ -1762,6 +1767,22 @@ public boolean isOpenapi31() {
17621767
return true;
17631768
return false;
17641769
}
1765-
1766-
1770+
1771+
/**
1772+
* Is enable default api docs boolean.
1773+
*
1774+
* @return the boolean
1775+
*/
1776+
public boolean isEnableDefaultApiDocs() {
1777+
return enableDefaultApiDocs;
1778+
}
1779+
1780+
/**
1781+
* Sets enable default api docs.
1782+
*
1783+
* @param enableDefaultApiDocs the enable default api docs
1784+
*/
1785+
public void setEnableDefaultApiDocs(boolean enableDefaultApiDocs) {
1786+
this.enableDefaultApiDocs = enableDefaultApiDocs;
1787+
}
17671788
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ public final class Constants {
370370
*/
371371
public static final String SPRINGDOC_USE_MANAGEMENT_PORT = "springdoc.use-management-port";
372372

373+
374+
/**
375+
* The constant SPRINGDOC_ENABLE_DEFAULT_API_DOCS.
376+
*/
377+
public static final String SPRINGDOC_ENABLE_DEFAULT_API_DOCS = "springdoc.enable-default-api-docs";
378+
373379
/**
374380
* The constant SPRINGDOC_USE_ROOT_PATH.
375381
*/

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/api/OpenApiActuatorResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.springframework.beans.factory.ObjectFactory;
4242
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
43+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4344
import org.springframework.http.MediaType;
4445
import org.springframework.http.server.reactive.ServerHttpRequest;
4546
import org.springframework.web.bind.annotation.GetMapping;
@@ -48,6 +49,7 @@
4849
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
4950
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
5051
import static org.springdoc.core.utils.Constants.DEFAULT_YAML_API_DOCS_ACTUATOR_PATH;
52+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
5153
import static org.springdoc.core.utils.Constants.YAML;
5254
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
5355

@@ -56,6 +58,7 @@
5658
* @author bnasslashen
5759
*/
5860
@RestControllerEndpoint(id = DEFAULT_API_DOCS_ACTUATOR_URL)
61+
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
5962
public class OpenApiActuatorResource extends OpenApiResource {
6063

6164
/**

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/api/OpenApiWebfluxResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.beans.factory.ObjectFactory;
4444
import org.springframework.beans.factory.annotation.Autowired;
4545
import org.springframework.beans.factory.annotation.Value;
46+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4647
import org.springframework.http.MediaType;
4748
import org.springframework.http.server.reactive.ServerHttpRequest;
4849
import org.springframework.web.bind.annotation.GetMapping;
@@ -51,13 +52,15 @@
5152
import static org.springdoc.core.utils.Constants.API_DOCS_URL;
5253
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
5354
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_URL_YAML;
55+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
5456

5557
/**
5658
* The type Open api resource.
5759
*
5860
* @author bnasslahsen
5961
*/
6062
@RestController
63+
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
6164
public class OpenApiWebfluxResource extends OpenApiResource {
6265

6366

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/configuration/SpringDocWebFluxConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class SpringDocWebFluxConfiguration {
9696
*/
9797
@Bean
9898
@ConditionalOnMissingBean
99-
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
99+
@ConditionalOnExpression("(${springdoc.use-management-port:false} == false ) and ${springdoc.enable-default-api-docs:true}")
100100
@Lazy(false)
101101
OpenApiWebfluxResource openApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
102102
GenericResponseService responseBuilder, OperationService operationParser,
@@ -205,7 +205,7 @@ ActuatorProvider actuatorProvider(ServerProperties serverProperties,
205205
*/
206206
@Bean
207207
@ConditionalOnMissingBean(MultipleOpenApiSupportConfiguration.class)
208-
@ConditionalOnProperty(SPRINGDOC_USE_MANAGEMENT_PORT)
208+
@ConditionalOnExpression("${springdoc.use-management-port:false} and ${springdoc.enable-default-api-docs:true}")
209209
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
210210
@Lazy(false)
211211
OpenApiActuatorResource actuatorOpenApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app190;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.utils.Constants;
23+
import test.org.springdoc.api.AbstractCommonTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
27+
import org.springframework.context.annotation.ComponentScan;
28+
29+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
30+
31+
@WebFluxTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=false")
32+
public class SpringDocApp190Test extends AbstractCommonTest {
33+
34+
@SpringBootApplication
35+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app190" })
36+
static class SpringDocTestApp {}
37+
38+
@Test
39+
public void test_disable_default_api_docs() throws Exception {
40+
webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
41+
.expectStatus().isNotFound();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app190;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.utils.Constants;
23+
import test.org.springdoc.api.AbstractCommonTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
27+
import org.springframework.context.annotation.ComponentScan;
28+
29+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
30+
31+
@WebFluxTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=true")
32+
public class SpringDocApp190bisTest extends AbstractCommonTest {
33+
34+
@SpringBootApplication
35+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app190" })
36+
static class SpringDocTestApp {}
37+
38+
@Test
39+
public void test_enable_default_api_docs() throws Exception {
40+
webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
41+
.expectStatus().isOk();
42+
}
43+
}

springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/api/OpenApiActuatorResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939

4040
import org.springframework.beans.factory.ObjectFactory;
4141
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
42+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4243
import org.springframework.http.MediaType;
4344
import org.springframework.web.bind.annotation.GetMapping;
4445

4546
import static org.apache.commons.lang3.StringUtils.EMPTY;
4647
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
4748
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
4849
import static org.springdoc.core.utils.Constants.DEFAULT_YAML_API_DOCS_ACTUATOR_PATH;
50+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
4951
import static org.springdoc.core.utils.Constants.YAML;
5052
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
5153

@@ -54,6 +56,7 @@
5456
* @author bnasslashen
5557
*/
5658
@RestControllerEndpoint(id = DEFAULT_API_DOCS_ACTUATOR_URL)
59+
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
5760
public class OpenApiActuatorResource extends OpenApiResource {
5861

5962
/**

springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/api/OpenApiWebMvcResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@
4343
import org.springframework.beans.factory.ObjectFactory;
4444
import org.springframework.beans.factory.annotation.Autowired;
4545
import org.springframework.beans.factory.annotation.Value;
46+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4647
import org.springframework.http.MediaType;
4748
import org.springframework.web.bind.annotation.GetMapping;
4849
import org.springframework.web.bind.annotation.RestController;
4950

5051
import static org.springdoc.core.utils.Constants.API_DOCS_URL;
5152
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
5253
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_URL_YAML;
54+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
5355

5456
/**
5557
* The type Open api resource.
5658
* @author bnasslahsen
5759
*/
5860
@RestController
61+
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
5962
public class OpenApiWebMvcResource extends OpenApiResource {
6063

6164
/**

springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/configuration/SpringDocWebMvcConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class SpringDocWebMvcConfiguration {
105105
*/
106106
@Bean
107107
@ConditionalOnMissingBean
108-
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
108+
@ConditionalOnExpression("(${springdoc.use-management-port:false} == false ) and ${springdoc.enable-default-api-docs:true}")
109109
@Lazy(false)
110110
OpenApiWebMvcResource openApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
111111
GenericResponseService responseBuilder, OperationService operationParser,
@@ -233,7 +233,7 @@ ActuatorProvider actuatorProvider(ServerProperties serverProperties,
233233
*/
234234
@Bean
235235
@ConditionalOnMissingBean(MultipleOpenApiSupportConfiguration.class)
236-
@ConditionalOnProperty(SPRINGDOC_USE_MANAGEMENT_PORT)
236+
@ConditionalOnExpression("${springdoc.use-management-port:false} and ${springdoc.enable-default-api-docs:true}")
237237
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
238238
@Lazy(false)
239239
OpenApiActuatorResource openApiActuatorResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.v30.app215;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.utils.Constants;
23+
import test.org.springdoc.api.AbstractCommonTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
27+
import org.springframework.context.annotation.ComponentScan;
28+
29+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32+
33+
@WebMvcTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=false")
34+
public class SpringDocApp215Test extends AbstractCommonTest {
35+
36+
@SpringBootApplication
37+
@ComponentScan(basePackages = { "org.springdoc" })
38+
static class SpringDocTestApp {}
39+
40+
@Test
41+
public void test_disable_default_api_docs() throws Exception {
42+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isNotFound());
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.v30.app215;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.utils.Constants;
23+
import test.org.springdoc.api.AbstractCommonTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
27+
import org.springframework.context.annotation.ComponentScan;
28+
29+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32+
33+
@WebMvcTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=true")
34+
public class SpringDocApp215bisTest extends AbstractCommonTest {
35+
36+
@SpringBootApplication
37+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.v30.app214" })
38+
static class SpringDocTestApp {}
39+
40+
@Test
41+
public void test_enable_default_api_docs() throws Exception {
42+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk());
43+
}
44+
}

0 commit comments

Comments
 (0)