From 45d7a33ca55b6237f175f3dcac5c605dbe675a13 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Wed, 27 Jan 2021 19:46:11 +0100 Subject: [PATCH] fix(mpconfig): Do not cutoff file extensions for property names in DirConfigSource As requested by @pdudits, do not cut off file extensions silently. People on Windows can deal with text files without a file extension, better stay consistent. Relates to #5006 --- .../config/source/DirConfigSource.java | 20 ++----------------- .../config/source/DirConfigSourceTest.java | 14 ++----------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java index a16c21c6fc9..e8bdc93f99e 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java @@ -418,30 +418,14 @@ public final static String parsePropertyNameFromPath(Path path, Path rootDir) { String property = ""; if (! path.getParent().equals(rootDir)) property += rootDir.relativize(path.getParent()).toString() + File.separatorChar; - // 2. ignore all file suffixes after last dot - property += removeFileExtension(path.getFileName().toString()); + // 2. add the file name (might be used for mangling in the future) + property += path.getFileName(); // 3. replace all path seps with a ".", property = property.replace(File.separatorChar, '.'); // so "/config/foo/bar/test/one.txt" becomes "foo/bar/test/one.txt" becomes "foo.bar.test.one" property name return property; } - /** - * Litte helper to remove the file extension (not present in Java std functionality) - * @param filename A filename containing a dot, marking the start of the file extension - * @return Filename without a suffix (if present) - */ - public final static String removeFileExtension(String filename) { - if (filename == null || ! filename.contains(".")) - return filename; - int lastIndex = filename.lastIndexOf('.'); - // dot does not belong to file, but parent dir or - // extension is longer than 3 chars (all clear text formats would have 3 chars max) - if (filename.lastIndexOf(File.separatorChar) > lastIndex || lastIndex < filename.length() - 4) - return filename; - return filename.substring(0, lastIndex); - } - /** * Actually read the data from the file, assuming UTF-8 formatted content, creating properties from it. * @param path The file to read diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java index ff341e401a6..5300557f64c 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java @@ -71,7 +71,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DirConfigSourceTest { @@ -179,17 +180,6 @@ public void testParsePropertyNameFromPath() { examples.put(subpath( "foo", "bar.test", "ex"), "foo.bar.test.ex"); examples.put(subpath( "foo.bar", "test", "ex"), "foo.bar.test.ex"); - // we ignore the last file extension if not more than 3 chars. - // this might lead to unexpected behaviour for a user. - // best advice: do not use dots in filename, only in directory names. - examples.put(subpath( "foo", "bar", "test", "ex.txt"), "foo.bar.test.ex"); - examples.put(subpath( "foo", "bar", "test", "ex.tar.gz"), "foo.bar.test.ex.tar"); - examples.put(subpath( "foo", "bar", "test", "ex.helo"), "foo.bar.test.ex.helo"); - examples.put(subpath( "foo.bar", "test.ex"), "foo.bar.test"); - examples.put(subpath( "foo", "bar.test.ex"), "foo.bar.test"); - examples.put(subpath( "foo.bar.test.ex"), "foo.bar.test"); - examples.put(subpath( "foo", "bar", "test.ex"), "foo.bar.test"); - // when & then for (Map.Entry ex : examples.entrySet()) { //System.out.println(ex.getKey()+" = "+ex.getValue());