Repository: springdoc/springdoc-openapi
Pull request #2243 (2023-05-27 14:41:52)

fix StringIndexOutOfBoundsException when path is same webjar. #2235
#2235 



Patch:
diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java
index 56db770b5..79d41b497 100644
--- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java
+++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java
@@ -36,7 +36,7 @@ public class AbstractSwaggerResourceResolver {
 	@Nullable
 	protected String findWebJarResourcePath(String path) {
 		String webjar = webjar(path);
-		if (webjar.length() > 0) {
+		if (webjar.length() > 0 && !path.equals(webjar)) {
 			String version = swaggerUiConfigProperties.getVersion();
 			if (version != null) {
 				String partialPath = path(webjar, path);
@@ -67,9 +67,9 @@ public class AbstractSwaggerResourceResolver {
 	 * @return the string
 	 */
 	private String path(String webjar, String path) {
-		if (path.startsWith(webjar)) {
+		if (path.startsWith(webjar) && path.length() > webjar.length()) {
 			path = path.substring(webjar.length() + 1);
 		}
 		return path;
 	}
-}
\ No newline at end of file
+}


Test patch:
diff --git a/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java b/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java
new file mode 100644
index 000000000..cb9da769d
--- /dev/null
+++ b/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java
@@ -0,0 +1,50 @@
+package org.springdoc.ui;
+
+import java.util.Objects;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springdoc.core.properties.SwaggerUiConfigProperties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AbstractSwaggerResourceResolverTest {
+	private SwaggerUiConfigProperties swaggerUiConfigProperties;
+
+	private AbstractSwaggerResourceResolver abstractSwaggerResourceResolver;
+
+	private final String VERSION = "4.18.2";
+
+	@BeforeEach
+	public void setup(){
+		swaggerUiConfigProperties = new SwaggerUiConfigProperties();
+		swaggerUiConfigProperties.setVersion(VERSION);
+		abstractSwaggerResourceResolver = new AbstractSwaggerResourceResolver(swaggerUiConfigProperties);
+	}
+
+	@Test
+	void findWebJarResourcePath() {
+		String path = "swagger-ui/swagger-initializer.js";
+
+		String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
+		assertEquals("swagger-ui/4.18.2/swagger-initializer.js", actual);
+	}
+
+	@Test
+	void returNullWhenPathIsSameAsWebjar() {
+		String path = "swagger-ui";
+
+		String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
+		assertTrue(Objects.isNull(actual));
+	}
+
+	@Test
+	void returNullWhenVersionIsNull() {
+		String path = "swagger-ui/swagger-initializer.js";
+		swaggerUiConfigProperties.setVersion(null);
+
+		String actual = abstractSwaggerResourceResolver.findWebJarResourcePath(path);
+		assertTrue(Objects.isNull(actual));
+	}
+}