Skip to content

Commit e0ee3ea

Browse files
committed
Fix handling of file: paths to non-existent files
For setAsText, if the text argument is a file: URL for a path that does not exist, Paths.get(text) is called where text is a file: URL, which doesn't work - the result is an InvalidPathException. To fix this issue, also check that the resource isn't a file before calling Paths.get(). That way, resources that are files skip to the other branch.
1 parent 274db2f commit e0ee3ea

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void setAsText(String text) throws IllegalArgumentException {
9797
if (resource == null) {
9898
setValue(null);
9999
}
100-
else if (!resource.exists() && nioPathCandidate) {
100+
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
101101
setValue(Paths.get(text).normalize());
102102
}
103103
else {

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

+24
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ public void testAbsolutePath() throws Exception {
7676
assertThat(condition).isTrue();
7777
}
7878

79+
@Test
80+
public void testWindowsAbsolutePath() throws Exception {
81+
PropertyEditor pathEditor = new PathEditor();
82+
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
83+
Object value = pathEditor.getValue();
84+
boolean condition1 = value instanceof Path;
85+
assertThat(condition1).isTrue();
86+
Path path = (Path) value;
87+
boolean condition = !path.toFile().exists();
88+
assertThat(condition).isTrue();
89+
}
90+
91+
@Test
92+
public void testWindowsAbsoluteFilePath() throws Exception {
93+
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();
101+
}
102+
79103
@Test
80104
public void testUnqualifiedPathNameFound() throws Exception {
81105
PropertyEditor pathEditor = new PathEditor();

0 commit comments

Comments
 (0)