Skip to content

Commit

Permalink
Merge pull request #2243 from uc4w6c/webjar-resolver-outofboundsexcep…
Browse files Browse the repository at this point in the history
…tion_v2

fix StringIndexOutOfBoundsException when path is same webjar. #2235
  • Loading branch information
bnasslahsen committed May 31, 2023
2 parents 9978ced + 7048c19 commit bb6baa4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfig
@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);
Expand Down Expand Up @@ -67,9 +67,9 @@ private String webjar(String path) {
* @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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit bb6baa4

Please sign in to comment.