From d476e5cb36ccbf5735742b6b623919ffe3f5bce2 Mon Sep 17 00:00:00 2001 From: Stefan Hahmann Date: Thu, 27 Jun 2024 14:47:29 +0200 Subject: [PATCH 1/2] Ignore empty Strings during String to FileArray conversion * The current state of the code is that an empty String, is converted to a new File("") object with an empty String * A File object instantiated by this will be a File object with an absolute Path that is equivalent to the Path from which the application was started * The result of this is relatively unpredictable * Thus empty String should not be converted to File object and not be added to the output --- src/main/java/org/scijava/convert/FileListConverters.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/scijava/convert/FileListConverters.java b/src/main/java/org/scijava/convert/FileListConverters.java index ed3da126e..47a81053c 100644 --- a/src/main/java/org/scijava/convert/FileListConverters.java +++ b/src/main/java/org/scijava/convert/FileListConverters.java @@ -82,6 +82,8 @@ public T convert(final Object src, final Class dest) { final String[] tokens = StringUtils.splitUnquoted((String) src, ","); final List fileList = new ArrayList<>(); for (final String filePath : tokens) { + if ( filePath.isEmpty() ) + continue; fileList.add(new File(filePath.replaceAll("^\"|\"$", ""))); } return (T) fileList.toArray(new File[fileList.size()]); From 8dc4c0e4146f779fd05c068974ad0b450bdc47bf Mon Sep 17 00:00:00 2001 From: Stefan Hahmann Date: Thu, 27 Jun 2024 15:33:44 +0200 Subject: [PATCH 2/2] Add assertion of 0 length File array in case of an empty String provided for String to File Array conversion --- src/test/java/org/scijava/convert/FileListConverterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/scijava/convert/FileListConverterTest.java b/src/test/java/org/scijava/convert/FileListConverterTest.java index 0276fe63a..71afaa2dc 100644 --- a/src/test/java/org/scijava/convert/FileListConverterTest.java +++ b/src/test/java/org/scijava/convert/FileListConverterTest.java @@ -69,6 +69,7 @@ public void testStringToFileArrayConverter() { conv.convert(path, File[].class)[0]); assertEquals("Wrong file name", new File("C:\\temp"), conv.convert(path, File[].class)[1]); + assertEquals( 0, conv.convert( "", File[].class ).length ); } @Test