Fix recent regression in PathMatchingResourcePatternResolver

Commit 0eb66789ed which introduced generic FileSystem support in
PathMatchingResourcePatternResolver also introduced a regression in that
a matching folder is now returned in the results.

This commit address this by additionally using Files#isRegularFile() in
the predicate used to filter candidates.

Closes gh-29163
This commit is contained in:
Sam Brannen
2022-09-30 13:05:31 +02:00
parent 7a2110d8a5
commit d21eea7de4
4 changed files with 71 additions and 36 deletions

View File

@@ -764,14 +764,15 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
try {
Path rootPath = fileSystem.getPath(rootDir);
String resourcePattern = rootPath.resolve(subPattern).toString();
Predicate<Path> resourcePatternMatches = path -> getPathMatcher().match(resourcePattern, path.toString());
Predicate<Path> isMatchingFile =
path -> Files.isRegularFile(path) && getPathMatcher().match(resourcePattern, path.toString());
if (logger.isTraceEnabled()) {
logger.trace("Searching directory [%s] for files matching pattern [%s]"
.formatted(rootPath.toAbsolutePath(), subPattern));
}
Set<Resource> result = new LinkedHashSet<>();
try (Stream<Path> files = Files.walk(rootPath)) {
files.filter(resourcePatternMatches).sorted().forEach(file -> {
files.filter(isMatchingFile).sorted().forEach(file -> {
try {
result.add(convertToResource(file.toUri()));
}