Skip to content

Commit 10ade23

Browse files
committed
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
1 parent 04bb336 commit 10ade23

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import java.util.stream.Collectors;
2323

2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.Assert;
2526

2627
/**
2728
* A hint that describes resources that should be made available at runtime.
2829
*
29-
* <p>The patterns may be a simple path which has a one-to-one mapping to a
30+
* <p>Each pattern may be a simple path which has a one-to-one mapping to a
3031
* resource on the classpath, or alternatively may contain the special
31-
* {@code *} character to indicate a wildcard search. For example:
32+
* {@code *} character to indicate a wildcard match. For example:
3233
* <ul>
3334
* <li>{@code file.properties}: matches just the {@code file.properties}
3435
* file at the root of the classpath.</li>
@@ -42,9 +43,12 @@
4243
* and its child directories at any depth.</li>
4344
* </ul>
4445
*
46+
* <p>A resource pattern must not start with a slash ({@code /}).
47+
*
4548
* @author Stephane Nicoll
4649
* @author Brian Clozel
4750
* @author Sebastien Deleuze
51+
* @author Sam Brannen
4852
* @since 6.0
4953
*/
5054
public final class ResourcePatternHint implements ConditionalHint {
@@ -55,6 +59,8 @@ public final class ResourcePatternHint implements ConditionalHint {
5559
private final TypeReference reachableType;
5660

5761
ResourcePatternHint(String pattern, @Nullable TypeReference reachableType) {
62+
Assert.isTrue(!pattern.startsWith("/"),
63+
() -> "Resource pattern [%s] must not start with a '/'".formatted(pattern));
5864
this.pattern = pattern;
5965
this.reachableType = reachableType;
6066
}
@@ -104,4 +110,5 @@ public boolean equals(Object o) {
104110
public int hashCode() {
105111
return Objects.hash(this.pattern, this.reachableType);
106112
}
113+
107114
}

spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2223

2324
/**
2425
* Tests for {@link ResourcePatternHint}.
2526
*
2627
* @author Sebastien Deleuze
28+
* @author Sam Brannen
2729
*/
28-
public class ResourcePatternHintTests {
30+
class ResourcePatternHintTests {
31+
32+
@Test
33+
void patternWithLeadingSlashIsRejected() {
34+
assertThatIllegalArgumentException()
35+
.isThrownBy(() -> new ResourcePatternHint("/file.properties", null))
36+
.withMessage("Resource pattern [/file.properties] must not start with a '/'");
37+
}
2938

3039
@Test
3140
void fileAtRoot() {
@@ -66,4 +75,5 @@ void anyFileInDirectoryAtAnyDepth() {
6675
.accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another")
6776
.rejects("file.properties", "com/file.properties");
6877
}
78+
6979
}

0 commit comments

Comments
 (0)