JDK-8218962 : URI incorrectly removes path leaf when the relative spec is query only (RFC1808)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8,9,10,11,12,13
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86_64
  • Submitted: 2019-02-13
  • Updated: 2019-08-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
ADDITIONAL SYSTEM INFORMATION :
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1+13-Ubuntu-3ubuntu3.18.10.1)
OpenJDK 64-Bit Server VM (build 11.0.1+13-Ubuntu-3ubuntu3.18.10.1, mixed mode, sharing)


A DESCRIPTION OF THE PROBLEM :
The method URI.resolve(URI uri) does not produce the correct URL if the provided URI describes a relative URI that is only a query.

A similar issue has been previously reported against java.net.URL as JDK-6519518, whose "Won't fix" resolution was explained as "URI is the preferred class for manipulating URLs. Due to legacy compatibility concerns,
we cannot make this change to URL".

I hope that contrary to URL, URI can be fixed

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
This behavior can be seen by using a base URI that includes a filename at the end of its path (i.e. not just a directory) such as that in RFC 1808 section 5.1., and a relative URI that contains only a query, such as "?y" (without the quotes).

For example, one might want to resolve the relative URI "?baz" against the base URI "http://example.com/foo/bar".

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following should be printed to standard output:

http://example.com/foo/bar?baz
ACTUAL -
The following is instead printed to standard output:

http://example.com/foo/?baz

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

public class URITest {

	public static void main(String[] args) throws URISyntaxException {
		URI u = new URI("http://example.com/foo/bar");
		System.out.println(u.resolve(new URI(null, null, null, "baz", null)));
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
In the specific case demonstrated in the source code, one might work around the issue by retrieving the path from the base URI and setting it on the relative URI before asking the base URI to resolve the relative URI:

		URI u = new URI("http://example.com/foo/bar");
		System.out.println(u.resolve(new URI(null, null, u.getPath(), "baz", null)));

This may be very cumbersome in some scenarios.

FREQUENCY : always



Comments
To reproduce the issue, run the attached test case. JDK 8u201 - Fail JDk 11.0.2 - Fail JDK 12-ea + 30 - Fail JDK 13-ea + 6 - Fail Output: http://example.com/foo/?baz
14-02-2019