Skip to content

Commit 65e6010

Browse files
committed
Copy queryParams MultiValueMap through addAll (for independent List entries)
Closes gh-25423
1 parent f1345aa commit 65e6010

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected UriComponentsBuilder(UriComponentsBuilder other) {
157157
this.port = other.port;
158158
this.pathBuilder = other.pathBuilder.cloneBuilder();
159159
this.uriVariables.putAll(other.uriVariables);
160-
this.queryParams.putAll(other.queryParams);
160+
this.queryParams.addAll(other.queryParams);
161161
this.fragment = other.fragment;
162162
this.encodeTemplate = other.encodeTemplate;
163163
this.charset = other.charset;

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -918,30 +918,32 @@ void parsesEmptyUri() {
918918
assertThat(components.toString()).isEqualTo("");
919919
}
920920

921-
@Test
921+
@Test // gh-25243
922922
void testCloneAndMerge() {
923923
UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
924-
builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode();
924+
builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1", "x").fragment("f1").encode();
925925

926-
UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();
926+
UriComponentsBuilder builder2 = builder1.cloneBuilder();
927927
builder2.scheme("https").host("e2.com").path("p2").pathSegment("{ps2}").queryParam("q2").fragment("f2");
928928

929+
builder1.queryParam("q1", "y"); // one more entry for an existing parameter
930+
929931
UriComponents result1 = builder1.build();
930932
assertThat(result1.getScheme()).isEqualTo("http");
931933
assertThat(result1.getHost()).isEqualTo("e1.com");
932934
assertThat(result1.getPath()).isEqualTo("/p1/ps1");
933-
assertThat(result1.getQuery()).isEqualTo("q1");
935+
assertThat(result1.getQuery()).isEqualTo("q1=x&q1=y");
934936
assertThat(result1.getFragment()).isEqualTo("f1");
935937

936938
UriComponents result2 = builder2.buildAndExpand("ps2;a");
937939
assertThat(result2.getScheme()).isEqualTo("https");
938940
assertThat(result2.getHost()).isEqualTo("e2.com");
939941
assertThat(result2.getPath()).isEqualTo("/p1/ps1/p2/ps2%3Ba");
940-
assertThat(result2.getQuery()).isEqualTo("q1&q2");
942+
assertThat(result2.getQuery()).isEqualTo("q1=x&q2");
941943
assertThat(result2.getFragment()).isEqualTo("f2");
942944
}
943945

944-
@Test // gh-24772
946+
@Test // gh-24772
945947
void testDeepClone() {
946948
HashMap<String, Object> vars = new HashMap<>();
947949
vars.put("ps1", "foo");
@@ -951,7 +953,7 @@ void testDeepClone() {
951953
builder1.scheme("http").host("e1.com").userInfo("user:pwd").path("/p1").pathSegment("{ps1}")
952954
.pathSegment("{ps2}").queryParam("q1").fragment("f1").uriVariables(vars).encode();
953955

954-
UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();
956+
UriComponentsBuilder builder2 = builder1.cloneBuilder();
955957

956958
UriComponents result1 = builder1.build();
957959
assertThat(result1.getScheme()).isEqualTo("http");

0 commit comments

Comments
 (0)