Skip to content

Commit

Permalink
Prevent resource hint registration for pattern with leading slash
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Sep 6, 2022
1 parent 04bb336 commit 10ade23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
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>
Expand All @@ -42,9 +43,12 @@
* 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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -104,4 +110,5 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(this.pattern, this.reachableType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@
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() {
Expand Down Expand Up @@ -66,4 +75,5 @@ void anyFileInDirectoryAtAnyDepth() {
.accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another")
.rejects("file.properties", "com/file.properties");
}

}

0 comments on commit 10ade23

Please sign in to comment.