Skip to content

Commit 015bde2

Browse files
committed
api: Highlight a bug in java.net.URI for those migrating.
Consider: URI uri = URI.create("file://?#"); // These getters distinguish null from empty, as documented. assertThat(uri.getPath()).isEqualTo(""); assertThat(uri.getQuery()).isEqualTo(""); assertThat(uri.getFragment()).isEqualTo(""); // These getters do not, unfortunately. assertThat(uri.getAuthority()).isNull(); assertThat(uri.getHost()).isNull();
1 parent 61d5492 commit 015bde2

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

api/src/main/java/io/grpc/Uri.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@
148148
* itself. RFC 9844 claims to obsolete RFC 6874 because web browsers would not support it. This
149149
* class implements RFC 6874 anyway, mostly to avoid creating a barrier to migration away from
150150
* {@link java.net.URI}.
151+
*
152+
* <p>Some URI components, e.g. scheme, are required while others may or may not be present, e.g.
153+
* authority. {@link Uri} is careful to preserve the distinction between an absent string component
154+
* (getter returns null) and one with an empty value (getter returns ""). {@link java.net.URI} makes
155+
* this distinction too, *except* when it comes to the authority and host components: {@link
156+
* java.net.URI#getAuthority()} and {@link java.net.URI#getHost()} return null when an authority is
157+
* absent, e.g. <code>file:/path</code> as expected. But these methods surprisingly also return null
158+
* when the authority is the empty string, e.g.<code>file:///path</code>. {@link Uri}'s getters
159+
* correctly return null and "" in these cases, respectively, as one would expect.
151160
*/
152161
@Internal
153162
public final class Uri {

api/src/test/java/io/grpc/UriTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ public void parse_emptyPath() throws URISyntaxException {
161161
assertThat(uri.isPathRootless()).isFalse();
162162
}
163163

164+
@Test
165+
public void parse_emptyQuery() {
166+
Uri uri = Uri.create("scheme:?");
167+
assertThat(uri.getScheme()).isEqualTo("scheme");
168+
assertThat(uri.getQuery()).isEmpty();
169+
}
170+
171+
@Test
172+
public void parse_emptyFragment() {
173+
Uri uri = Uri.create("scheme:#");
174+
assertThat(uri.getScheme()).isEqualTo("scheme");
175+
assertThat(uri.getFragment()).isEmpty();
176+
}
177+
178+
@Test
179+
public void parse_emptyUserInfo() {
180+
Uri uri = Uri.create("scheme://@host");
181+
assertThat(uri.getScheme()).isEqualTo("scheme");
182+
assertThat(uri.getAuthority()).isEqualTo("@host");
183+
assertThat(uri.getHost()).isEqualTo("host");
184+
assertThat(uri.getUserInfo()).isEmpty();
185+
assertThat(uri.toString()).isEqualTo("scheme://@host");
186+
}
187+
164188
@Test
165189
public void parse_invalidScheme_throws() {
166190
URISyntaxException e =

0 commit comments

Comments
 (0)