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 84efebc7a6.
This commit is contained in:
@@ -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<T extends LinkBuilder> 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()) {
|
||||
|
||||
@@ -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<SampleLinkBuilder> {
|
||||
|
||||
public SampleLinkBuilder(UriComponentsBuilder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SampleLinkBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SampleLinkBuilder createNewInstance(UriComponentsBuilder builder) {
|
||||
return new SampleLinkBuilder(builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user