Skip to content

Commit

Permalink
Option to disable root api-docs path when using groups Fixes #2510
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Mar 2, 2024
1 parent 8ece255 commit 737ecca
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public class SpringDocConfigProperties {
*/
private boolean enableDataRest = true;

/**
* The Enable default api docs.
*/
private boolean enableDefaultApiDocs = true;

/**
* convert query param to form data when consumes is multipart/form-data
*/
Expand Down Expand Up @@ -1762,6 +1767,22 @@ public boolean isOpenapi31() {
return true;
return false;
}



/**
* Is enable default api docs boolean.
*
* @return the boolean
*/
public boolean isEnableDefaultApiDocs() {
return enableDefaultApiDocs;
}

/**
* Sets enable default api docs.
*
* @param enableDefaultApiDocs the enable default api docs
*/
public void setEnableDefaultApiDocs(boolean enableDefaultApiDocs) {
this.enableDefaultApiDocs = enableDefaultApiDocs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ public final class Constants {
*/
public static final String SPRINGDOC_USE_MANAGEMENT_PORT = "springdoc.use-management-port";


/**
* The constant SPRINGDOC_ENABLE_DEFAULT_API_DOCS.
*/
public static final String SPRINGDOC_ENABLE_DEFAULT_API_DOCS = "springdoc.enable-default-api-docs";

/**
* The constant SPRINGDOC_USE_ROOT_PATH.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -48,6 +49,7 @@
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
import static org.springdoc.core.utils.Constants.DEFAULT_YAML_API_DOCS_ACTUATOR_PATH;
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
import static org.springdoc.core.utils.Constants.YAML;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;

Expand All @@ -56,6 +58,7 @@
* @author bnasslashen
*/
@RestControllerEndpoint(id = DEFAULT_API_DOCS_ACTUATOR_URL)
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
public class OpenApiActuatorResource extends OpenApiResource {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -51,13 +52,15 @@
import static org.springdoc.core.utils.Constants.API_DOCS_URL;
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_URL_YAML;
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;

/**
* The type Open api resource.
*
* @author bnasslahsen
*/
@RestController
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
public class OpenApiWebfluxResource extends OpenApiResource {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class SpringDocWebFluxConfiguration {
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
@ConditionalOnExpression("(${springdoc.use-management-port:false} == false ) and ${springdoc.enable-default-api-docs:true}")
@Lazy(false)
OpenApiWebfluxResource openApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
GenericResponseService responseBuilder, OperationService operationParser,
Expand Down Expand Up @@ -205,7 +205,7 @@ ActuatorProvider actuatorProvider(ServerProperties serverProperties,
*/
@Bean
@ConditionalOnMissingBean(MultipleOpenApiSupportConfiguration.class)
@ConditionalOnProperty(SPRINGDOC_USE_MANAGEMENT_PORT)
@ConditionalOnExpression("${springdoc.use-management-port:false} and ${springdoc.enable-default-api-docs:true}")
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@Lazy(false)
OpenApiActuatorResource actuatorOpenApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app190;

import org.junit.jupiter.api.Test;
import org.springdoc.core.utils.Constants;
import test.org.springdoc.api.AbstractCommonTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.context.annotation.ComponentScan;

import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;

@WebFluxTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=false")
public class SpringDocApp190Test extends AbstractCommonTest {

@SpringBootApplication
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app190" })
static class SpringDocTestApp {}

@Test
public void test_disable_default_api_docs() throws Exception {
webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
.expectStatus().isNotFound();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app190;

import org.junit.jupiter.api.Test;
import org.springdoc.core.utils.Constants;
import test.org.springdoc.api.AbstractCommonTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.context.annotation.ComponentScan;

import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;

@WebFluxTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=true")
public class SpringDocApp190bisTest extends AbstractCommonTest {

@SpringBootApplication
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app190" })
static class SpringDocTestApp {}

@Test
public void test_enable_default_api_docs() throws Exception {
webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
.expectStatus().isOk();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
import static org.springdoc.core.utils.Constants.DEFAULT_YAML_API_DOCS_ACTUATOR_PATH;
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
import static org.springdoc.core.utils.Constants.YAML;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;

Expand All @@ -54,6 +56,7 @@
* @author bnasslashen
*/
@RestControllerEndpoint(id = DEFAULT_API_DOCS_ACTUATOR_URL)
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
public class OpenApiActuatorResource extends OpenApiResource {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,22 @@
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springdoc.core.utils.Constants.API_DOCS_URL;
import static org.springdoc.core.utils.Constants.APPLICATION_OPENAPI_YAML;
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_URL_YAML;
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;

/**
* The type Open api resource.
* @author bnasslahsen
*/
@RestController
@ConditionalOnProperty(name = SPRINGDOC_ENABLE_DEFAULT_API_DOCS, havingValue = "true", matchIfMissing = true)
public class OpenApiWebMvcResource extends OpenApiResource {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class SpringDocWebMvcConfiguration {
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
@ConditionalOnExpression("(${springdoc.use-management-port:false} == false ) and ${springdoc.enable-default-api-docs:true}")
@Lazy(false)
OpenApiWebMvcResource openApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
GenericResponseService responseBuilder, OperationService operationParser,
Expand Down Expand Up @@ -233,7 +233,7 @@ ActuatorProvider actuatorProvider(ServerProperties serverProperties,
*/
@Bean
@ConditionalOnMissingBean(MultipleOpenApiSupportConfiguration.class)
@ConditionalOnProperty(SPRINGDOC_USE_MANAGEMENT_PORT)
@ConditionalOnExpression("${springdoc.use-management-port:false} and ${springdoc.enable-default-api-docs:true}")
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@Lazy(false)
OpenApiActuatorResource openApiActuatorResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.v30.app215;

import org.junit.jupiter.api.Test;
import org.springdoc.core.utils.Constants;
import test.org.springdoc.api.AbstractCommonTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.ComponentScan;

import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=false")
public class SpringDocApp215Test extends AbstractCommonTest {

@SpringBootApplication
@ComponentScan(basePackages = { "org.springdoc" })
static class SpringDocTestApp {}

@Test
public void test_disable_default_api_docs() throws Exception {
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isNotFound());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.v30.app215;

import org.junit.jupiter.api.Test;
import org.springdoc.core.utils.Constants;
import test.org.springdoc.api.AbstractCommonTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.ComponentScan;

import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLE_DEFAULT_API_DOCS;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(properties = SPRINGDOC_ENABLE_DEFAULT_API_DOCS+"=true")
public class SpringDocApp215bisTest extends AbstractCommonTest {

@SpringBootApplication
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.v30.app214" })
static class SpringDocTestApp {}

@Test
public void test_enable_default_api_docs() throws Exception {
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk());
}
}

0 comments on commit 737ecca

Please sign in to comment.