Skip to content

Commit 3eb3610

Browse files
odrotbohmphilwebb
authored andcommitted
UriComponentsBuilder parse of empty fragments
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
1 parent 8e4e0f3 commit 3eb3610

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Arjen Poutsma
4949
* @author Rossen Stoyanchev
5050
* @author Phillip Webb
51+
* @author Oliver Gierke
5152
* @since 3.1
5253
* @see #newInstance()
5354
* @see #fromPath(String)
@@ -204,7 +205,10 @@ public static UriComponentsBuilder fromUriString(String uri) {
204205
builder.path(path);
205206
builder.query(query);
206207
}
207-
builder.fragment(fragment);
208+
209+
if (StringUtils.hasText(fragment)) {
210+
builder.fragment(fragment);
211+
}
208212

209213
return builder;
210214
}

spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import java.util.Map;
2424

2525
import org.junit.Test;
26-
2726
import org.springframework.util.LinkedMultiValueMap;
2827
import org.springframework.util.MultiValueMap;
2928

30-
import static org.hamcrest.Matchers.equalTo;
29+
import static org.hamcrest.Matchers.*;
3130
import static org.junit.Assert.*;
3231

3332
/**
3433
* @author Arjen Poutsma
3534
* @author Phillip Webb
35+
* @author Oliver Gierke
3636
*/
3737
public class UriComponentsBuilderTests {
3838

@@ -354,4 +354,11 @@ public void emptySegments() throws Exception {
354354
assertThat(UriComponentsBuilder.fromUriString("http://example.com/abc/").path("/x/").path("/y/z").build().toString(), equalTo("http://example.com/abc/x/y/z"));
355355
assertThat(UriComponentsBuilder.fromUriString("http://example.com/abc/").pathSegment("x").path("y").build().toString(), equalTo("http://example.com/abc/x/y"));
356356
}
357+
358+
@Test
359+
public void parsesEmptyFragment() {
360+
UriComponents components = UriComponentsBuilder.fromUriString("/example#").build();
361+
assertThat(components.getFragment(), is(nullValue()));
362+
assertThat(components.toString(), equalTo("/example"));
363+
}
357364
}

0 commit comments

Comments
 (0)