Skip to content

Commit 1846049

Browse files
committed
Add support for api groups in Scalar #3158
1 parent 0423220 commit 1846049

15 files changed

Lines changed: 88 additions & 66 deletions

File tree

springdoc-openapi-starter-common/src/main/java/org/springdoc/scalar/AbstractScalarController.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
package org.springdoc.scalar;
2828

2929
import java.io.IOException;
30+
import java.util.List;
3031

3132
import com.scalar.maven.core.ScalarHtmlRenderer;
3233
import com.scalar.maven.core.ScalarProperties;
34+
import com.scalar.maven.core.config.ScalarSource;
3335
import io.swagger.v3.oas.annotations.Operation;
36+
import org.springdoc.core.properties.SpringDocConfigProperties;
3437

3538
import org.springframework.http.MediaType;
3639
import org.springframework.http.ResponseEntity;
40+
import org.springframework.util.CollectionUtils;
3741
import org.springframework.web.bind.annotation.GetMapping;
3842

3943
import static org.springdoc.scalar.ScalarConstants.SCALAR_DEFAULT_URL;
@@ -47,6 +51,11 @@
4751
*/
4852
public abstract class AbstractScalarController {
4953

54+
/**
55+
* The Spring doc config properties.
56+
*/
57+
protected final SpringDocConfigProperties springDocConfigProperties;
58+
5059
/**
5160
* The Scalar properties.
5261
*/
@@ -61,16 +70,18 @@ public abstract class AbstractScalarController {
6170
* Instantiates a new Abstract scalar controller.
6271
*
6372
* @param scalarProperties the scalar properties
73+
* @param springDocConfigProperties the spring doc config properties
6474
*/
65-
protected AbstractScalarController(ScalarProperties scalarProperties) {
75+
protected AbstractScalarController(ScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties) {
76+
this.springDocConfigProperties = springDocConfigProperties;
6677
this.scalarProperties = scalarProperties;
6778
this.originalScalarUrl = scalarProperties.getUrl();
6879
}
6980

7081
/**
7182
* Gets scalar js.
7283
*
73-
* @return the scalar js
84+
* @return the scalar js
7485
* @throws IOException the io exception
7586
*/
7687
@GetMapping({ DEFAULT_PATH_SEPARATOR + SCALAR_JS_FILENAME, SCALAR_JS_FILENAME })
@@ -85,17 +96,26 @@ public ResponseEntity<byte[]> getScalarJs() throws IOException {
8596
/**
8697
* Gets docs.
8798
*
88-
* @param requestUrl the request url
89-
* @param apiDocsPath the api docs path
90-
* @param scalarPath the scalar path
91-
* @return the docs
99+
* @param requestUrl the request url
100+
* @param apiDocsPath the api docs path
101+
* @param scalarPath the scalar path
102+
* @return the docs
92103
* @throws IOException the io exception
93104
*/
94105
protected ResponseEntity<String> getDocs(String requestUrl, String apiDocsPath, String scalarPath) throws IOException {
95106
ScalarProperties configuredProperties = configureProperties(scalarProperties, requestUrl, apiDocsPath);
107+
String url = configuredProperties.getUrl();
108+
List<ScalarSource> scalarSources = springDocConfigProperties.getGroupConfigs().stream()
109+
.map(groupConfig -> new ScalarSource(url + DEFAULT_PATH_SEPARATOR + groupConfig.getGroup(), groupConfig.getDisplayName(), null, false)).toList();
110+
111+
if(!CollectionUtils.isEmpty(scalarSources)) {
112+
scalarProperties.setSources(scalarSources);
113+
scalarProperties.setUrl(null);
114+
}
115+
96116
String html = ScalarHtmlRenderer.render(configuredProperties);
97117
String bundleUrl = buildJsBundleUrl(requestUrl, scalarPath);
98-
html = html.replaceAll("(<script[^>]*\\s+src\\s*=\\s*\")([^\"]*)(\")", "$1"+bundleUrl+"$3");
118+
html = html.replaceAll("(<script[^>]*\\s+src\\s*=\\s*\")([^\"]*)(\")", "$1" + bundleUrl + "$3");
99119
return ResponseEntity.ok()
100120
.contentType(MediaType.TEXT_HTML)
101121
.body(html);
@@ -104,12 +124,12 @@ protected ResponseEntity<String> getDocs(String requestUrl, String apiDocsPath,
104124
/**
105125
* Configure properties scalar properties.
106126
*
107-
* @param properties the properties
108-
* @param requestUrl the request url
109-
* @param apiDocsPath the api docs path
127+
* @param properties the properties
128+
* @param requestUrl the request url
129+
* @param apiDocsPath the api docs path
110130
* @return the scalar properties
111131
*/
112-
private ScalarProperties configureProperties(ScalarProperties properties, String requestUrl, String apiDocsPath ) {
132+
private ScalarProperties configureProperties(ScalarProperties properties, String requestUrl, String apiDocsPath) {
113133
String url = buildApiDocsUrl(requestUrl, apiDocsPath);
114134
properties.setUrl(url);
115135
return properties;
@@ -118,8 +138,8 @@ private ScalarProperties configureProperties(ScalarProperties properties, String
118138
/**
119139
* Build js bundle url string.
120140
*
121-
* @param requestUrl the request url
122-
* @param scalarPath the scalar path
141+
* @param requestUrl the request url
142+
* @param scalarPath the scalar path
123143
* @return the string
124144
*/
125145
private String buildJsBundleUrl(String requestUrl, String scalarPath) {
@@ -136,8 +156,8 @@ private String buildJsBundleUrl(String requestUrl, String scalarPath) {
136156
/**
137157
* Gets api docs url.
138158
*
139-
* @param requestUrl the request url
140-
* @param apiDocsPath the api docs path
159+
* @param requestUrl the request url
160+
* @param apiDocsPath the api docs path
141161
* @return the api docs url
142162
*/
143163
private String buildApiDocsUrl(String requestUrl, String apiDocsPath) {

springdoc-openapi-starter-webflux-scalar/src/main/java/org/springdoc/webflux/scalar/ScalarActuatorController.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.scalar.maven.webflux.SpringBootScalarProperties;
3232
import io.swagger.v3.oas.annotations.Operation;
33+
import org.springdoc.core.properties.SpringDocConfigProperties;
3334
import org.springdoc.scalar.AbstractScalarController;
3435

3536
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
@@ -56,14 +57,22 @@ public class ScalarActuatorController extends AbstractScalarController {
5657
/**
5758
* Instantiates a new Scalar actuator controller.
5859
*
59-
* @param scalarProperties the scalar properties
60+
* @param scalarProperties the scalar properties
61+
* @param springDocConfigProperties the spring doc config properties
6062
* @param webEndpointProperties the web endpoint properties
6163
*/
62-
public ScalarActuatorController(SpringBootScalarProperties scalarProperties, WebEndpointProperties webEndpointProperties) {
63-
super(scalarProperties);
64+
public ScalarActuatorController(SpringBootScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties, WebEndpointProperties webEndpointProperties) {
65+
super(scalarProperties, springDocConfigProperties);
6466
this.webEndpointProperties = webEndpointProperties;
6567
}
6668

69+
/**
70+
* Gets docs.
71+
*
72+
* @param serverHttpRequest the server http request
73+
* @return the docs
74+
* @throws IOException the io exception
75+
*/
6776
@Operation(hidden = true)
6877
@GetMapping(DEFAULT_PATH_SEPARATOR)
6978
public ResponseEntity<String> getDocs(ServerHttpRequest serverHttpRequest) throws IOException {

springdoc-openapi-starter-webflux-scalar/src/main/java/org/springdoc/webflux/scalar/ScalarConfiguration.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
/**
5555
* The type Scalar configuration.
5656
*
57-
* @author bnasslahsen
57+
* @author bnasslahsen
5858
*/
5959
@Lazy(false)
6060
@Configuration(proxyBeanMethods = false)
@@ -67,9 +67,9 @@ public class ScalarConfiguration {
6767
/**
6868
* Scalar web mvc controller scalar web mvc controller.
6969
*
70-
* @param scalarProperties the scalar properties
71-
* @param springDocConfigProperties the spring doc config properties
72-
* @return the scalar web mvc controller
70+
* @param scalarProperties the scalar properties
71+
* @param springDocConfigProperties the spring doc config properties
72+
* @return the scalar web mvc controller
7373
*/
7474
@Bean
7575
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
@@ -82,7 +82,7 @@ ScalarWebFluxController scalarWebMvcController(SpringBootScalarProperties scalar
8282
/**
8383
* Forwarded header transformer forwarded header transformer.
8484
*
85-
* @return the forwarded header transformer
85+
* @return the forwarded header transformer
8686
*/
8787
@Bean
8888
@ConditionalOnMissingBean
@@ -94,8 +94,8 @@ ForwardedHeaderTransformer forwardedHeaderTransformer() {
9494
/**
9595
* Spring doc app initializer spring doc app initializer.
9696
*
97-
* @param scalarProperties the spring doc config properties
98-
* @return the spring doc app initializer
97+
* @param scalarProperties the spring doc config properties
98+
* @return the spring doc app initializer
9999
*/
100100
@Bean
101101
@ConditionalOnMissingBean(name = "springDocScalarInitializer")
@@ -116,21 +116,23 @@ static class SwaggerActuatorWelcomeConfiguration {
116116
/**
117117
* Scalar actuator controller scalar actuator controller.
118118
*
119-
* @param properties the properties
120-
* @param webEndpointProperties the web endpoint properties
121-
* @return the scalar actuator controller
119+
* @param properties the properties
120+
* @param springDocConfigProperties the spring doc config properties
121+
* @param webEndpointProperties the web endpoint properties
122+
* @return the scalar actuator controller
122123
*/
123124
@Bean
124125
@ConditionalOnMissingBean
125126
@Lazy(false)
126-
ScalarActuatorController scalarActuatorController(SpringBootScalarProperties properties, WebEndpointProperties webEndpointProperties) {
127-
return new ScalarActuatorController(properties, webEndpointProperties);
127+
ScalarActuatorController scalarActuatorController(SpringBootScalarProperties properties, SpringDocConfigProperties springDocConfigProperties, WebEndpointProperties webEndpointProperties) {
128+
return new ScalarActuatorController(properties, springDocConfigProperties, webEndpointProperties);
128129
}
129130

130131
/**
131132
* Spring doc scalar initializer spring doc app initializer.
132133
*
133-
* @return the spring doc app initializer
134+
* @param scalarProperties the scalar properties
135+
* @return the spring doc app initializer
134136
*/
135137
@Bean
136138
@ConditionalOnMissingBean(name = "springDocScalarInitializer")

springdoc-openapi-starter-webflux-scalar/src/main/java/org/springdoc/webflux/scalar/ScalarWebFluxController.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,27 @@
4343
/**
4444
* The type Scalar web mvc controller.
4545
*
46-
* @author bnasslahsen
46+
* @author bnasslahsen
4747
*/
4848
@Controller
4949
@RequestMapping("${scalar.path:" + SCALAR_DEFAULT_PATH + "}")
5050
public class ScalarWebFluxController extends AbstractScalarController {
5151

52-
/**
53-
* The Spring doc config properties.
54-
*/
55-
private final SpringDocConfigProperties springDocConfigProperties;
56-
5752
/**
5853
* Instantiates a new Scalar web mvc controller.
5954
*
60-
* @param scalarProperties the scalar properties
55+
* @param scalarProperties the scalar properties
6156
* @param springDocConfigProperties the spring doc config properties
6257
*/
63-
public ScalarWebFluxController(SpringBootScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties) {
64-
super(scalarProperties);
65-
this.springDocConfigProperties = springDocConfigProperties;
58+
public ScalarWebFluxController(SpringBootScalarProperties scalarProperties,SpringDocConfigProperties springDocConfigProperties) {
59+
super(scalarProperties, springDocConfigProperties);
6660
}
6761

6862
/**
6963
* Gets docs.
7064
*
71-
* @param serverHttpRequest the server http request
72-
* @return the docs
65+
* @param serverHttpRequest the server http request
66+
* @return the docs
7367
* @throws IOException the io exception
7468
*/
7569
@GetMapping

springdoc-openapi-starter-webflux-scalar/src/test/resources/results/app5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- Initialize the Scalar API Reference -->
1818
<script>
19-
Scalar.createApiReference('#app', {"url":"http://localhost:9593/application/openapi","showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
19+
Scalar.createApiReference('#app', {"showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"sources":[{"url":"http://localhost:9593/application/openapi/users","title":"users","default":false}],"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
2020
</script>
2121
</body>
2222
</html>

springdoc-openapi-starter-webflux-scalar/src/test/resources/results/app7

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- Initialize the Scalar API Reference -->
1818
<script>
19-
Scalar.createApiReference('#app', {"url":"http://localhost/v3/api-docs","showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
19+
Scalar.createApiReference('#app', {"showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"sources":[{"url":"http://localhost/v3/api-docs/springdocDefault","title":"springdocDefault","default":false},{"url":"http://localhost/v3/api-docs/x-actuator","title":"x-actuator","default":false}],"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
2020
</script>
2121
</body>
2222
</html>

springdoc-openapi-starter-webflux-scalar/src/test/resources/results/app8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- Initialize the Scalar API Reference -->
1818
<script>
19-
Scalar.createApiReference('#app', {"url":"http://localhost/v3/api-docs","showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
19+
Scalar.createApiReference('#app', {"showSidebar":true,"hideModels":false,"hideTestRequestButton":false,"darkMode":false,"hideDarkModeToggle":false,"withDefaultFonts":true,"defaultOpenAllTags":false,"expandAllModelSections":false,"expandAllResponses":false,"hideSearch":false,"theme":"default","layout":"modern","favicon":"favicon.svg","hideClientButton":false,"sources":[{"url":"http://localhost/v3/api-docs/users","title":"users","default":false}],"persistAuth":false,"telemetry":true,"documentDownloadType":"both","orderRequiredPropertiesFirst":true,"showOperationId":false})
2020
</script>
2121
</body>
2222
</html>

springdoc-openapi-starter-webmvc-scalar/src/main/java/org/springdoc/webmvc/scalar/ScalarActuatorController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.scalar.maven.webmvc.SpringBootScalarProperties;
3232
import io.swagger.v3.oas.annotations.Operation;
3333
import jakarta.servlet.http.HttpServletRequest;
34+
import org.springdoc.core.properties.SpringDocConfigProperties;
3435
import org.springdoc.scalar.AbstractScalarController;
3536

3637
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
@@ -56,11 +57,12 @@ public class ScalarActuatorController extends AbstractScalarController {
5657
/**
5758
* Instantiates a new Scalar actuator controller.
5859
*
59-
* @param scalarProperties the scalar properties
60+
* @param scalarProperties the scalar properties
61+
* @param springDocConfigProperties the spring doc config properties
6062
* @param webEndpointProperties the web endpoint properties
6163
*/
62-
public ScalarActuatorController(SpringBootScalarProperties scalarProperties, WebEndpointProperties webEndpointProperties) {
63-
super(scalarProperties);
64+
public ScalarActuatorController(SpringBootScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties, WebEndpointProperties webEndpointProperties) {
65+
super(scalarProperties, springDocConfigProperties);
6466
this.webEndpointProperties = webEndpointProperties;
6567
}
6668

springdoc-openapi-starter-webmvc-scalar/src/main/java/org/springdoc/webmvc/scalar/ScalarConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ static class SwaggerActuatorWelcomeConfiguration {
117117
/**
118118
* Scalar actuator controller scalar actuator controller.
119119
*
120-
* @param properties the properties
121-
* @param webEndpointProperties the web endpoint properties
120+
* @param properties the properties
121+
* @param springDocConfigProperties the spring doc config properties
122+
* @param webEndpointProperties the web endpoint properties
122123
* @return the scalar actuator controller
123124
*/
124125
@Bean
125126
@ConditionalOnMissingBean
126127
@Lazy(false)
127-
ScalarActuatorController scalarActuatorController(SpringBootScalarProperties properties, WebEndpointProperties webEndpointProperties) {
128-
return new ScalarActuatorController(properties,webEndpointProperties);
128+
ScalarActuatorController scalarActuatorController(SpringBootScalarProperties properties, SpringDocConfigProperties springDocConfigProperties, WebEndpointProperties webEndpointProperties) {
129+
return new ScalarActuatorController(properties,springDocConfigProperties, webEndpointProperties);
129130
}
130131

131132
/**

springdoc-openapi-starter-webmvc-scalar/src/main/java/org/springdoc/webmvc/scalar/ScalarWebMvcController.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@
4949
@RequestMapping("${scalar.path:" + SCALAR_DEFAULT_PATH + "}")
5050
public class ScalarWebMvcController extends AbstractScalarController {
5151

52-
/**
53-
* The Spring doc config properties.
54-
*/
55-
private final SpringDocConfigProperties springDocConfigProperties;
56-
5752
/**
5853
* Instantiates a new Scalar web mvc controller.
5954
*
6055
* @param scalarProperties the scalar properties
6156
* @param springDocConfigProperties the spring doc config properties
6257
*/
6358
public ScalarWebMvcController(SpringBootScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties) {
64-
super(scalarProperties);
65-
this.springDocConfigProperties = springDocConfigProperties;
59+
super(scalarProperties, springDocConfigProperties);
6660
}
6761

6862
/**
@@ -75,7 +69,7 @@ public ScalarWebMvcController(SpringBootScalarProperties scalarProperties, Sprin
7569
@GetMapping
7670
public ResponseEntity<String> getDocs(HttpServletRequest request) throws IOException {
7771
String apiDocsPath = springDocConfigProperties.getApiDocs().getPath();
78-
String requestUrl =request.getRequestURL().toString();
72+
String requestUrl = request.getRequestURL().toString();
7973
String scalarPath = scalarProperties.getPath();
8074
return getDocs(requestUrl, apiDocsPath, scalarPath);
8175
}

0 commit comments

Comments
 (0)