Skip to content

Commit 90b7d42

Browse files
committed
Merge branch 'hismaili-fix/issue907'
2 parents 52a0a63 + 6dec561 commit 90b7d42

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import org.springframework.beans.factory.config.BeanDefinition;
6060
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
61+
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
6162
import org.springframework.context.ApplicationContext;
6263
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
6364
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -73,6 +74,7 @@
7374
import static org.springdoc.core.Constants.DEFAULT_SERVER_DESCRIPTION;
7475
import static org.springdoc.core.Constants.DEFAULT_TITLE;
7576
import static org.springdoc.core.Constants.DEFAULT_VERSION;
77+
import static org.springdoc.core.SpringDocUtils.getConfig;
7678

7779
/**
7880
* The type Open api builder.
@@ -213,11 +215,23 @@ else if (calculatedOpenAPI.getInfo() == null) {
213215
this.mappingsMap.putAll(context.getBeansWithAnnotation(RequestMapping.class));
214216
this.mappingsMap.putAll(context.getBeansWithAnnotation(Controller.class));
215217

218+
initializeHiddenRestController();
219+
216220
// add security schemes
217221
this.calculateSecuritySchemes(calculatedOpenAPI.getComponents());
218222
openApiBuilderCustomisers.ifPresent(customisers -> customisers.forEach(customiser -> customiser.customise(this)));
219223
}
220224

225+
private void initializeHiddenRestController() {
226+
getConfig().addHiddenRestControllers(BasicErrorController.class);
227+
List<Class<?>> hiddenRestControllers = this.mappingsMap.entrySet().parallelStream()
228+
.filter(controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(),
229+
Hidden.class) != null)).map(controller -> controller.getValue().getClass())
230+
.collect(Collectors.toList());
231+
if(!CollectionUtils.isEmpty(hiddenRestControllers))
232+
getConfig().addHiddenRestControllers(hiddenRestControllers.toArray(new Class<?>[hiddenRestControllers.size()]));
233+
}
234+
221235
/**
222236
* Update servers open api.
223237
*
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.app143;
20+
21+
import io.swagger.v3.oas.annotations.Hidden;
22+
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
@RestController
27+
@Hidden
28+
public class HelloHiddenController {
29+
30+
@GetMapping("/testA")
31+
public void testA(String hello) {
32+
}
33+
34+
@GetMapping("/testB")
35+
public void testB(String hello) {
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.app143;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
/**
27+
* Test issue 907 fix.
28+
* Hidden controller showing up in swagger UI when springdoc.show-actuator is enabled
29+
*/
30+
@TestPropertySource(properties = "springdoc.show-actuator=true")
31+
public class SpringDocApp143Test extends AbstractSpringDocTest {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
36+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "Actuator",
16+
"description": "Monitor and interact",
17+
"externalDocs": {
18+
"description": "Spring Boot Actuator Web API Documentation",
19+
"url": "https://docs.spring.io/spring-boot/docs/current/actuator-api/html/"
20+
}
21+
}
22+
],
23+
"paths": {
24+
"/actuator": {
25+
"get": {
26+
"tags": [
27+
"Actuator"
28+
],
29+
"summary": "Actuator root web endpoint",
30+
"operationId": "links_0",
31+
"responses": {
32+
"200": {
33+
"description": "OK",
34+
"content": {
35+
"*/*": {
36+
"schema": {
37+
"type": "object",
38+
"additionalProperties": {
39+
"type": "object",
40+
"additionalProperties": {
41+
"$ref": "#/components/schemas/Link"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"/actuator/health": {
52+
"get": {
53+
"tags": [
54+
"Actuator"
55+
],
56+
"summary": "Actuator web endpoint 'health'",
57+
"operationId": "handle_2",
58+
"responses": {
59+
"200": {
60+
"description": "OK",
61+
"content": {
62+
"*/*": {
63+
"schema": {
64+
"type": "object"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
},
72+
"/actuator/health/**": {
73+
"get": {
74+
"tags": [
75+
"Actuator"
76+
],
77+
"summary": "Actuator web endpoint 'health-path'",
78+
"operationId": "handle_3",
79+
"responses": {
80+
"200": {
81+
"description": "OK",
82+
"content": {
83+
"*/*": {
84+
"schema": {
85+
"type": "object"
86+
}
87+
}
88+
}
89+
}
90+
}
91+
}
92+
},
93+
"/actuator/info": {
94+
"get": {
95+
"tags": [
96+
"Actuator"
97+
],
98+
"summary": "Actuator web endpoint 'info'",
99+
"operationId": "handle_1",
100+
"responses": {
101+
"200": {
102+
"description": "OK",
103+
"content": {
104+
"*/*": {
105+
"schema": {
106+
"type": "object"
107+
}
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
114+
},
115+
"components": {
116+
"schemas": {
117+
"Link": {
118+
"type": "object",
119+
"properties": {
120+
"href": {
121+
"type": "string"
122+
},
123+
"templated": {
124+
"type": "boolean"
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)