@@ -754,7 +754,7 @@ private void assertResultTypeIsNotVoid( @Nonnull final Class<?> cls )
754754 }
755755
756756 @ Nonnull
757- private String removeDuplicateQueryParameters ( @ Nonnull String nextLink )
757+ private String removeDuplicateQueryParameters ( @ Nonnull final String nextLink )
758758 {
759759 if ( !(httpClient instanceof UriQueryMerger ) ) {
760760 return nextLink ;
@@ -763,18 +763,20 @@ private String removeDuplicateQueryParameters( @Nonnull String nextLink )
763763 if ( query == null ) {
764764 return nextLink ;
765765 }
766- boolean changed = false ;
766+ final String [] segments = nextLink . split ( " \\ ?" , 2 ) ;
767767 final String [] queryArguments = query .split ("&" );
768768 for ( final String argument : queryArguments ) {
769- if ( nextLink .contains (argument ) ) {
770- changed = true ;
771- nextLink = nextLink .replace (argument , "" );
769+ if ( segments [1 ].contains (argument ) ) {
770+ segments [1 ] = segments [1 ].replace (argument , "" );
772771 }
773772 }
774- if ( changed ) {
775- nextLink = nextLink .replaceAll ("&&+" , "&" ).replace ("?&" , "?" ).replaceAll ("&$" , "" );
776- log .debug ("Altered reference to next link: {}" , nextLink );
773+ if ( nextLink .length () + 1 == segments [0 ].length () + segments [1 ].length () ) {
774+ return nextLink ;
777775 }
778- return nextLink ;
776+ // after removal of arguments clean-up query: fix "?foo=bar&&&one=1", fix "?&one=1", fix "foo=bar&"
777+ segments [1 ] = segments [1 ].replaceAll ("&&+" , "&" ).replace ("?&" , "?" ).replaceAll ("&$" , "" );
778+ final String updatedLink = segments [0 ] + "?" + segments [1 ];
779+ log .debug ("Altered reference to next link: {}" , nextLink );
780+ return updatedLink ;
779781 }
780782}
0 commit comments