Prevent resource hint registration for pattern with leading slash
Prior to this commit, if a ResourcePatternHint was created with a resource pattern with a leading slash, the hint was registered and eventually converted to configuration for the GraalVM native image compiler. However, such a resource pattern is invalid for GraalVM. Consequently, the registered resources were not available within the compiled native image. This commit ensures that registered patterns are applicable in a native image by preventing creation of a ResourcePatternHint with a pattern with a leading slash. Closes gh-29088
This commit is contained in:
@@ -22,13 +22,14 @@ import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A hint that describes resources that should be made available at runtime.
|
||||
*
|
||||
* <p>The patterns may be a simple path which has a one-to-one mapping to a
|
||||
* <p>Each pattern may be a simple path which has a one-to-one mapping to a
|
||||
* resource on the classpath, or alternatively may contain the special
|
||||
* {@code *} character to indicate a wildcard search. For example:
|
||||
* {@code *} character to indicate a wildcard match. For example:
|
||||
* <ul>
|
||||
* <li>{@code file.properties}: matches just the {@code file.properties}
|
||||
* file at the root of the classpath.</li>
|
||||
@@ -42,9 +43,12 @@ import org.springframework.lang.Nullable;
|
||||
* and its child directories at any depth.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>A resource pattern must not start with a slash ({@code /}).
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Brian Clozel
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
public final class ResourcePatternHint implements ConditionalHint {
|
||||
@@ -55,6 +59,8 @@ public final class ResourcePatternHint implements ConditionalHint {
|
||||
private final TypeReference reachableType;
|
||||
|
||||
ResourcePatternHint(String pattern, @Nullable TypeReference reachableType) {
|
||||
Assert.isTrue(!pattern.startsWith("/"),
|
||||
() -> "Resource pattern [%s] must not start with a '/'".formatted(pattern));
|
||||
this.pattern = pattern;
|
||||
this.reachableType = reachableType;
|
||||
}
|
||||
@@ -104,4 +110,5 @@ public final class ResourcePatternHint implements ConditionalHint {
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.pattern, this.reachableType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,13 +19,22 @@ package org.springframework.aot.hint;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link ResourcePatternHint}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class ResourcePatternHintTests {
|
||||
class ResourcePatternHintTests {
|
||||
|
||||
@Test
|
||||
void patternWithLeadingSlashIsRejected() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new ResourcePatternHint("/file.properties", null))
|
||||
.withMessage("Resource pattern [/file.properties] must not start with a '/'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fileAtRoot() {
|
||||
@@ -66,4 +75,5 @@ public class ResourcePatternHintTests {
|
||||
.accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another")
|
||||
.rejects("file.properties", "com/file.properties");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user