Skip to content

Commit f494346

Browse files
author
bnasslahsen
committed
Review configuration via springdoc.swagger-ui.urls. Fixes #481
1 parent a5fc911 commit f494346

File tree

13 files changed

+1079
-43
lines changed

13 files changed

+1079
-43
lines changed

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/GroupedOpenApi.java

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ private GroupedOpenApi(Builder builder) {
4848
this.packagesToExclude=builder.packagesToExclude;
4949
this.pathsToExclude=builder.pathsToExclude;
5050
this.openApiCustomisers = Objects.requireNonNull(builder.openApiCustomisers);
51-
SwaggerUiConfigProperties.addGroup(this.group);
5251
if (CollectionUtils.isEmpty(this.pathsToMatch)
5352
&& CollectionUtils.isEmpty(this.packagesToScan)
5453
&& CollectionUtils.isEmpty(this.pathsToExclude)

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

+26-15
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class SwaggerUiConfigProperties {
5454

5555
public static final String CONFIG_URL_PROPERTY = "configUrl";
5656

57-
private static Set<SwaggerUrl> swaggerUrls = new HashSet<>();
57+
private Set<SwaggerUrl> urls = new HashSet<>();
5858

5959
/**
6060
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
@@ -158,26 +158,26 @@ public class SwaggerUiConfigProperties {
158158

159159
private Direction groupsOrder = Direction.ASC;
160160

161-
public static void addGroup(String group) {
161+
public void addGroup(String group) {
162162
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
163-
swaggerUrls.add(swaggerUrl);
163+
urls.add(swaggerUrl);
164164
}
165165

166-
public static void addGroup(String group, String url) {
167-
SwaggerUrl swaggerUrl = new SwaggerUrl(group, url);
168-
swaggerUrls.add(swaggerUrl);
166+
public Set<SwaggerUrl> getUrls() {
167+
return this.urls;
169168
}
170169

171-
public static Set<SwaggerUrl> getSwaggerUrls() {
172-
return swaggerUrls;
170+
public void setUrls(Set<SwaggerUrl> urls) {
171+
this.urls = urls;
173172
}
174173

175-
public static void setSwaggerUrls(Set<SwaggerUrl> swaggerUrls) {
176-
SwaggerUiConfigProperties.swaggerUrls = swaggerUrls;
177-
}
178-
179-
public static void addUrl(String url) {
180-
swaggerUrls.forEach(elt -> elt.setUrl(url + DEFAULT_PATH_SEPARATOR + elt.getName()));
174+
public void addUrl(String url) {
175+
this.urls.forEach(elt ->
176+
{
177+
if (StringUtils.isBlank(elt.url))
178+
elt.setUrl(url + DEFAULT_PATH_SEPARATOR + elt.getName());
179+
}
180+
);
181181
}
182182

183183
public Map<String, Object> getConfigParameters() {
@@ -203,7 +203,7 @@ public Map<String, Object> getConfigParameters() {
203203
SpringDocPropertiesUtils.put("supportedSubmitMethods", supportedSubmitMethods.toString(), params);
204204
SpringDocPropertiesUtils.put("oauth2RedirectUrl", oauth2RedirectUrl, params);
205205
SpringDocPropertiesUtils.put("url", url, params);
206-
put("urls", swaggerUrls, params);
206+
put("urls", urls, params);
207207
return params;
208208
}
209209

@@ -395,6 +395,8 @@ static class SwaggerUrl {
395395

396396
private String name;
397397

398+
public SwaggerUrl() { }
399+
398400
public SwaggerUrl(String group, String url) {
399401
this.url = url;
400402
this.name = group;
@@ -432,6 +434,15 @@ public boolean equals(Object o) {
432434
public int hashCode() {
433435
return Objects.hash(name);
434436
}
437+
438+
@Override
439+
public String toString() {
440+
final StringBuilder sb = new StringBuilder("SwaggerUrl{");
441+
sb.append("url='").append(url).append('\'');
442+
sb.append(", name='").append(name).append('\'');
443+
sb.append('}');
444+
return sb.toString();
445+
}
435446
}
436447

437448
enum Direction {

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
5757

5858
@Override
5959
public void afterPropertiesSet() {
60+
springDocConfigProperties.getGroupConfigs().forEach( groupConfig -> swaggerUiConfig.addGroup(groupConfig.getGroup()));
6061
calculateUiRootPath();
6162
}
6263

@@ -73,14 +74,14 @@ protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriCompon
7374
String url = buildUrl(contextPath, apiDocsUrl);
7475
String swaggerConfigUrl = url + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
7576
swaggerUiConfig.setConfigUrl(swaggerConfigUrl);
76-
if (SwaggerUiConfigProperties.getSwaggerUrls().isEmpty()) {
77+
if (swaggerUiConfig.getUrls().isEmpty()) {
7778
if (StringUtils.isEmpty(swaggerUiUrl))
7879
swaggerUiConfig.setUrl(url);
7980
else
8081
swaggerUiConfig.setUrl(swaggerUiUrl);
8182
}
8283
else
83-
SwaggerUiConfigProperties.addUrl(url);
84+
swaggerUiConfig.addUrl(url);
8485
}
8586
calculateOauth2RedirectUrl(uriComponentsBuilder);
8687
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<root level="off"/>
4+
</configuration>

Diff for: springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app4/SpringDocApp4Test.java

-9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818

1919
package test.org.springdoc.ui.app4;
2020

21-
import java.util.HashSet;
22-
23-
import org.junit.jupiter.api.AfterEach;
2421
import org.junit.jupiter.api.Test;
25-
import org.springdoc.core.SwaggerUiConfigProperties;
2622
import test.org.springdoc.ui.AbstractSpringDocTest;
2723

2824
import org.springframework.test.context.TestPropertySource;
@@ -46,9 +42,4 @@ public void swagger_config_for_multiple_groups() throws Exception {
4642
.andExpect(jsonPath("urls[1].url", equalTo("/v3/api-docs/pets")))
4743
.andExpect(jsonPath("urls[1].name", equalTo("pets")));
4844
}
49-
50-
@AfterEach
51-
public void reset() {
52-
SwaggerUiConfigProperties.setSwaggerUrls(new HashSet<>());
53-
}
5445
}
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.app8;
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+
}
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.app8;
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.hamcrest.CoreMatchers.equalTo;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
33+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34+
35+
@TestPropertySource(properties = {
36+
"springdoc.swagger-ui.urls[0].name=users",
37+
"springdoc.swagger-ui.urls[0].url=/api-docs.yaml"
38+
})
39+
public class SpringDocApp8Test extends AbstractSpringDocTest {
40+
41+
@Test
42+
public void swagger_config_for_multiple_groups() throws Exception {
43+
mockMvc.perform(get("/v3/api-docs/swagger-config"))
44+
.andExpect(status().isOk())
45+
.andExpect(jsonPath("configUrl", equalTo("/v3/api-docs/swagger-config")))
46+
.andExpect(jsonPath("url").doesNotExist())
47+
.andExpect(jsonPath("urls[0].url", equalTo("/api-docs.yaml")))
48+
.andExpect(jsonPath("urls[0].name", equalTo("users")));
49+
}
50+
51+
@SpringBootApplication
52+
static class SpringDocTestApp {}
53+
54+
}

0 commit comments

Comments
 (0)