Skip to content

Commit 2f7c4c6

Browse files
author
bnasslahsen
committed
Ability to disable swagger-ui default petstore url. Fixes #714.
1 parent ab0daf3 commit 2f7c4c6

File tree

19 files changed

+677
-11
lines changed

19 files changed

+677
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public final class Constants {
112112

113113
public static final String GRACEFUL_EXCEPTION_OCCURRED = "Graceful exception occurred";
114114

115+
public static final String SWAGGER_UI_DEFAULT_URL = "https://petstore.swagger.io/v2/swagger.json";
116+
115117
private Constants() {
116118
super();
117119
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public class SwaggerUiConfigProperties {
167167

168168
private String urlsPrimaryName;
169169

170+
private boolean disableSwaggerDefaultUrl;
171+
170172
public void addGroup(String group) {
171173
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
172174
urls.add(swaggerUrl);
@@ -375,6 +377,14 @@ public void setUrl(String url) {
375377
this.url = url;
376378
}
377379

380+
public boolean isDisableSwaggerDefaultUrl() {
381+
return disableSwaggerDefaultUrl;
382+
}
383+
384+
public void setDisableSwaggerDefaultUrl(boolean disableSwaggerDefaultUrl) {
385+
this.disableSwaggerDefaultUrl = disableSwaggerDefaultUrl;
386+
}
387+
378388
public boolean isValidUrl(String url) {
379389
try {
380390
new URL(url).toURI();

springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerIndexTransformer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
import com.fasterxml.jackson.core.JsonProcessingException;
2929
import com.fasterxml.jackson.databind.ObjectMapper;
30+
import org.apache.commons.lang3.StringUtils;
31+
import org.springdoc.core.Constants;
32+
import org.springdoc.core.SwaggerUiConfigProperties;
3033
import org.springdoc.core.SwaggerUiOAuthProperties;
3134

3235
public class AbstractSwaggerIndexTransformer {
@@ -35,7 +38,10 @@ public class AbstractSwaggerIndexTransformer {
3538

3639
protected ObjectMapper objectMapper;
3740

38-
public AbstractSwaggerIndexTransformer(SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
41+
protected SwaggerUiConfigProperties swaggerUiConfig;
42+
43+
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
44+
this.swaggerUiConfig = swaggerUiConfig;
3945
this.swaggerUiOAuthProperties = swaggerUiOAuthProperties;
4046
this.objectMapper = objectMapper;
4147
}
@@ -59,4 +65,8 @@ protected String readFullyAsString(InputStream inputStream)
5965
}
6066
return baos.toString(StandardCharsets.UTF_8.name());
6167
}
68+
69+
protected String overwriteSwaggerDefaultUrl(String html) {
70+
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
71+
}
6272
}

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
4848

4949
@Bean
5050
@ConditionalOnMissingBean
51-
SwaggerIndexTransformer indexPageTransformer(SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
52-
return new SwaggerIndexTransformer(swaggerUiOAuthProperties, objectMapper);
51+
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
52+
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
5353
}
5454

5555
@Bean

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerIndexTransformer.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.http.HttpServletRequest;
2626

2727
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.springdoc.core.SwaggerUiConfigProperties;
2829
import org.springdoc.core.SwaggerUiOAuthProperties;
2930
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
3031

@@ -37,18 +38,29 @@
3738

3839
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
3940

40-
public SwaggerIndexTransformer(SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
41-
super(swaggerUiOAuthProperties, objectMapper);
41+
public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
42+
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
4243
}
4344

4445
@Override
4546
public Resource transform(HttpServletRequest request, Resource resource,
4647
ResourceTransformerChain transformerChain) throws IOException {
4748
final AntPathMatcher antPathMatcher = new AntPathMatcher();
4849
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
49-
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
50+
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
5051
String html = readFullyAsString(resource.getInputStream());
5152
html = addInitOauth(html);
53+
html = overwriteSwaggerDefaultUrl(html);
54+
return new TransformedResource(resource, html.getBytes());
55+
}
56+
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
57+
String html = readFullyAsString(resource.getInputStream());
58+
html = addInitOauth(html);
59+
return new TransformedResource(resource, html.getBytes());
60+
}
61+
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
62+
String html = readFullyAsString(resource.getInputStream());
63+
html = overwriteSwaggerDefaultUrl(html);
5264
return new TransformedResource(resource, html.getBytes());
5365
}
5466
else
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.ui.app10;
20+
21+
import javax.validation.Valid;
22+
import javax.validation.constraints.Size;
23+
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RequestParam;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@RestController
29+
public class HelloController {
30+
31+
@GetMapping(value = "/persons")
32+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
33+
34+
}
35+
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.ui.app10;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.test.context.TestPropertySource;
26+
import org.springframework.test.web.servlet.MvcResult;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32+
33+
34+
@TestPropertySource(properties = "springdoc.swagger-ui.disable-swagger-default-url=true" )
35+
public class SpringDocApp10Test extends AbstractSpringDocTest {
36+
37+
@Test
38+
public void shouldDisplaySwaggerUiPage() throws Exception {
39+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
40+
String contentAsString = mvcResult.getResponse().getContentAsString();
41+
assertTrue(contentAsString.contains("Swagger UI"));
42+
}
43+
44+
@Test
45+
public void originalIndex() throws Exception {
46+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
47+
String transformedIndex = mvcResult.getResponse().getContentAsString();
48+
assertTrue(transformedIndex.contains("Swagger UI"));
49+
assertEquals(getExpectedResult(), transformedIndex);
50+
}
51+
52+
@SpringBootApplication
53+
static class SpringDocTestApp {}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app9;
25+
26+
import javax.validation.Valid;
27+
import javax.validation.constraints.Size;
28+
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
@RestController
34+
public class HelloController {
35+
36+
@GetMapping(value = "/persons")
37+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
38+
39+
}
40+
41+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app9;
25+
26+
import org.junit.jupiter.api.Test;
27+
import test.org.springdoc.ui.AbstractSpringDocTest;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.test.context.TestPropertySource;
31+
import org.springframework.test.web.servlet.MvcResult;
32+
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
37+
38+
@TestPropertySource(properties = { "springdoc.swagger-ui.oauth.clientId=myClientId",
39+
"springdoc.swagger-ui.oauth.additionalQueryStringParams.test1=test1",
40+
"springdoc.swagger-ui.oauth.additionalQueryStringParams.test2=test2",
41+
"springdoc.swagger-ui.disable-swagger-default-url=true" })
42+
public class SpringDocApp9Test extends AbstractSpringDocTest {
43+
44+
@Test
45+
public void transformed_index_with_oauth() throws Exception {
46+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
47+
String transformedIndex = mvcResult.getResponse().getContentAsString();
48+
assertTrue(transformedIndex.contains("Swagger UI"));
49+
assertEquals(this.getExpectedResult(), transformedIndex);
50+
}
51+
52+
@SpringBootApplication
53+
static class SpringDocTestApp {}
54+
55+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!-- HTML for static distribution bundle build -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Swagger UI</title>
7+
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
8+
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
9+
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
10+
<style>
11+
html
12+
{
13+
box-sizing: border-box;
14+
overflow: -moz-scrollbars-vertical;
15+
overflow-y: scroll;
16+
}
17+
18+
*,
19+
*:before,
20+
*:after
21+
{
22+
box-sizing: inherit;
23+
}
24+
25+
body
26+
{
27+
margin:0;
28+
background: #fafafa;
29+
}
30+
</style>
31+
</head>
32+
33+
<body>
34+
<div id="swagger-ui"></div>
35+
36+
<script src="./swagger-ui-bundle.js"> </script>
37+
<script src="./swagger-ui-standalone-preset.js"> </script>
38+
<script>
39+
window.onload = function() {
40+
// Begin Swagger UI call region
41+
const ui = SwaggerUIBundle({
42+
url: "",
43+
dom_id: '#swagger-ui',
44+
deepLinking: true,
45+
presets: [
46+
SwaggerUIBundle.presets.apis,
47+
SwaggerUIStandalonePreset
48+
],
49+
plugins: [
50+
SwaggerUIBundle.plugins.DownloadUrl
51+
],
52+
layout: "StandaloneLayout"
53+
})
54+
// End Swagger UI call region
55+
56+
window.ui = ui
57+
}
58+
</script>
59+
</body>
60+
</html>

0 commit comments

Comments
 (0)