From 9029bba5dcb5a0379b27b32d811756de965d706c Mon Sep 17 00:00:00 2001 From: Patrick GRANDJEAN Date: Thu, 18 May 2023 19:46:55 -0400 Subject: [PATCH] HADOOP-18652. Path.suffix raises NullPointerException (#5653). Contributed by Patrick Grandjean. Reviewed-by: Wei-Chiu Chuang Signed-off-by: Ayush Saxena --- .../src/main/java/org/apache/hadoop/fs/Path.java | 7 ++++++- .../src/test/java/org/apache/hadoop/fs/TestPath.java | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java index 2649d279aa..9daa782e1c 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java @@ -447,7 +447,12 @@ public Path getParent() { * @return a new path with the suffix added */ public Path suffix(String suffix) { - return new Path(getParent(), getName()+suffix); + Path parent = getParent(); + if (parent == null) { + return new Path("/", getName() + suffix); + } + + return new Path(parent, getName() + suffix); } @Override diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java index 18f88084c3..e775d1d069 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java @@ -528,4 +528,11 @@ public void testSerDeser() throws Throwable { } } + + @Test(timeout = 30000) + public void testSuffixFromRoot() { + Path root = new Path("/"); + Assert.assertNull(root.getParent()); + Assert.assertEquals(new Path("/bar"), root.suffix("bar")); + } }