@@ -821,6 +821,13 @@ public Builder setRawQuery(@Nullable String query) {
821821 * <p>The fragment can contain any string of codepoints. Codepoints that can't be encoded
822822 * literally will be percent-encoded for you as UTF-8.
823823 *
824+ * <p>NB: Choose carefully between this method and {@link #setRawFragment(String)}. Many URI
825+ * schemes embed further structure in the fragment that isn't part of the RFC 3986 generic
826+ * syntax. These schemes often use internal delimiters that must be carefully percent-encoded in
827+ * ways that this method doesn't understand. See {@link #getFragment()} for an example. In this
828+ * case, callers should percent-encode externally then call {@link #setRawFragment(String)}
829+ * instead.
830+ *
824831 * <p>This field is optional.
825832 *
826833 * @param fragment the new fragment component, or null to clear this field
@@ -832,9 +839,26 @@ public Builder setFragment(@Nullable String fragment) {
832839 return this ;
833840 }
834841
842+ /**
843+ * Specifies the fragment component of the new URI, already percent-encoded, exactly as it will
844+ * appear after the '#' delimiter in the string form of the built URI.
845+ *
846+ * <p>NB: Choose carefully between this method and {@link #setFragment(String)}. {@code
847+ * fragment} must only contain codepoints from RFC 3986's "fragment" character class. Use
848+ * percent-encoding and UTF-8 to represent any other characters, or call {@link
849+ * #setFragment(String)} to automatically percent-encode just the characters that need it.
850+ *
851+ * <p>This field is optional.
852+ *
853+ * @param fragment the new fragment component, or null to clear this field
854+ * @return this, for fluent building
855+ * @throws IllegalArgumentException if 'fragment' contains forbidden characters
856+ */
835857 @ CanIgnoreReturnValue
836- Builder setRawFragment (String fragment ) {
837- checkPercentEncodedArg (fragment , "fragment" , fragmentChars );
858+ public Builder setRawFragment (@ Nullable String fragment ) {
859+ if (fragment != null ) {
860+ checkPercentEncodedArg (fragment , "fragment" , fragmentChars );
861+ }
838862 this .fragment = fragment ;
839863 return this ;
840864 }
0 commit comments