Skip to content

Commit 41a988e

Browse files
bjornblomqvistBjörn Blomqvist
authored and
Björn Blomqvist
committed
Add fix for using spec file and generated spec at the same time
Being able to use both a spec file and generated spec stopped working in version 1.6.5. The test shows the behavior found 1.6.4 and the fix brings back that behavior. BUGFIX
1 parent 1902053 commit 41a988e

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
132132
else
133133
swaggerUiConfigParameters.addUrl(apiDocsUrl);
134134
if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) {
135-
swaggerUiConfigParameters.setUrls(swaggerUiConfig.cloneUrls());
136-
swaggerUiConfigParameters.getUrls().forEach(swaggerUrl -> {
135+
swaggerUiConfig.cloneUrls().forEach(swaggerUrl -> {
136+
swaggerUiConfigParameters.getUrls().remove(swaggerUrl);
137137
if (!swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl()))
138138
swaggerUrl.setUrl(buildUrlWithContextPath(swaggerUrl.getUrl()));
139+
swaggerUiConfigParameters.getUrls().add(swaggerUrl);
139140
});
140141
}
141142
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.springframework.test.annotation.DirtiesContext;
2223
import test.org.springdoc.ui.AbstractSpringDocActuatorTest;
2324

2425
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -30,6 +31,7 @@
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233

34+
@DirtiesContext
3335
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
3436
properties = { "spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE", "springdoc.show-actuator=true",
3537
"management.endpoints.web.base-path=/management",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.app1;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.test.annotation.DirtiesContext;
25+
import test.org.springdoc.ui.AbstractSpringDocActuatorTest;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
/*
33+
Test showing how specs generated at runtime and specs configured in can work at the same time.
34+
35+
The expectation is that the openapi.yml file will be shown together with the generated ones.
36+
*/
37+
@DirtiesContext
38+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
39+
properties = { "spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE", "springdoc.show-actuator=true",
40+
"management.endpoints.web.base-path=/management",
41+
"server.servlet.context-path=/demo/api", "management.server.port=9002", "management.server.base-path=/demo/api",
42+
"springdoc.swagger-ui.urls[0].url=/api-docs/xxx/v1/openapi.yml",
43+
"springdoc.swagger-ui.urls[0].name=toto",
44+
})
45+
public class SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest extends AbstractSpringDocActuatorTest {
46+
47+
@Test
48+
public void testIndexSwaggerConfig() throws Exception {
49+
mockMvc.perform(get("/demo/api/v3/api-docs/swagger-config").contextPath("/demo/api"))
50+
.andExpect(status().isOk())
51+
.andExpect(jsonPath("validatorUrl", equalTo("")))
52+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/demo/api/swagger-ui/oauth2-redirect.html")))
53+
.andExpect(jsonPath("url").doesNotExist())
54+
.andExpect(jsonPath("urls[0].url", equalTo("/demo/api/v3/api-docs/springdocDefault")))
55+
.andExpect(jsonPath("urls[0].name", equalTo("springdocDefault")))
56+
.andExpect(jsonPath("urls[1].url", equalTo("/demo/api/api-docs/xxx/v1/openapi.yml")))
57+
.andExpect(jsonPath("urls[1].name", equalTo("toto")))
58+
.andExpect(jsonPath("urls[2].url", equalTo("/demo/api/v3/api-docs/x-actuator")))
59+
.andExpect(jsonPath("urls[2].name", equalTo("x-actuator")));
60+
}
61+
62+
63+
@SpringBootApplication
64+
static class SpringDocTestApp {}
65+
66+
}

0 commit comments

Comments
 (0)