diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 171441ba13ac..1a64481252dc 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -236,7 +236,8 @@ public static UriComponentsBuilder fromUriString(String uri) throws InvalidUrlEx builder.port(urlRecord.port().toString()); } if (urlRecord.path().isOpaque()) { - builder.schemeSpecificPart(urlRecord.path().toString()); + String ssp = urlRecord.path() + urlRecord.search(); + builder.schemeSpecificPart(ssp); } else { builder.path(urlRecord.path().toString()); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index 949b1b91706c..a5face72d171 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -196,6 +196,16 @@ void fromUriString() { assertThat(result.getQuery()).isNull(); assertThat(result.getFragment()).isEqualTo("baz"); + result = UriComponentsBuilder.fromUriString("mailto:user@example.com?subject=foo").build(); + assertThat(result.getScheme()).isEqualTo("mailto"); + assertThat(result.getUserInfo()).isNull(); + assertThat(result.getHost()).isNull(); + assertThat(result.getPort()).isEqualTo(-1); + assertThat(result.getSchemeSpecificPart()).isEqualTo("user@example.com?subject=foo"); + assertThat(result.getPath()).isNull(); + assertThat(result.getQuery()).isNull(); + assertThat(result.getFragment()).isNull(); + result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28").build(); assertThat(result.getScheme()).isNull(); assertThat(result.getUserInfo()).isNull(); diff --git a/spring-web/src/test/java/org/springframework/web/util/UrlParserTests.java b/spring-web/src/test/java/org/springframework/web/util/UrlParserTests.java index ce8d884afff0..2dc1f51a259b 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UrlParserTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UrlParserTests.java @@ -61,8 +61,31 @@ private void testParse(String input, String scheme, @Nullable String host, @Null else { assertThat(result.port()).as("Port is not null").isNull(); } + assertThat(result.hasOpaquePath()).as("Result has opaque path").isFalse(); assertThat(result.path().toString()).as("Invalid path").isEqualTo(path); assertThat(result.query()).as("Invalid query").isEqualTo(query); assertThat(result.fragment()).as("Invalid fragment").isEqualTo(fragment); } + + @Test + void parseOpaque() { + testParseOpaque("mailto:user@example.com?subject=foo", "user@example.com", "subject=foo"); + + } + + void testParseOpaque(String input, String path, @Nullable String query) { + UrlParser.UrlRecord result = UrlParser.parse("mailto:user@example.com?subject=foo", EMPTY_URL_RECORD, null, null); + + + assertThat(result.scheme()).as("Invalid scheme").isEqualTo("mailto"); + assertThat(result.hasOpaquePath()).as("Result has no opaque path").isTrue(); + assertThat(result.path().toString()).as("Invalid path").isEqualTo(path); + if (query != null) { + assertThat(result.query()).as("Query is null").isNotNull(); + assertThat(result.query()).as("Invalid query").isEqualTo(query); + } + else { + assertThat(result.query()).as("Query is not null").isNull(); + } + } }