Skip to content

Commit ab2538e

Browse files
committed
Move to webjars-locator-lite, in preparation for spring-boot 3.4 GA. Fixes #2747
1 parent a98d3e6 commit ab2538e

File tree

8 files changed

+121
-161
lines changed

8 files changed

+121
-161
lines changed

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

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springdoc.ui;
2+
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
6+
/**
7+
* The interface Swagger resource resolver utils.
8+
*
9+
* @author bnasslahsen
10+
*/
11+
public interface SwaggerResourceResolverUtils {
12+
13+
/**
14+
* Find swagger resource path string.
15+
*
16+
* @param pathStr the path
17+
* @param version the version
18+
* @return the string
19+
*/
20+
static String findSwaggerResourcePath(String pathStr, String version) {
21+
Path path = Paths.get(pathStr);
22+
if (path.getNameCount() < 2) return null;
23+
if (version == null) return null;
24+
Path first = path.getName(0);
25+
Path rest = path.subpath(1, path.getNameCount());
26+
return first.resolve(version).resolve(rest).toString();
27+
}
28+
29+
}

springdoc-openapi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.springdoc.ui;
2+
3+
import java.io.File;
4+
import java.util.Objects;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath;
11+
12+
class SwaggerResourceResolverUtilsTest {
13+
14+
private final String VERSION = "4.18.2";
15+
16+
@Test
17+
void findWebJarResourcePath() {
18+
String path = "swagger-ui/swagger-initializer.js";
19+
String actual = findSwaggerResourcePath(path,VERSION);
20+
assertEquals("swagger-ui" + File.separator + "4.18.2" + File.separator + "swagger-initializer.js", actual);
21+
}
22+
23+
@Test
24+
void returnNullWhenPathIsSameAsWebjar() {
25+
String path = "swagger-ui";
26+
String actual = findSwaggerResourcePath(path,VERSION);
27+
assertTrue(Objects.isNull(actual));
28+
}
29+
30+
@Test
31+
void returnNullWhenVersionIsNull() {
32+
String path = "swagger-ui/swagger-initializer.js";
33+
String actual = findSwaggerResourcePath(path,null);
34+
assertTrue(Objects.isNull(actual));
35+
}
36+
}

springdoc-openapi-starter-webflux-ui/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<groupId>org.webjars</groupId>
1919
<artifactId>swagger-ui</artifactId>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.webjars</groupId>
23+
<artifactId>webjars-locator-lite</artifactId>
24+
</dependency>
2125
<!-- Actuator dependencies -->
2226
<dependency>
2327
<groupId>org.springframework.boot</groupId>
Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
11
package org.springdoc.webflux.ui;
22

3-
import java.util.List;
4-
53
import org.springdoc.core.properties.SwaggerUiConfigProperties;
6-
import org.springdoc.ui.AbstractSwaggerResourceResolver;
7-
import reactor.core.publisher.Mono;
84

9-
import org.springframework.core.io.Resource;
10-
import org.springframework.web.reactive.resource.ResourceResolver;
11-
import org.springframework.web.reactive.resource.ResourceResolverChain;
12-
import org.springframework.web.server.ServerWebExchange;
5+
import org.springframework.lang.Nullable;
6+
import org.springframework.web.reactive.resource.LiteWebJarsResourceResolver;
7+
8+
import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath;
139

1410
/**
15-
* The type Web jars version resource resolver.
11+
* The type Swagger resource resolver.
1612
*
1713
* @author bnasslahsen
1814
*/
19-
public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver {
15+
public class SwaggerResourceResolver extends LiteWebJarsResourceResolver {
2016

2117

18+
/**
19+
* The Swagger ui config properties.
20+
*/
21+
private final SwaggerUiConfigProperties swaggerUiConfigProperties;
22+
2223
/**
2324
* Instantiates a new Web jars version resource resolver.
2425
*
2526
* @param swaggerUiConfigProperties the swagger ui config properties
2627
*/
2728
public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
28-
super(swaggerUiConfigProperties);
29+
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
2930
}
3031

31-
@Override
32-
public Mono<Resource> resolveResource(ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
33-
return chain.resolveResource(exchange, requestPath, locations)
34-
.switchIfEmpty(Mono.defer(() -> {
35-
String webJarsResourcePath = findWebJarResourcePath(requestPath);
36-
if (webJarsResourcePath != null) {
37-
return chain.resolveResource(exchange, webJarsResourcePath, locations);
38-
}
39-
else {
40-
return Mono.empty();
41-
}
42-
}));
43-
}
4432

33+
/**
34+
* Find web jar resource path string.
35+
*
36+
* @param pathStr the path
37+
* @return the string
38+
*/
39+
@Nullable
4540
@Override
46-
public Mono<String> resolveUrlPath(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) {
47-
return chain.resolveUrlPath(resourceUrlPath, locations)
48-
.switchIfEmpty(Mono.defer(() -> {
49-
String webJarResourcePath = findWebJarResourcePath(resourceUrlPath);
50-
if (webJarResourcePath != null) {
51-
return chain.resolveUrlPath(webJarResourcePath, locations);
52-
}
53-
else {
54-
return Mono.empty();
55-
}
56-
}));
41+
protected String findWebJarResourcePath(String pathStr) {
42+
String resourcePath = super.findWebJarResourcePath(pathStr);
43+
if(resourcePath == null)
44+
return findSwaggerResourcePath(pathStr, swaggerUiConfigProperties.getVersion());
45+
return resourcePath;
5746
}
5847
}

springdoc-openapi-starter-webmvc-ui/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<groupId>org.webjars</groupId>
2424
<artifactId>swagger-ui</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.webjars</groupId>
28+
<artifactId>webjars-locator-lite</artifactId>
29+
</dependency>
2630
<dependency>
2731
<groupId>org.springframework.boot</groupId>
2832
<artifactId>spring-boot-starter-security</artifactId>
Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
11
package org.springdoc.webmvc.ui;
22

3-
import java.util.List;
4-
5-
import jakarta.servlet.http.HttpServletRequest;
63
import org.springdoc.core.properties.SwaggerUiConfigProperties;
7-
import org.springdoc.ui.AbstractSwaggerResourceResolver;
84

9-
import org.springframework.core.io.Resource;
10-
import org.springframework.web.servlet.resource.ResourceResolver;
11-
import org.springframework.web.servlet.resource.ResourceResolverChain;
5+
import org.springframework.lang.Nullable;
6+
import org.springframework.web.servlet.resource.LiteWebJarsResourceResolver;
7+
8+
import static org.springdoc.ui.SwaggerResourceResolverUtils.findSwaggerResourcePath;
129

1310
/**
1411
* The type Web jars version resource resolver.
1512
*
1613
* @author bnasslahsen
1714
*/
18-
public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver {
15+
public class SwaggerResourceResolver extends LiteWebJarsResourceResolver {
1916

17+
/**
18+
* The Swagger ui config properties.
19+
*/
20+
private final SwaggerUiConfigProperties swaggerUiConfigProperties;
21+
2022
/**
2123
* Instantiates a new Web jars version resource resolver.
2224
*
2325
* @param swaggerUiConfigProperties the swagger ui config properties
2426
*/
2527
public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
26-
super(swaggerUiConfigProperties);
28+
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
2729
}
2830

31+
/**
32+
* Find web jar resource path string.
33+
*
34+
* @param pathStr the path
35+
* @return the string
36+
*/
37+
@Nullable
2938
@Override
30-
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
31-
Resource resolved = chain.resolveResource(request, requestPath, locations);
32-
if (resolved == null) {
33-
String webJarResourcePath = findWebJarResourcePath(requestPath);
34-
if (webJarResourcePath != null)
35-
return chain.resolveResource(request, webJarResourcePath, locations);
36-
}
37-
return resolved; }
38-
39-
@Override
40-
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
41-
String path = chain.resolveUrlPath(resourcePath, locations);
42-
if (path == null) {
43-
String webJarResourcePath = findWebJarResourcePath(resourcePath);
44-
if (webJarResourcePath != null)
45-
return chain.resolveUrlPath(webJarResourcePath, locations);
46-
}
47-
return path;
39+
protected String findWebJarResourcePath(String pathStr) {
40+
String resourcePath = super.findWebJarResourcePath(pathStr);
41+
if(resourcePath == null)
42+
return findSwaggerResourcePath(pathStr, swaggerUiConfigProperties.getVersion());
43+
return resourcePath;
4844
}
45+
4946
}

0 commit comments

Comments
 (0)