@@ -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 that
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,27 @@ 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 anything else. In certain cases, you can use {@link
849+ * #setFragment(String)} to have the fragment percent-encoded for you instead, but see that
850+ * method's Javadoc for its limitations.
851+ *
852+ * <p>This field is optional.
853+ *
854+ * @param fragment the new fragment component, or null to clear this field
855+ * @return this, for fluent building
856+ * @throws IllegalArgumentException if 'fragment' contains forbidden characters
857+ */
835858 @ CanIgnoreReturnValue
836- Builder setRawFragment (String fragment ) {
837- checkPercentEncodedArg (fragment , "fragment" , fragmentChars );
859+ public Builder setRawFragment (@ Nullable String fragment ) {
860+ if (fragment != null ) {
861+ checkPercentEncodedArg (fragment , "fragment" , fragmentChars );
862+ }
838863 this .fragment = fragment ;
839864 return this ;
840865 }
0 commit comments