Skip to content

Commit 491b853

Browse files
committed
Add support for api groups in Scalar #3158
1 parent 5cad78b commit 491b853

File tree

15 files changed

+85
-62
lines changed

15 files changed

+85
-62
lines changed

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

Lines changed: 34 additions & 13 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,12 @@
4751
*/
4852
public abstract class AbstractScalarController {
4953

54+
55+
/**
56+
* The Spring doc config properties.
57+
*/
58+
protected final SpringDocConfigProperties springDocConfigProperties;
59+
5060
/**
5161
* The Scalar properties.
5262
*/
@@ -61,16 +71,18 @@ public abstract class AbstractScalarController {
6171
* Instantiates a new Abstract scalar controller.
6272
*
6373
* @param scalarProperties the scalar properties
74+
* @param springDocConfigProperties the spring doc config properties
6475
*/
65-
protected AbstractScalarController(ScalarProperties scalarProperties) {
76+
protected AbstractScalarController(ScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties) {
77+
this.springDocConfigProperties = springDocConfigProperties;
6678
this.scalarProperties = scalarProperties;
6779
this.originalScalarUrl = scalarProperties.getUrl();
6880
}
6981

7082
/**
7183
* Gets scalar js.
7284
*
73-
* @return the scalar js
85+
* @return the scalar js
7486
* @throws IOException the io exception
7587
*/
7688
@GetMapping({ DEFAULT_PATH_SEPARATOR + SCALAR_JS_FILENAME, SCALAR_JS_FILENAME })
@@ -85,14 +97,23 @@ public ResponseEntity<byte[]> getScalarJs() throws IOException {
8597
/**
8698
* Gets docs.
8799
*
88-
* @param requestUrl the request url
89-
* @param apiDocsPath the api docs path
90-
* @param scalarPath the scalar path
91-
* @return the docs
100+
* @param requestUrl the request url
101+
* @param apiDocsPath the api docs path
102+
* @param scalarPath the scalar path
103+
* @return the docs
92104
* @throws IOException the io exception
93105
*/
94106
protected ResponseEntity<String> getDocs(String requestUrl, String apiDocsPath, String scalarPath) throws IOException {
95107
ScalarProperties configuredProperties = configureProperties(scalarProperties, requestUrl, apiDocsPath);
108+
String url = configuredProperties.getUrl();
109+
List<ScalarSource> scalarSources = springDocConfigProperties.getGroupConfigs().stream()
110+
.map(groupConfig -> new ScalarSource(url + DEFAULT_PATH_SEPARATOR + groupConfig.getGroup(), groupConfig.getDisplayName(), null, false)).toList();
111+
112+
if(!CollectionUtils.isEmpty(scalarSources)) {
113+
scalarProperties.setSources(scalarSources);
114+
scalarProperties.setUrl(null);
115+
}
116+
96117
String html = ScalarHtmlRenderer.render(configuredProperties);
97118
String bundleUrl = buildJsBundleUrl(requestUrl, scalarPath);
98119
html = html.replaceAll("(<script[^>]*\\s+src\\s*=\\s*\")([^\"]*)(\")", "$1"+bundleUrl+"$3");
@@ -104,9 +125,9 @@ protected ResponseEntity<String> getDocs(String requestUrl, String apiDocsPath,
104125
/**
105126
* Configure properties scalar properties.
106127
*
107-
* @param properties the properties
108-
* @param requestUrl the request url
109-
* @param apiDocsPath the api docs path
128+
* @param properties the properties
129+
* @param requestUrl the request url
130+
* @param apiDocsPath the api docs path
110131
* @return the scalar properties
111132
*/
112133
private ScalarProperties configureProperties(ScalarProperties properties, String requestUrl, String apiDocsPath ) {
@@ -118,8 +139,8 @@ private ScalarProperties configureProperties(ScalarProperties properties, String
118139
/**
119140
* Build js bundle url string.
120141
*
121-
* @param requestUrl the request url
122-
* @param scalarPath the scalar path
142+
* @param requestUrl the request url
143+
* @param scalarPath the scalar path
123144
* @return the string
124145
*/
125146
private String buildJsBundleUrl(String requestUrl, String scalarPath) {
@@ -136,8 +157,8 @@ private String buildJsBundleUrl(String requestUrl, String scalarPath) {
136157
/**
137158
* Gets api docs url.
138159
*
139-
* @param requestUrl the request url
140-
* @param apiDocsPath the api docs path
160+
* @param requestUrl the request url
161+
* @param apiDocsPath the api docs path
141162
* @return the api docs url
142163
*/
143164
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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@
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
*
6055
* @param scalarProperties the scalar properties
6156
* @param springDocConfigProperties the spring doc config properties
6257
*/
6358
public ScalarWebFluxController(SpringBootScalarProperties scalarProperties, SpringDocConfigProperties springDocConfigProperties) {
64-
super(scalarProperties);
65-
this.springDocConfigProperties = springDocConfigProperties;
59+
super(scalarProperties, springDocConfigProperties);
6660
}
6761

6862
/**

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:9522/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:9522/v3/api-docs/springdocDefault","title":"springdocDefault","default":false},{"url":"http://localhost:9522/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:9519/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:9519/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: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public class ScalarConfiguration {
6868
/**
6969
* Scalar web mvc controller scalar web mvc controller.
7070
*
71-
* @param scalarProperties the scalar properties
72-
* @param springDocConfigProperties the spring doc config properties
71+
* @param scalarProperties the scalar properties
72+
* @param springDocConfigProperties the spring doc config properties
7373
* @return the scalar web mvc controller
7474
*/
7575
@Bean
@@ -95,7 +95,7 @@ public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {
9595
/**
9696
* Spring doc app initializer spring doc app initializer.
9797
*
98-
* @param scalarProperties the spring doc config properties
98+
* @param scalarProperties the spring doc config properties
9999
* @return the spring doc app initializer
100100
*/
101101
@Bean
@@ -117,21 +117,22 @@ 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
/**
132133
* Spring doc scalar initializer spring doc app initializer.
133134
*
134-
* @param scalarProperties the scalar properties
135+
* @param scalarProperties the scalar properties
135136
* @return the spring doc app initializer
136137
*/
137138
@Bean

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

Lines changed: 1 addition & 7 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
/**

0 commit comments

Comments
 (0)