-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to webjars-locator-lite, in preparation for spring-boot 3.4 GA. F…
…ixes #2747
- Loading branch information
1 parent
a98d3e6
commit ab2538e
Showing
8 changed files
with
121 additions
and
161 deletions.
There are no files selected for viewing
48 changes: 0 additions & 48 deletions
48
...penapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...c-openapi-starter-common/src/main/java/org/springdoc/ui/SwaggerResourceResolverUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
51 changes: 0 additions & 51 deletions
51
...pi-starter-common/src/test/java/org/springdoc/ui/AbstractSwaggerResourceResolverTest.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
...enapi-starter-common/src/test/java/org/springdoc/ui/SwaggerResourceResolverUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 24 additions & 35 deletions
59
...pi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerResourceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 24 additions & 27 deletions
51
...napi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |