You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by st...@apache.org on 2026/09/27 17:07:13 UTC

(hadoop) branch branch-3.5 updated: HADOOP-19815. Document Path trailing slash and URI resolution semantics (#8307)

This is an automated email from the ASF dual-hosted git repository.

steveloughran pushed a commit to branch branch-3.5
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/branch-3.5 by this push:
     new e7d3cb3e3fe HADOOP-19815. Document Path trailing slash and URI resolution semantics (#8307)
e7d3cb3e3fe is described below

commit e7d3cb3e3fec8ca8431bf53cfb5edc6cb99febdf
Author: Deepak Jain (DJ) <de...@users.noreply.github.com>
AuthorDate: Sun Sep 27 10:06:11 2026 -0700

    HADOOP-19815. Document Path trailing slash and URI resolution semantics (#8307)
    
    Contributed by Deepak Jain
---
 .../src/main/java/org/apache/hadoop/fs/Path.java   | 19 +++++++++++++++++-
 .../test/java/org/apache/hadoop/fs/TestPath.java   | 23 ++++++++++++++++++++++
 2 files changed, 41 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 bcbdd0c1932..377a35cb2cc 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
@@ -37,6 +37,18 @@
 /**
  * Names a file or directory in a {@link FileSystem}.
  * Path strings use slash as the directory separator.
+ * <p>
+ * <b>Trailing slash and URI resolution:</b> {@link URI#resolve(String) URI.resolve()} treats
+ * a trailing slash as significant: it resolves a relative path against the last path segment
+ * when the URI has no trailing slash, and against the path as a directory when it has a
+ * trailing slash. The {@link #Path(URI)} constructor preserves the provided URI (after
+ * {@link URI#normalize()}), including any trailing slash, so {@code path.toUri().resolve("x")}
+ * behaves correctly when the Path was built from a URI. The {@link #Path(String)} constructor
+ * (and other constructors that use string path components) normalize the path and
+ * <em>remove trailing slashes</em> from non-root paths, so resolution via
+ * {@code new Path("hdfs://host/dir/").toUri().resolve("x")} yields {@code hdfs://host/x}
+ * rather than {@code hdfs://host/dir/x}. When resolution behavior matters, construct the Path
+ * from a URI or use path concatenation methods instead of relying on {@code toUri().resolve()}.
  */
 @Stringable
 @InterfaceAudience.Public
@@ -178,6 +190,9 @@ private void checkPathArg( String path ) throws IllegalArgumentException {
   /**
    * Construct a path from a String.  Path strings are URIs, but with
    * unescaped elements and some additional normalization.
+   * Trailing slashes are removed from non-root paths during normalization.
+   * For URI resolution behavior that preserves a trailing slash, use
+   * {@link #Path(URI)} instead.
    *
    * @param pathString the path string
    */
@@ -223,7 +238,9 @@ public Path(String pathString) throws IllegalArgumentException {
   }
 
   /**
-   * Construct a path from a URI
+   * Construct a path from a URI. The URI is preserved after {@link URI#normalize()},
+   * including any trailing slash. This allows {@code path.toUri().resolve(relative)}
+   * to resolve correctly when the path represents a directory (trailing slash present).
    *
    * @param aUri the source URI
    */
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 c0406c43eeb..f12f42849e2 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
@@ -40,6 +40,7 @@
 
 import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows;
 import static org.apache.hadoop.test.PlatformAssumptions.assumeWindows;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -560,4 +561,26 @@ public void testSuffixFromRoot() {
     assertNull(root.getParent());
     assertEquals(new Path("/bar"), root.suffix("bar"));
   }
+
+  /**
+   * HADOOP-19815: Path(String) strips trailing slash; Path(URI) preserves it.
+   * So URI.resolve() behavior differs: String-constructed dir path resolves
+   * "x" to host/x; URI-constructed dir path resolves "x" to host/dir/x.
+   */
+  @Test
+  @Timeout(value = 30)
+  public void testTrailingSlashAndUriResolve() throws URISyntaxException {
+    // Path from String: trailing slash is normalized away
+    Path fromString = new Path("hdfs://host/dir/");
+    assertThat(fromString.toUri().toString()).isEqualTo("hdfs://host/dir");
+    assertThat(fromString.toUri().resolve("x"))
+        .isEqualTo(URI.create("hdfs://host/x"));
+
+    // Path from URI: trailing slash is preserved
+    Path fromUri = new Path(new URI("hdfs://host/dir/"));
+    assertThat(fromUri.toUri().toString()).isEqualTo("hdfs://host/dir/");
+    assertThat(fromUri.toUri().resolve("x"))
+        .isEqualTo(URI.create("hdfs://host/dir/x"));
+  }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org