From 3eb361066067438efbcf0758e78f229e2655a599 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 8 Mar 2013 17:22:13 +0100 Subject: [PATCH] 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 --- .../web/util/UriComponentsBuilder.java | 6 +++++- .../web/util/UriComponentsBuilderTests.java | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 30dcac0f84..7fbd0178f2 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -48,6 +48,7 @@ import org.springframework.web.util.HierarchicalUriComponents.PathComponent; * @author Arjen Poutsma * @author Rossen Stoyanchev * @author Phillip Webb + * @author Oliver Gierke * @since 3.1 * @see #newInstance() * @see #fromPath(String) @@ -204,7 +205,10 @@ public class UriComponentsBuilder { builder.path(path); builder.query(query); } - builder.fragment(fragment); + + if (StringUtils.hasText(fragment)) { + builder.fragment(fragment); + } return builder; } diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index 25fbb98d59..fd60e2dcd9 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -23,16 +23,16 @@ import java.util.HashMap; 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 { @@ -354,4 +354,11 @@ public class UriComponentsBuilderTests { 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")); + } }