Skip to content

Commit 330a81e

Browse files
committed
added the skipTags query param for the endpoint /springfox-loader/api-docs
1 parent e5a8c16 commit 330a81e

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ __Application properties__
126126
* springfox.license.url
127127
* springfox.activeProfiles - _Enable springfox for the configured profiles. If not set, all profiles loads springfox. Default is all profiles._
128128
* springfox.swagger-ui-base-path
129-
* springfox.endpoints - _Enables springfox-loader endpoints, `/springfox-loader/api-docs`. Is [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) enabled._
129+
* springfox.endpoints - _Enables springfox-loader endpoints, see [Springfox loader endpoints](#springfox-loader-endpoints) for more details_
130130

131131
### Swagger UI
132132

@@ -150,6 +150,13 @@ public void init() {
150150
}
151151
```
152152

153+
### Springfox loader endpoints
154+
155+
`GET /springfox-loader/api-docs`
156+
157+
Returns the swagger api-docs with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) enabled.
158+
Also has the option to remove the tags from the json response by adding `?skipTags=true` as a query param.
159+
153160
### References
154161
* [Springfox Reference Documentation](http://springfox.github.io/springfox/docs/current/)
155162
* [Swagger Core Annotations](https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X)

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<lombok.version>1.16.16</lombok.version>
1818
<reflections.version>0.9.11</reflections.version>
1919
<guava.version>22.0</guava.version>
20+
<jsonpath.version>2.2.0</jsonpath.version>
2021

2122
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2223
<java.version>1.8</java.version>
@@ -72,6 +73,11 @@
7273
<artifactId>guava</artifactId>
7374
<version>${guava.version}</version>
7475
</dependency>
76+
<dependency>
77+
<groupId>com.jayway.jsonpath</groupId>
78+
<artifactId>json-path</artifactId>
79+
<version>${jsonpath.version}</version>
80+
</dependency>
7581
<dependency>
7682
<groupId>org.projectlombok</groupId>
7783
<artifactId>lombok</artifactId>

src/main/java/com/github/springfox/loader/controller/SpringfoxLoaderController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.springfox.loader.SpringfoxLoaderProps;
44
import com.google.common.collect.Lists;
5+
import com.jayway.jsonpath.JsonPath;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
78
import org.springframework.http.HttpEntity;
@@ -10,6 +11,7 @@
1011
import org.springframework.http.ResponseEntity;
1112
import org.springframework.web.bind.annotation.GetMapping;
1213
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestParam;
1315
import org.springframework.web.bind.annotation.RestController;
1416
import org.springframework.web.client.RestTemplate;
1517
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -33,9 +35,15 @@ public SpringfoxLoaderController() {
3335
}
3436

3537
@GetMapping("/api-docs")
36-
public ResponseEntity getApiDocs(HttpServletRequest request) throws IOException {
38+
public ResponseEntity getApiDocs(HttpServletRequest request, @RequestParam(required = false) Boolean skipTags) throws IOException {
3739
UriComponents uri = ServletUriComponentsBuilder.fromServletMapping(request).path("/v2/api-docs").build();
3840
ResponseEntity<String> response = restTemplate.exchange(uri.toString(), HttpMethod.GET, new HttpEntity<>(null), String.class);
39-
return new ResponseEntity<>(response.getBody(), headers, response.getStatusCode());
41+
42+
String json = response.getBody();
43+
if (skipTags != null && skipTags) {
44+
json = JsonPath.parse(json).set("$.tags", new String[]{}).jsonString();
45+
}
46+
47+
return new ResponseEntity<>(json, headers, response.getStatusCode());
4048
}
4149
}

src/test/groovy/com/github/springfox/loader/controller/SpringfoxLoaderControllerSpec.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders
88
import org.springframework.web.client.RestTemplate
99
import spock.lang.Specification
1010

11+
import static org.hamcrest.Matchers.hasSize
1112
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
1214
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
1315

1416
class SpringfoxLoaderControllerSpec extends Specification {
@@ -30,4 +32,14 @@ class SpringfoxLoaderControllerSpec extends Specification {
3032
1 * restTemplate.exchange('http://localhost/v2/api-docs', HttpMethod.GET, _ as HttpEntity, String) >> ResponseEntity.ok('')
3133
response.andExpect(status().isOk())
3234
}
35+
36+
def "GET api-docs and skip tags"() {
37+
when:
38+
def response = mockMvc.perform(get('/springfox-loader/api-docs').param('skipTags', 'true'))
39+
40+
then:
41+
1 * restTemplate.exchange('http://localhost/v2/api-docs', HttpMethod.GET, _ as HttpEntity, String) >> ResponseEntity.ok('{ "tags": [ "test123", "test234" ] }')
42+
response.andExpect(status().isOk())
43+
.andExpect(jsonPath('$.tags', hasSize(0)))
44+
}
3345
}

0 commit comments

Comments
 (0)