diff --git a/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java b/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java index ee108d54769..8ca54f5ae3a 100644 --- a/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java +++ b/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java @@ -82,7 +82,12 @@ class ResourceHintsWriter { } private String patternToRegexp(String pattern) { - return Arrays.stream(pattern.split("\\*")).map(Pattern::quote).collect(Collectors.joining(".*")); + String prefix = (pattern.startsWith("*") ? ".*" : ""); + String suffix = (pattern.endsWith("*") ? ".*" : ""); + return Arrays.stream(pattern.split("\\*")) + .filter(s -> !s.isEmpty()) + .map(Pattern::quote) + .collect(Collectors.joining(".*", prefix, suffix)); } private void addIfNotEmpty(Map attributes, String name, @Nullable Object value) { diff --git a/spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java b/spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java index 9f950f4558d..2774f59919b 100644 --- a/spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java +++ b/spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java @@ -57,7 +57,21 @@ public class ResourceHintsWriterTests { } @Test - void registerPattern() throws JSONException { + void registerWildcardAtTheBeginningPattern() throws JSONException { + ResourceHints hints = new ResourceHints(); + hints.registerPattern("*.properties"); + assertEquals(""" + { + "resources": { + "includes": [ + { "pattern": ".*\\\\Q.properties\\\\E"} + ] + } + }""", hints); + } + + @Test + void registerWildcardInTheMiddlePattern() throws JSONException { ResourceHints hints = new ResourceHints(); hints.registerPattern("com/example/*.properties"); assertEquals(""" @@ -70,6 +84,20 @@ public class ResourceHintsWriterTests { }""", hints); } + @Test + void registerWildcardAtTheEndPattern() throws JSONException { + ResourceHints hints = new ResourceHints(); + hints.registerPattern("static/*"); + assertEquals(""" + { + "resources": { + "includes": [ + { "pattern": "\\\\Qstatic/\\\\E.*"} + ] + } + }""", hints); + } + @Test void registerPatternWithIncludesAndExcludes() throws JSONException { ResourceHints hints = new ResourceHints();