Skip to content

Commit

Permalink
UriComponentsBuilder parse of empty fragments
Browse files Browse the repository at this point in the history
Check for an empty fragment in UriComponentsBuilder.fromUriString(...)
to prevent the invocation of fragment(...).

Previously, UriComponentsBuilder.fromUriString(...) threw an exception
in the case of an empty fragment being provided (e.g. /example#).

Issue: SPR-10363
  • Loading branch information
odrotbohm authored and philwebb committed Mar 9, 2013
1 parent 8e4e0f3 commit 3eb3610
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Phillip Webb
* @author Oliver Gierke
* @since 3.1
* @see #newInstance()
* @see #fromPath(String)
Expand Down Expand Up @@ -204,7 +205,10 @@ public static UriComponentsBuilder fromUriString(String uri) {
builder.path(path);
builder.query(query);
}
builder.fragment(fragment);

if (StringUtils.hasText(fragment)) {
builder.fragment(fragment);
}

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
import java.util.Map;

import org.junit.Test;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

/**
* @author Arjen Poutsma
* @author Phillip Webb
* @author Oliver Gierke
*/
public class UriComponentsBuilderTests {

Expand Down Expand Up @@ -354,4 +354,11 @@ public void emptySegments() throws Exception {
assertThat(UriComponentsBuilder.fromUriString("http://example.com/abc/").path("/x/").path("/y/z").build().toString(), equalTo("http://example.com/abc/x/y/z"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com/abc/").pathSegment("x").path("y").build().toString(), equalTo("http://example.com/abc/x/y"));
}

@Test
public void parsesEmptyFragment() {
UriComponents components = UriComponentsBuilder.fromUriString("/example#").build();
assertThat(components.getFragment(), is(nullValue()));
assertThat(components.toString(), equalTo("/example"));
}
}

0 comments on commit 3eb3610

Please sign in to comment.