Skip to content

Commit 247771c

Browse files
committed
UriComponentBuilder allows for multiple independent build() calls on same builder instance
Issue: SPR-11885 (cherry picked from commit c73ac07)
1 parent 1be3a6c commit 247771c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ static final class PathSegmentComponent implements PathComponent {
649649
private final List<String> pathSegments;
650650

651651
public PathSegmentComponent(List<String> pathSegments) {
652-
this.pathSegments = Collections.unmodifiableList(pathSegments);
652+
this.pathSegments = Collections.unmodifiableList(new ArrayList<String>(pathSegments));
653653
}
654654

655655
public String getPath() {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424

2525
import org.junit.Test;
26+
2627
import org.springframework.util.LinkedMultiValueMap;
2728
import org.springframework.util.MultiValueMap;
2829

@@ -50,6 +51,28 @@ public void plain() throws URISyntaxException {
5051
assertEquals("Invalid result URI", expected, result.toUri());
5152
}
5253

54+
@Test
55+
public void multipleFromSameBuilder() throws URISyntaxException {
56+
UriComponentsBuilder builder = UriComponentsBuilder.newInstance().scheme("http").host("example.com").pathSegment("foo");
57+
UriComponents result1 = builder.build();
58+
builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz");
59+
UriComponents result2 = builder.build();
60+
61+
assertEquals("http", result1.getScheme());
62+
assertEquals("example.com", result1.getHost());
63+
assertEquals("/foo", result1.getPath());
64+
URI expected = new URI("http://example.com/foo");
65+
assertEquals("Invalid result URI", expected, result1.toUri());
66+
67+
assertEquals("http", result2.getScheme());
68+
assertEquals("example.com", result2.getHost());
69+
assertEquals("/foo/foo2", result2.getPath());
70+
assertEquals("bar", result2.getQuery());
71+
assertEquals("baz", result2.getFragment());
72+
expected = new URI("http://example.com/foo/foo2?bar#baz");
73+
assertEquals("Invalid result URI", expected, result2.toUri());
74+
}
75+
5376
@Test
5477
public void fromPath() throws URISyntaxException {
5578
UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();

0 commit comments

Comments
 (0)