Skip to content

Commit 63e7f25

Browse files
committed
springdoc-openapi-starter-webmvc-ui 2.1.0. Fixes #2311
1 parent e4ceeec commit 63e7f25

File tree

7 files changed

+130
-5
lines changed

7 files changed

+130
-5
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import org.springdoc.core.service.GenericResponseService;
9898
import org.springdoc.core.service.OpenAPIService;
9999
import org.springdoc.core.service.OperationService;
100+
import org.springdoc.core.utils.SpringDocUtils;
100101

101102
import org.springframework.aop.support.AopUtils;
102103
import org.springframework.beans.factory.ObjectFactory;
@@ -1269,7 +1270,7 @@ protected URI getActuatorURI(String scheme, String host) {
12691270
port = actuatorProvider.getApplicationPort();
12701271
path = actuatorProvider.getContextPath();
12711272
String mvcServletPath = this.openAPIService.getContext().getBean(Environment.class).getProperty(SPRING_MVC_SERVLET_PATH);
1272-
if (StringUtils.isNotEmpty(mvcServletPath))
1273+
if (SpringDocUtils.isValidPath(mvcServletPath))
12731274
path = path + mvcServletPath;
12741275
}
12751276
try {

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SpringDocUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.function.Predicate;
2929

3030
import io.swagger.v3.oas.models.media.Schema;
31+
import org.apache.commons.lang3.StringUtils;
3132
import org.springdoc.api.AbstractOpenApiResource;
3233
import org.springdoc.core.converters.AdditionalModelsConverter;
3334
import org.springdoc.core.converters.ConverterUtils;
@@ -375,5 +376,17 @@ public SpringDocUtils removeJavaTypeToIgnore(Class<?> clazz) {
375376
ConverterUtils.removeJavaTypeToIgnore(clazz);
376377
return this;
377378
}
379+
380+
/**
381+
* Is valid path boolean.
382+
*
383+
* @param path the path
384+
* @return the boolean
385+
*/
386+
public static boolean isValidPath(String path) {
387+
if (StringUtils.isNotBlank(path) && !path.equals("/"))
388+
return true;
389+
return false;
390+
}
378391
}
379392

springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerUiHome.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package org.springdoc.webmvc.ui;
2626

2727
import io.swagger.v3.oas.annotations.Operation;
28-
import org.apache.commons.lang3.StringUtils;
28+
import org.springdoc.core.utils.SpringDocUtils;
2929

3030
import org.springframework.beans.factory.annotation.Value;
3131
import org.springframework.stereotype.Controller;
@@ -57,7 +57,7 @@ public class SwaggerUiHome {
5757
@Operation(hidden = true)
5858
public String index() {
5959
StringBuilder uiRootPath = new StringBuilder();
60-
if (StringUtils.isNotBlank(mvcServletPath))
60+
if (SpringDocUtils.isValidPath(mvcServletPath))
6161
uiRootPath.append(mvcServletPath);
6262

6363
return REDIRECT_URL_PREFIX + uiRootPath + swaggerUiPath;

springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springdoc.core.properties.SwaggerUiConfigParameters;
3232
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3333
import org.springdoc.core.providers.SpringWebProvider;
34+
import org.springdoc.core.utils.SpringDocUtils;
3435

3536
import org.springframework.beans.factory.annotation.Value;
3637
import org.springframework.http.ResponseEntity;
@@ -99,7 +100,7 @@ public ResponseEntity<Void> redirectToUi(HttpServletRequest request) {
99100
@Override
100101
protected void calculateUiRootPath(StringBuilder... sbUrls) {
101102
StringBuilder sbUrl = new StringBuilder();
102-
if (StringUtils.isNotBlank(mvcServletPath))
103+
if (SpringDocUtils.isValidPath(mvcServletPath))
103104
sbUrl.append(mvcServletPath);
104105
calculateUiRootCommon(sbUrl, sbUrls);
105106
}
@@ -113,7 +114,7 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
113114
*/
114115
@Override
115116
protected String buildUrl(String contextPath, final String docsUrl) {
116-
if (StringUtils.isNotBlank(mvcServletPath))
117+
if (SpringDocUtils.isValidPath(mvcServletPath))
117118
contextPath += mvcServletPath;
118119
return super.buildUrl(contextPath, docsUrl);
119120
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.app33;
20+
21+
22+
import jakarta.validation.Valid;
23+
import jakarta.validation.constraints.Size;
24+
25+
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.RequestParam;
27+
import org.springframework.web.bind.annotation.RestController;
28+
29+
@RestController
30+
public class HelloController {
31+
32+
@GetMapping(value = "/persons")
33+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
34+
35+
}
36+
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.app33;
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+
27+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30+
31+
@TestPropertySource(properties = {
32+
"server.servlet.context-path=/",
33+
"spring.mvc.servlet.path=/"
34+
})
35+
public class SpringDocApp33Test extends AbstractSpringDocTest {
36+
37+
@Test
38+
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
39+
mockMvc.perform(get("/swagger-ui.html"))
40+
.andExpect(status().isFound())
41+
.andExpect(header().string("Location", "/swagger-ui/index.html"));
42+
43+
super.chekJS();
44+
}
45+
46+
@SpringBootApplication
47+
static class SpringDocTestApp {}
48+
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
window.onload = function() {
2+
//<editor-fold desc="Changeable Configuration Block">
3+
4+
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
5+
window.ui = SwaggerUIBundle({
6+
url: "https://petstore.swagger.io/v2/swagger.json",
7+
dom_id: '#swagger-ui',
8+
deepLinking: true,
9+
presets: [
10+
SwaggerUIBundle.presets.apis,
11+
SwaggerUIStandalonePreset
12+
],
13+
plugins: [
14+
SwaggerUIBundle.plugins.DownloadUrl
15+
],
16+
layout: "StandaloneLayout" ,
17+
18+
"configUrl" : "/v3/api-docs/swagger-config",
19+
"validatorUrl" : ""
20+
21+
});
22+
23+
//</editor-fold>
24+
};

0 commit comments

Comments
 (0)