Skip to content

Commit

Permalink
Move to webjars-locator-lite, in preparation for spring-boot 3.4 GA. F…
Browse files Browse the repository at this point in the history
…ixes #2747
  • Loading branch information
bnasslahsen committed Oct 5, 2024
1 parent a98d3e6 commit ab2538e
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 161 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
}
}
4 changes: 4 additions & 0 deletions springdoc-openapi-starter-webflux-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-lite</artifactId>
</dependency>
<!-- Actuator dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Resource> resolveResource(ServerWebExchange exchange, String requestPath, List<? extends Resource> 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<String> resolveUrlPath(String resourceUrlPath, List<? extends Resource> 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;
}
}
4 changes: 4 additions & 0 deletions springdoc-openapi-starter-webmvc-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-lite</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends Resource> 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<? extends Resource> 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;
}

}

0 comments on commit ab2538e

Please sign in to comment.