Skip to content

Commit d03102e

Browse files
committed
Allow registration of resource hint for root directory
Prior to this commit, it was not possible to register the root directory as a native image resource; however, it is necessary to be able to register the root directory to enable classpath scanning of the root directory within a native image -- for example, to support resource patterns such as `classpath*:/*.properties`. This commit therefore relaxes the precondition check in the ResourcePatternHint constructor to allow explicit registration of the root directory. Closes gh-29402
1 parent 3c42363 commit d03102e

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
* and its child directories at any depth.</li>
4444
* </ul>
4545
*
46-
* <p>A resource pattern must not start with a slash ({@code /}).
46+
* <p>A resource pattern must not start with a slash ({@code /}) unless it is the
47+
* root directory.
4748
*
4849
* @author Stephane Nicoll
4950
* @author Brian Clozel
@@ -58,13 +59,16 @@ public final class ResourcePatternHint implements ConditionalHint {
5859
@Nullable
5960
private final TypeReference reachableType;
6061

62+
6163
ResourcePatternHint(String pattern, @Nullable TypeReference reachableType) {
62-
Assert.isTrue(!pattern.startsWith("/"),
63-
() -> "Resource pattern [%s] must not start with a '/'".formatted(pattern));
64+
Assert.isTrue(("/".equals(pattern) || !pattern.startsWith("/")),
65+
() -> "Resource pattern [%s] must not start with a '/' unless it is the root directory"
66+
.formatted(pattern));
6467
this.pattern = pattern;
6568
this.reachableType = reachableType;
6669
}
6770

71+
6872
/**
6973
* Return the pattern to use for identifying the resources to match.
7074
* @return the pattern

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ void registerExactMatch() {
8181
.hasSize(2);
8282
}
8383

84+
@Test
85+
void registerRootDirectory() {
86+
this.resourceHints.registerPattern("/");
87+
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
88+
patternOf("/"));
89+
}
90+
8491
@Test
8592
void registerPattern() {
8693
this.resourceHints.registerPattern("com/example/*.properties");

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ class ResourcePatternHintTests {
3333
void patternWithLeadingSlashIsRejected() {
3434
assertThatIllegalArgumentException()
3535
.isThrownBy(() -> new ResourcePatternHint("/file.properties", null))
36-
.withMessage("Resource pattern [/file.properties] must not start with a '/'");
36+
.withMessage("Resource pattern [/file.properties] must not start with a '/' unless it is the root directory");
37+
}
38+
39+
@Test
40+
void rootDirectory() {
41+
ResourcePatternHint hint = new ResourcePatternHint("/", null);
42+
assertThat(hint.toRegex().asMatchPredicate())
43+
.accepts("/")
44+
.rejects("/com/example", "/file.txt");
3745
}
3846

3947
@Test

0 commit comments

Comments
 (0)