From e0ee3ea86a5b22a23083e8d6c7d215971be42718 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Sat, 20 Feb 2021 01:05:13 -0500 Subject: [PATCH] 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. --- .../beans/propertyeditors/PathEditor.java | 2 +- .../propertyeditors/PathEditorTests.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java index f1edae00c793..3aabd846b904 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java @@ -97,7 +97,7 @@ public void setAsText(String text) throws IllegalArgumentException { if (resource == null) { setValue(null); } - else if (!resource.exists() && nioPathCandidate) { + else if (!resource.isFile() && !resource.exists() && nioPathCandidate) { setValue(Paths.get(text).normalize()); } else { diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java index 40354fc44643..60df90c7f103 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java @@ -76,6 +76,30 @@ public void testAbsolutePath() throws Exception { assertThat(condition).isTrue(); } + @Test + public void testWindowsAbsolutePath() throws Exception { + PropertyEditor pathEditor = new PathEditor(); + pathEditor.setAsText("C:\\no_way_this_file_is_found.doc"); + Object value = pathEditor.getValue(); + boolean condition1 = value instanceof Path; + assertThat(condition1).isTrue(); + Path path = (Path) value; + boolean condition = !path.toFile().exists(); + assertThat(condition).isTrue(); + } + + @Test + public void testWindowsAbsoluteFilePath() throws Exception { + PropertyEditor pathEditor = new PathEditor(); + pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc"); + Object value = pathEditor.getValue(); + boolean condition1 = value instanceof Path; + assertThat(condition1).isTrue(); + Path path = (Path) value; + boolean condition = !path.toFile().exists(); + assertThat(condition).isTrue(); + } + @Test public void testUnqualifiedPathNameFound() throws Exception { PropertyEditor pathEditor = new PathEditor();