Skip to content

Commit 4b6b12b

Browse files
committed
Bypass root path resolution for "file:" prefix only
Closes gh-26702
1 parent ab0e8f0 commit 4b6b12b

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

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

Lines changed: 10 additions & 7 deletions
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

Lines changed: 11 additions & 22 deletions
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,39 @@ 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();
9487
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
9588
Object value = pathEditor.getValue();
96-
boolean condition1 = value instanceof Path;
97-
assertThat(condition1).isTrue();
89+
assertThat(value instanceof Path).isTrue();
9890
Path path = (Path) value;
99-
boolean condition = !path.toFile().exists();
100-
assertThat(condition).isTrue();
91+
assertThat(!path.toFile().exists()).isTrue();
10192
}
10293

10394
@Test
@@ -107,8 +98,7 @@ public void testUnqualifiedPathNameFound() {
10798
ClassUtils.getShortName(getClass()) + ".class";
10899
pathEditor.setAsText(fileName);
109100
Object value = pathEditor.getValue();
110-
boolean condition = value instanceof Path;
111-
assertThat(condition).isTrue();
101+
assertThat(value instanceof Path).isTrue();
112102
Path path = (Path) value;
113103
File file = path.toFile();
114104
assertThat(file.exists()).isTrue();
@@ -126,8 +116,7 @@ public void testUnqualifiedPathNameNotFound() {
126116
ClassUtils.getShortName(getClass()) + ".clazz";
127117
pathEditor.setAsText(fileName);
128118
Object value = pathEditor.getValue();
129-
boolean condition = value instanceof Path;
130-
assertThat(condition).isTrue();
119+
assertThat(value instanceof Path).isTrue();
131120
Path path = (Path) value;
132121
File file = path.toFile();
133122
assertThat(file.exists()).isFalse();

0 commit comments

Comments
 (0)