From 6bc906971ab1fc52b27cdaa9a7fade4d7bb331af Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 4 Mar 2013 15:59:22 +0100 Subject: [PATCH] #26, #39 - Fixed potential transitive assertion error in UrComponentsBuilder. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a String piped into LinkBuilderSupport.slash(…) is empty, the call to UriComponentsBuilder.fromUriString(…) fails with an assertion exception. We now guard against this specific case by returning the current builder instance in case an empty String (potentially returned from the toString() method of the object handed in) would be handed to the UriComponentsBuilder. The issue was introduced by commit 84efebc7a65afd7b5decbae054af13406ccb59c2. --- .../hateoas/core/LinkBuilderSupport.java | 9 ++- .../core/LinkBuilderSupportUnitTest.java | 55 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/hateoas/core/LinkBuilderSupportUnitTest.java diff --git a/src/main/java/org/springframework/hateoas/core/LinkBuilderSupport.java b/src/main/java/org/springframework/hateoas/core/LinkBuilderSupport.java index c5af037a..17b97429 100644 --- a/src/main/java/org/springframework/hateoas/core/LinkBuilderSupport.java +++ b/src/main/java/org/springframework/hateoas/core/LinkBuilderSupport.java @@ -21,6 +21,7 @@ import org.springframework.hateoas.Identifiable; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkBuilder; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; @@ -59,7 +60,13 @@ public abstract class LinkBuilderSupport implements LinkB return slash((Identifiable) object); } - UriComponents components = UriComponentsBuilder.fromUriString(object.toString()).build(); + String path = object.toString(); + + if (!StringUtils.hasText(path)) { + return getThis(); + } + + UriComponents components = UriComponentsBuilder.fromUriString(path).build(); UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uriComponents.toUri()); for (String pathSegment : components.getPathSegments()) { diff --git a/src/test/java/org/springframework/hateoas/core/LinkBuilderSupportUnitTest.java b/src/test/java/org/springframework/hateoas/core/LinkBuilderSupportUnitTest.java new file mode 100644 index 00000000..d597f6c7 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/core/LinkBuilderSupportUnitTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.core; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.hateoas.TestUtils; +import org.springframework.web.util.UriComponentsBuilder; + +/** + * Unit tests for {@link LinkBuilderSupport}. + * + * @author Oliver Gierke + */ +public class LinkBuilderSupportUnitTest extends TestUtils { + + @Test + public void callingSlashWithEmptyStringIsNoOp() { + + SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance()); + assertThat(builder.slash(""), is(builder)); + } + + static class SampleLinkBuilder extends LinkBuilderSupport { + + public SampleLinkBuilder(UriComponentsBuilder builder) { + super(builder); + } + + @Override + protected SampleLinkBuilder getThis() { + return this; + } + + @Override + protected SampleLinkBuilder createNewInstance(UriComponentsBuilder builder) { + return new SampleLinkBuilder(builder); + } + } +}