Skip to content

Commit c49419f

Browse files
author
bnasslahsen
committed
Syntax highlighting in Swagger-ui can't be configured in Springdoc. Fixes #877
1 parent 0575b08 commit c49419f

File tree

6 files changed

+245
-1
lines changed

6 files changed

+245
-1
lines changed

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,29 @@ public abstract class AbstractSwaggerUiConfigProperties {
152152
*/
153153
protected String urlsPrimaryName;
154154

155+
/**
156+
* The Syntax highlight.
157+
*/
158+
protected SyntaxHighlight syntaxHighlight;
159+
160+
/**
161+
* Gets syntax highlight.
162+
*
163+
* @return the syntax highlight
164+
*/
165+
public SyntaxHighlight getSyntaxHighlight() {
166+
return syntaxHighlight;
167+
}
168+
169+
/**
170+
* Sets syntax highlight.
171+
*
172+
* @param syntaxHighlight the syntax highlight
173+
*/
174+
public void setSyntaxHighlight(SyntaxHighlight syntaxHighlight) {
175+
this.syntaxHighlight = syntaxHighlight;
176+
}
177+
155178
/**
156179
* Gets urls.
157180
*
@@ -567,6 +590,58 @@ public void setUrlsPrimaryName(String urlsPrimaryName) {
567590
this.urlsPrimaryName = urlsPrimaryName;
568591
}
569592

593+
/**
594+
* The type Syntax highlight.
595+
*/
596+
public static class SyntaxHighlight{
597+
598+
/**
599+
* The Activated.
600+
*/
601+
private Boolean activated;
602+
603+
/**
604+
* The Theme.
605+
*/
606+
private String theme;
607+
608+
/**
609+
* Gets activated.
610+
*
611+
* @return the activated
612+
*/
613+
public Boolean getActivated() {
614+
return activated;
615+
}
616+
617+
/**
618+
* Sets activated.
619+
*
620+
* @param activated the activated
621+
*/
622+
public void setActivated(Boolean activated) {
623+
this.activated = activated;
624+
}
625+
626+
/**
627+
* Gets theme.
628+
*
629+
* @return the theme
630+
*/
631+
public String getTheme() {
632+
return theme;
633+
}
634+
635+
/**
636+
* Sets theme.
637+
*
638+
* @param theme the theme
639+
*/
640+
public void setTheme(String theme) {
641+
this.theme = theme;
642+
}
643+
}
644+
570645
/**
571646
* The type Swagger url.
572647
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
116116
this.urls = swaggerUiConfig.getUrls() == null ? new HashSet<>() : swaggerUiConfig.getUrls();
117117
this.urlsPrimaryName = swaggerUiConfig.getUrlsPrimaryName();
118118
this.groupsOrder = swaggerUiConfig.getGroupsOrder();
119+
this.syntaxHighlight = swaggerUiConfig.getSyntaxHighlight();
119120
}
120121

121122
/**

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected String overwriteSwaggerDefaultUrl(String html) {
119119
*/
120120
protected boolean hasDefaultTransformations() {
121121
boolean oauth2Configured = !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters());
122-
return oauth2Configured || swaggerUiConfig.isDisableSwaggerDefaultUrl() || swaggerUiConfig.isCsrfEnabled();
122+
return oauth2Configured || swaggerUiConfig.isDisableSwaggerDefaultUrl() || swaggerUiConfig.isCsrfEnabled() || swaggerUiConfig.getSyntaxHighlight() != null;
123123
}
124124

125125
/**
@@ -140,6 +140,9 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
140140
if (swaggerUiConfig.isCsrfEnabled()) {
141141
html = addCSRF(html);
142142
}
143+
if (swaggerUiConfig.getSyntaxHighlight() != null) {
144+
html = addSyntaxHighlight(html);
145+
}
143146
return html;
144147
}
145148

@@ -165,4 +168,24 @@ protected String addCSRF(String html) {
165168
stringBuilder.append("presets: [");
166169
return html.replace("presets: [", stringBuilder.toString());
167170
}
171+
172+
protected String addSyntaxHighlight(String html) {
173+
StringBuilder stringBuilder = new StringBuilder();
174+
stringBuilder.append("syntaxHighlight: {");
175+
if (swaggerUiConfig.getSyntaxHighlight().getActivated() != null) {
176+
stringBuilder.append("activated: ");
177+
stringBuilder.append(swaggerUiConfig.getSyntaxHighlight().getActivated());
178+
}
179+
if (StringUtils.isNotEmpty(swaggerUiConfig.getSyntaxHighlight().getTheme())) {
180+
if (swaggerUiConfig.getSyntaxHighlight().getActivated() != null)
181+
stringBuilder.append(" , ");
182+
stringBuilder.append("theme: \"");
183+
stringBuilder.append(swaggerUiConfig.getSyntaxHighlight().getTheme());
184+
stringBuilder.append("\"");
185+
}
186+
stringBuilder.append("},\n");
187+
stringBuilder.append("presets: [");
188+
return html.replace("presets: [", stringBuilder.toString());
189+
}
190+
168191
}
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.app12;
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.app12;
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+
@TestPropertySource(properties = { "springdoc.swagger-ui.syntaxHighlight.activated=false",
34+
"springdoc.swagger-ui.syntaxHighlight.theme=monokai" })
35+
public class SpringDocApp12Test extends AbstractSpringDocTest {
36+
37+
@Test
38+
public void transformed_index_with_oauth() throws Exception {
39+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
40+
String transformedIndex = mvcResult.getResponse().getContentAsString();
41+
assertTrue(transformedIndex.contains("Swagger UI"));
42+
assertEquals(this.getExpectedResult(), transformedIndex);
43+
}
44+
45+
@SpringBootApplication
46+
static class SpringDocTestApp {}
47+
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
38+
<script>
39+
window.onload = function() {
40+
// Begin Swagger UI call region
41+
const ui = SwaggerUIBundle({
42+
url: "https://petstore.swagger.io/v2/swagger.json",
43+
dom_id: '#swagger-ui',
44+
deepLinking: true,
45+
syntaxHighlight: {activated: false , theme: "monokai"},
46+
presets: [
47+
SwaggerUIBundle.presets.apis,
48+
SwaggerUIStandalonePreset
49+
],
50+
plugins: [
51+
SwaggerUIBundle.plugins.DownloadUrl
52+
],
53+
layout: "StandaloneLayout"
54+
})
55+
// End Swagger UI call region
56+
57+
window.ui = ui
58+
}
59+
</script>
60+
</body>
61+
</html>

0 commit comments

Comments
 (0)