Normalize URIs returned from FileSystemResource.getURI()

Prior to this commit, if a FileSystemResource was created from a
java.nio.file.Path or java.nio.file.FileSystem, the URI returned from
getURI() was based on the semantics of Path#toUri() which always
includes the "authority component" (i.e., "//") after the scheme (e.g.,
"file://..."). In contrast, if a FileSystemResource is created from a
java.io.File or String, the URI returned from getURI() never includes
the "authority component" due to the semantics of File#toURI().

Consequently, invoking myFileSystemResource.getURI().toString() could
result in "file:/my/path" or "file:///my/path", depending on how the
FileSystemResource was created.

This behavior is not new; however, recent changes in
PathMatchingResourcePatternResolver -- which result in a
FileSystemResource being created from a java.nio.file.Path instead of
from a java.io.File -- make this difference more noticeable to users
and may in fact surface as a breaking change.

To address that issue, this commit revises the implementation of
FileSystemResource.getURI() by normalizing any `file:` URIs that it
returns. Effectively, URIs like "file:///my/path" will now take the
form "file:/my/path".

See gh-29163
Closes gh-29275
This commit is contained in:
Sam Brannen
2022-10-07 13:06:31 +02:00
parent af617c7da9
commit 7175c4ca9a
2 changed files with 21 additions and 4 deletions

View File

@@ -23,7 +23,6 @@ import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -108,7 +107,6 @@ class PathMatchingResourcePatternResolverTests {
assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt");
}
@Disabled("Disabled until gh-29275 is addressed")
@Test
void usingFileProtocolAndAssertingUrlAndUriSyntax() throws Exception {
Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();