Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8272702

Resolving URI relative path with no / may lead to incorrect toString

XMLWordPrintable

    • b12
    • generic
    • generic
    • Verified

        ADDITIONAL SYSTEM INFORMATION :
        Fedora 33 Linux 5.13.9-100.fc33.x86_64
        openjdk version "16.0.2" 2021-07-20


        A DESCRIPTION OF THE PROBLEM :
        Given an absolute URI: 'http://example.com' (note no trailing '/')
        and a relative path: 'test'
        intuitively one would expect resolving to result in 'http://example.com/test'
        This is how Python behaves, for example.

        However, doing so in Java leads to a surprising result: 'http://example.comtest'
        Seemingly the relative path ends up in the authority!
        Under the hood, it turns out that 'test' is correctly set as the path, but there is never a '/' character introduced to separate the authority from the path.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        The attached test case shows the issue.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        http://example.com/test
        ACTUAL -
        http://example.comtest

        ---------- BEGIN SOURCE ----------
        import java.net.URI;

        class UriResolve {
            public static void main(String[] args) {
                var base = URI.create("http://example.com");
                var rel = URI.create("test");
                System.out.format("base a: %s p: %s u: %s%n", base.getAuthority(), base.getPath(), base);
                System.out.format("rel a: %s p: %s u: %s%n", rel.getAuthority(), rel.getPath(), rel);
                var res = base.resolve(rel);
                System.out.format("res a: %s p: %s u: %s%n", res.getAuthority(), res.getPath(), res);
            }
        }

        ---------- END SOURCE ----------

        FREQUENCY : always


              dfuchs Daniel Fuchs
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: