Skip to content

Commit bb6baa4

Browse files
authored
Merge pull request #2243 from uc4w6c/webjar-resolver-outofboundsexception_v2
fix StringIndexOutOfBoundsException when path is same webjar. #2235
2 parents 9978ced + 7048c19 commit bb6baa4

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfig
3636
@Nullable
3737
protected String findWebJarResourcePath(String path) {
3838
String webjar = webjar(path);
39-
if (webjar.length() > 0) {
39+
if (webjar.length() > 0 && !path.equals(webjar)) {
4040
String version = swaggerUiConfigProperties.getVersion();
4141
if (version != null) {
4242
String partialPath = path(webjar, path);
@@ -67,9 +67,9 @@ private String webjar(String path) {
6767
* @return the string
6868
*/
6969
private String path(String webjar, String path) {
70-
if (path.startsWith(webjar)) {
70+
if (path.startsWith(webjar) && path.length() > webjar.length()) {
7171
path = path.substring(webjar.length() + 1);
7272
}
7373
return path;
7474
}
75-
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.springdoc.ui;
2+
3+
import java.util.Objects;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.springdoc.core.properties.SwaggerUiConfigProperties;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
class AbstractSwaggerResourceResolverTest {
13+
private SwaggerUiConfigProperties swaggerUiConfigProperties;
14+
15+
private AbstractSwaggerResourceResolver abstractSwaggerResourceResolver;
16+
17+
private final String VERSION = "4.18.2";
18+
19+
@BeforeEach
20+
public void setup(){
21+
swaggerUiConfigProperties = new SwaggerUiConfigProperties();
22+
swaggerUiConfigProperties.setVersion(VERSION);
23+
abstractSwaggerResourceResolver = new AbstractSwaggerResourceResolver(swaggerUiConfigProperties);
24+
}
25+
26+
@Test
27+
void findWebJarResourcePath() {
28+
String path = "swagger-ui/swagger-initializer.js";
29+
30+
String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
31+
assertEquals("swagger-ui/4.18.2/swagger-initializer.js", actual);
32+
}
33+
34+
@Test
35+
void returNullWhenPathIsSameAsWebjar() {
36+
String path = "swagger-ui";
37+
38+
String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
39+
assertTrue(Objects.isNull(actual));
40+
}
41+
42+
@Test
43+
void returNullWhenVersionIsNull() {
44+
String path = "swagger-ui/swagger-initializer.js";
45+
swaggerUiConfigProperties.setVersion(null);
46+
47+
String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
48+
assertTrue(Objects.isNull(actual));
49+
}
50+
}

0 commit comments

Comments
 (0)