Skip to content

Commit c82f35a

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

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -153,7 +153,7 @@ protected UriComponentsBuilder(UriComponentsBuilder other) {
153153
this.port = other.port;
154154
this.pathBuilder = other.pathBuilder.cloneBuilder();
155155
this.uriVariables.putAll(other.uriVariables);
156-
this.queryParams.putAll(other.queryParams);
156+
this.queryParams.addAll(other.queryParams);
157157
this.fragment = other.fragment;
158158
this.encodeTemplate = other.encodeTemplate;
159159
this.charset = other.charset;

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -737,30 +737,32 @@ public void parsesEmptyUri() {
737737
assertThat(components.toString(), equalTo(""));
738738
}
739739

740-
@Test
740+
@Test // gh-25243
741741
public void testCloneAndMerge() {
742742
UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
743-
builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode();
743+
builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1", "x").fragment("f1").encode();
744744

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

748+
builder1.queryParam("q1", "y"); // one more entry for an existing parameter
749+
748750
UriComponents result1 = builder1.build();
749751
assertEquals("http", result1.getScheme());
750752
assertEquals("e1.com", result1.getHost());
751753
assertEquals("/p1/ps1", result1.getPath());
752-
assertEquals("q1", result1.getQuery());
754+
assertEquals("q1=x&q1=y", result1.getQuery());
753755
assertEquals("f1", result1.getFragment());
754756

755757
UriComponents result2 = builder2.buildAndExpand("ps2;a");
756758
assertEquals("https", result2.getScheme());
757759
assertEquals("e2.com", result2.getHost());
758760
assertEquals("/p1/ps1/p2/ps2%3Ba", result2.getPath());
759-
assertEquals("q1&q2", result2.getQuery());
761+
assertEquals("q1=x&q2", result2.getQuery());
760762
assertEquals("f2", result2.getFragment());
761763
}
762764

763-
@Test // gh-24772
765+
@Test // gh-24772
764766
public void testDeepClone() {
765767
HashMap<String, Object> vars = new HashMap<>();
766768
vars.put("ps1", "foo");
@@ -770,7 +772,7 @@ public void testDeepClone() {
770772
builder1.scheme("http").host("e1.com").userInfo("user:pwd").path("/p1").pathSegment("{ps1}")
771773
.pathSegment("{ps2}").queryParam("q1").fragment("f1").uriVariables(vars).encode();
772774

773-
UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();
775+
UriComponentsBuilder builder2 = builder1.cloneBuilder();
774776

775777
UriComponents result1 = builder1.build();
776778
assertEquals("http", result1.getScheme());

0 commit comments

Comments
 (0)