diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 3b37c68d3800..299095ae4e1e 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -153,7 +153,7 @@ protected UriComponentsBuilder(UriComponentsBuilder other) { this.port = other.port; this.pathBuilder = other.pathBuilder.cloneBuilder(); this.uriVariables.putAll(other.uriVariables); - this.queryParams.putAll(other.queryParams); + this.queryParams.addAll(other.queryParams); this.fragment = other.fragment; this.encodeTemplate = other.encodeTemplate; this.charset = other.charset; diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index a7b7d67e557f..adbe0654d874 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -737,30 +737,32 @@ public void parsesEmptyUri() { assertThat(components.toString(), equalTo("")); } - @Test + @Test // gh-25243 public void testCloneAndMerge() { UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance(); - builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode(); + builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1", "x").fragment("f1").encode(); - UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone(); + UriComponentsBuilder builder2 = builder1.cloneBuilder(); builder2.scheme("https").host("e2.com").path("p2").pathSegment("{ps2}").queryParam("q2").fragment("f2"); + builder1.queryParam("q1", "y"); // one more entry for an existing parameter + UriComponents result1 = builder1.build(); assertEquals("http", result1.getScheme()); assertEquals("e1.com", result1.getHost()); assertEquals("/p1/ps1", result1.getPath()); - assertEquals("q1", result1.getQuery()); + assertEquals("q1=x&q1=y", result1.getQuery()); assertEquals("f1", result1.getFragment()); UriComponents result2 = builder2.buildAndExpand("ps2;a"); assertEquals("https", result2.getScheme()); assertEquals("e2.com", result2.getHost()); assertEquals("/p1/ps1/p2/ps2%3Ba", result2.getPath()); - assertEquals("q1&q2", result2.getQuery()); + assertEquals("q1=x&q2", result2.getQuery()); assertEquals("f2", result2.getFragment()); } - @Test // gh-24772 + @Test // gh-24772 public void testDeepClone() { HashMap vars = new HashMap<>(); vars.put("ps1", "foo"); @@ -770,7 +772,7 @@ public void testDeepClone() { builder1.scheme("http").host("e1.com").userInfo("user:pwd").path("/p1").pathSegment("{ps1}") .pathSegment("{ps2}").queryParam("q1").fragment("f1").uriVariables(vars).encode(); - UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone(); + UriComponentsBuilder builder2 = builder1.cloneBuilder(); UriComponents result1 = builder1.build(); assertEquals("http", result1.getScheme());