From ab2538e7280d7403d4e9917069d50efaa5f94635 Mon Sep 17 00:00:00 2001 From: "Badr.NassLahsen" Date: Sat, 5 Oct 2024 18:26:03 +0200 Subject: [PATCH] Move to webjars-locator-lite, in preparation for spring-boot 3.4 GA. Fixes #2747 --- .../ui/AbstractSwaggerResourceResolver.java | 48 --------------- .../ui/SwaggerResourceResolverUtils.java | 29 +++++++++ .../AbstractSwaggerResourceResolverTest.java | 51 ---------------- .../ui/SwaggerResourceResolverUtilsTest.java | 36 +++++++++++ springdoc-openapi-starter-webflux-ui/pom.xml | 4 ++ .../webflux/ui/SwaggerResourceResolver.java | 59 ++++++++----------- springdoc-openapi-starter-webmvc-ui/pom.xml | 4 ++ .../webmvc/ui/SwaggerResourceResolver.java | 51 ++++++++-------- 8 files changed, 121 insertions(+), 161 deletions(-) delete mode 100644 springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java create mode 100644 springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/SwaggerResourceResolverUtils.java delete mode 100644 springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java create mode 100644 springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/SwaggerResourceResolverUtilsTest.java 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 deleted file mode 100644 index 1ead80c67..000000000 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.springdoc.ui; - -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.springdoc.core.properties.SwaggerUiConfigProperties; - -import org.springframework.lang.Nullable; - -/** - * The type Web jars version resource resolver. - * - * @author bnasslahsen - */ -public class AbstractSwaggerResourceResolver { - - /** - * The Swagger ui config properties. - */ - private final SwaggerUiConfigProperties swaggerUiConfigProperties; - - /** - * Instantiates a new Web jars version resource resolver. - * - * @param swaggerUiConfigProperties the swagger ui config properties - */ - public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { - this.swaggerUiConfigProperties = swaggerUiConfigProperties; - } - - /** - * Find web jar resource path string. - * - * @param pathStr the path - * @return the string - */ - @Nullable - protected String findWebJarResourcePath(String pathStr) { - Path path = Paths.get(pathStr); - if (path.getNameCount() < 2) return null; - String version = swaggerUiConfigProperties.getVersion(); - if (version == null) return null; - Path first = path.getName(0); - Path rest = path.subpath(1, path.getNameCount()); - return first.resolve(version).resolve(rest).toString(); - } - -} diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/SwaggerResourceResolverUtils.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/SwaggerResourceResolverUtils.java new file mode 100644 index 000000000..b0a1aa3ac --- /dev/null +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/SwaggerResourceResolverUtils.java @@ -0,0 +1,29 @@ +package org.springdoc.ui; + +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * The interface Swagger resource resolver utils. + * + * @author bnasslahsen + */ +public interface SwaggerResourceResolverUtils { + + /** + * Find swagger resource path string. + * + * @param pathStr the path + * @param version the version + * @return the string + */ + static String findSwaggerResourcePath(String pathStr, String version) { + Path path = Paths.get(pathStr); + if (path.getNameCount() < 2) return null; + if (version == null) return null; + Path first = path.getName(0); + Path rest = path.subpath(1, path.getNameCount()); + return first.resolve(version).resolve(rest).toString(); + } + +} 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 deleted file mode 100644 index 0e558fef9..000000000 --- a/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.springdoc.ui; - -import java.io.File; -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" + File.separator + "4.18.2" + File.separator + "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)); - } -} diff --git a/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/SwaggerResourceResolverUtilsTest.java b/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/SwaggerResourceResolverUtilsTest.java new file mode 100644 index 000000000..04fd1a1f7 --- /dev/null +++ b/springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/SwaggerResourceResolverUtilsTest.java @@ -0,0 +1,36 @@ +package org.springdoc.ui; + +import java.io.File; +import java.util.Objects; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath; + +class SwaggerResourceResolverUtilsTest { + + private final String VERSION = "4.18.2"; + + @Test + void findWebJarResourcePath() { + String path = "swagger-ui/swagger-initializer.js"; + String actual = findSwaggerResourcePath(path,VERSION); + assertEquals("swagger-ui" + File.separator + "4.18.2" + File.separator + "swagger-initializer.js", actual); + } + + @Test + void returnNullWhenPathIsSameAsWebjar() { + String path = "swagger-ui"; + String actual = findSwaggerResourcePath(path,VERSION); + assertTrue(Objects.isNull(actual)); + } + + @Test + void returnNullWhenVersionIsNull() { + String path = "swagger-ui/swagger-initializer.js"; + String actual = findSwaggerResourcePath(path,null); + assertTrue(Objects.isNull(actual)); + } +} diff --git a/springdoc-openapi-starter-webflux-ui/pom.xml b/springdoc-openapi-starter-webflux-ui/pom.xml index 36e6ace7b..83b20885c 100644 --- a/springdoc-openapi-starter-webflux-ui/pom.xml +++ b/springdoc-openapi-starter-webflux-ui/pom.xml @@ -18,6 +18,10 @@ org.webjars swagger-ui + + org.webjars + webjars-locator-lite + org.springframework.boot diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerResourceResolver.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerResourceResolver.java index 343227120..0b1b8750e 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerResourceResolver.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerResourceResolver.java @@ -1,58 +1,47 @@ package org.springdoc.webflux.ui; -import java.util.List; - import org.springdoc.core.properties.SwaggerUiConfigProperties; -import org.springdoc.ui.AbstractSwaggerResourceResolver; -import reactor.core.publisher.Mono; -import org.springframework.core.io.Resource; -import org.springframework.web.reactive.resource.ResourceResolver; -import org.springframework.web.reactive.resource.ResourceResolverChain; -import org.springframework.web.server.ServerWebExchange; +import org.springframework.lang.Nullable; +import org.springframework.web.reactive.resource.LiteWebJarsResourceResolver; + +import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath; /** - * The type Web jars version resource resolver. + * The type Swagger resource resolver. * * @author bnasslahsen */ -public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver { +public class SwaggerResourceResolver extends LiteWebJarsResourceResolver { + /** + * The Swagger ui config properties. + */ + private final SwaggerUiConfigProperties swaggerUiConfigProperties; + /** * Instantiates a new Web jars version resource resolver. * * @param swaggerUiConfigProperties the swagger ui config properties */ public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { - super(swaggerUiConfigProperties); + this.swaggerUiConfigProperties = swaggerUiConfigProperties; } - @Override - public Mono resolveResource(ServerWebExchange exchange, String requestPath, List locations, ResourceResolverChain chain) { - return chain.resolveResource(exchange, requestPath, locations) - .switchIfEmpty(Mono.defer(() -> { - String webJarsResourcePath = findWebJarResourcePath(requestPath); - if (webJarsResourcePath != null) { - return chain.resolveResource(exchange, webJarsResourcePath, locations); - } - else { - return Mono.empty(); - } - })); - } + /** + * Find web jar resource path string. + * + * @param pathStr the path + * @return the string + */ + @Nullable @Override - public Mono resolveUrlPath(String resourceUrlPath, List locations, ResourceResolverChain chain) { - return chain.resolveUrlPath(resourceUrlPath, locations) - .switchIfEmpty(Mono.defer(() -> { - String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); - if (webJarResourcePath != null) { - return chain.resolveUrlPath(webJarResourcePath, locations); - } - else { - return Mono.empty(); - } - })); + protected String findWebJarResourcePath(String pathStr) { + String resourcePath = super.findWebJarResourcePath(pathStr); + if(resourcePath == null) + return findSwaggerResourcePath(pathStr, swaggerUiConfigProperties.getVersion()); + return resourcePath; } } \ No newline at end of file diff --git a/springdoc-openapi-starter-webmvc-ui/pom.xml b/springdoc-openapi-starter-webmvc-ui/pom.xml index 92eddc837..d3b0ae01a 100644 --- a/springdoc-openapi-starter-webmvc-ui/pom.xml +++ b/springdoc-openapi-starter-webmvc-ui/pom.xml @@ -23,6 +23,10 @@ org.webjars swagger-ui + + org.webjars + webjars-locator-lite + org.springframework.boot spring-boot-starter-security diff --git a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java index 4a980f69b..3938b1a4c 100644 --- a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java +++ b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java @@ -1,49 +1,46 @@ package org.springdoc.webmvc.ui; -import java.util.List; - -import jakarta.servlet.http.HttpServletRequest; import org.springdoc.core.properties.SwaggerUiConfigProperties; -import org.springdoc.ui.AbstractSwaggerResourceResolver; -import org.springframework.core.io.Resource; -import org.springframework.web.servlet.resource.ResourceResolver; -import org.springframework.web.servlet.resource.ResourceResolverChain; +import org.springframework.lang.Nullable; +import org.springframework.web.servlet.resource.LiteWebJarsResourceResolver; + +import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath; /** * The type Web jars version resource resolver. * * @author bnasslahsen */ -public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver { +public class SwaggerResourceResolver extends LiteWebJarsResourceResolver { + /** + * The Swagger ui config properties. + */ + private final SwaggerUiConfigProperties swaggerUiConfigProperties; + /** * Instantiates a new Web jars version resource resolver. * * @param swaggerUiConfigProperties the swagger ui config properties */ public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { - super(swaggerUiConfigProperties); + this.swaggerUiConfigProperties = swaggerUiConfigProperties; } + /** + * Find web jar resource path string. + * + * @param pathStr the path + * @return the string + */ + @Nullable @Override - public Resource resolveResource(HttpServletRequest request, String requestPath, List locations, ResourceResolverChain chain) { - Resource resolved = chain.resolveResource(request, requestPath, locations); - if (resolved == null) { - String webJarResourcePath = findWebJarResourcePath(requestPath); - if (webJarResourcePath != null) - return chain.resolveResource(request, webJarResourcePath, locations); - } - return resolved; } - - @Override - public String resolveUrlPath(String resourcePath, List locations, ResourceResolverChain chain) { - String path = chain.resolveUrlPath(resourcePath, locations); - if (path == null) { - String webJarResourcePath = findWebJarResourcePath(resourcePath); - if (webJarResourcePath != null) - return chain.resolveUrlPath(webJarResourcePath, locations); - } - return path; + protected String findWebJarResourcePath(String pathStr) { + String resourcePath = super.findWebJarResourcePath(pathStr); + if(resourcePath == null) + return findSwaggerResourcePath(pathStr, swaggerUiConfigProperties.getVersion()); + return resourcePath; } + } \ No newline at end of file