Skip to content

Commit e4e2212

Browse files
committed
Bypass root path resolution for "file:" prefix only
See gh-26702
1 parent 34fcbfb commit e4e2212

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
import org.springframework.core.io.Resource;
2828
import org.springframework.core.io.ResourceEditor;
29-
import org.springframework.core.io.ResourceLoader;
3029
import org.springframework.util.Assert;
30+
import org.springframework.util.ResourceUtils;
3131

3232
/**
3333
* Editor for {@code java.nio.file.Path}, to directly populate a Path
@@ -74,7 +74,7 @@ public PathEditor(ResourceEditor resourceEditor) {
7474

7575
@Override
7676
public void setAsText(String text) throws IllegalArgumentException {
77-
boolean nioPathCandidate = !text.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX);
77+
boolean nioPathCandidate = !text.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX);
7878
if (nioPathCandidate && !text.startsWith("/")) {
7979
try {
8080
URI uri = new URI(text);
@@ -85,9 +85,13 @@ public void setAsText(String text) throws IllegalArgumentException {
8585
return;
8686
}
8787
}
88-
catch (URISyntaxException | FileSystemNotFoundException ex) {
89-
// Not a valid URI (let's try as Spring resource location),
90-
// or a URI scheme not registered for NIO (let's try URL
88+
catch (URISyntaxException ex) {
89+
// Not a valid URI; potentially a Windows-style path after
90+
// a file prefix (let's try as Spring resource location)
91+
nioPathCandidate = !text.startsWith(ResourceUtils.FILE_URL_PREFIX);
92+
}
93+
catch (FileSystemNotFoundException ex) {
94+
// URI scheme not registered for NIO (let's try URL
9195
// protocol handlers via Spring's resource mechanism).
9296
}
9397
}
@@ -97,8 +101,7 @@ public void setAsText(String text) throws IllegalArgumentException {
97101
if (resource == null) {
98102
setValue(null);
99103
}
100-
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
101-
// Prefer getFile().toPath() below for non-existent file handles
104+
else if (nioPathCandidate && !resource.exists()) {
102105
setValue(Paths.get(text).normalize());
103106
}
104107
else {

spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java

+21-25
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public void testClasspathPathName() {
3939
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
4040
ClassUtils.getShortName(getClass()) + ".class");
4141
Object value = pathEditor.getValue();
42-
boolean condition = value instanceof Path;
43-
assertThat(condition).isTrue();
42+
assertThat(value instanceof Path).isTrue();
4443
Path path = (Path) value;
4544
assertThat(path.toFile().exists()).isTrue();
4645
}
@@ -57,47 +56,46 @@ public void testWithNonExistentPath() {
5756
PropertyEditor pathEditor = new PathEditor();
5857
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
5958
Object value = pathEditor.getValue();
60-
boolean condition1 = value instanceof Path;
61-
assertThat(condition1).isTrue();
59+
assertThat(value instanceof Path).isTrue();
6260
Path path = (Path) value;
63-
boolean condition = !path.toFile().exists();
64-
assertThat(condition).isTrue();
61+
assertThat(!path.toFile().exists()).isTrue();
6562
}
6663

6764
@Test
6865
public void testAbsolutePath() {
6966
PropertyEditor pathEditor = new PathEditor();
7067
pathEditor.setAsText("/no_way_this_file_is_found.doc");
7168
Object value = pathEditor.getValue();
72-
boolean condition1 = value instanceof Path;
73-
assertThat(condition1).isTrue();
69+
assertThat(value instanceof Path).isTrue();
7470
Path path = (Path) value;
75-
boolean condition = !path.toFile().exists();
76-
assertThat(condition).isTrue();
71+
assertThat(!path.toFile().exists()).isTrue();
7772
}
7873

7974
@Test
8075
public void testWindowsAbsolutePath() {
8176
PropertyEditor pathEditor = new PathEditor();
8277
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
8378
Object value = pathEditor.getValue();
84-
boolean condition1 = value instanceof Path;
85-
assertThat(condition1).isTrue();
79+
assertThat(value instanceof Path).isTrue();
8680
Path path = (Path) value;
87-
boolean condition = !path.toFile().exists();
88-
assertThat(condition).isTrue();
81+
assertThat(!path.toFile().exists()).isTrue();
8982
}
9083

9184
@Test
9285
public void testWindowsAbsoluteFilePath() {
9386
PropertyEditor pathEditor = new PathEditor();
94-
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
95-
Object value = pathEditor.getValue();
96-
boolean condition1 = value instanceof Path;
97-
assertThat(condition1).isTrue();
98-
Path path = (Path) value;
99-
boolean condition = !path.toFile().exists();
100-
assertThat(condition).isTrue();
87+
try {
88+
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
89+
Object value = pathEditor.getValue();
90+
assertThat(value instanceof Path).isTrue();
91+
Path path = (Path) value;
92+
assertThat(!path.toFile().exists()).isTrue();
93+
}
94+
catch (IllegalArgumentException ex) {
95+
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
96+
throw ex;
97+
}
98+
}
10199
}
102100

103101
@Test
@@ -107,8 +105,7 @@ public void testUnqualifiedPathNameFound() {
107105
ClassUtils.getShortName(getClass()) + ".class";
108106
pathEditor.setAsText(fileName);
109107
Object value = pathEditor.getValue();
110-
boolean condition = value instanceof Path;
111-
assertThat(condition).isTrue();
108+
assertThat(value instanceof Path).isTrue();
112109
Path path = (Path) value;
113110
File file = path.toFile();
114111
assertThat(file.exists()).isTrue();
@@ -126,8 +123,7 @@ public void testUnqualifiedPathNameNotFound() {
126123
ClassUtils.getShortName(getClass()) + ".clazz";
127124
pathEditor.setAsText(fileName);
128125
Object value = pathEditor.getValue();
129-
boolean condition = value instanceof Path;
130-
assertThat(condition).isTrue();
126+
assertThat(value instanceof Path).isTrue();
131127
Path path = (Path) value;
132128
File file = path.toFile();
133129
assertThat(file.exists()).isFalse();

0 commit comments

Comments
 (0)