Skip to content

Commit e0db67a

Browse files
xyracliusrstoyanchev
authored andcommitted
Fix double encoding in DefaultApiVersionInserter
Ensure that the DefaultApiVersionInserter does not re-encode existing parts of the input URI by using the 'encoded' flag in UriComponentsBuilder. This prevents percent-encoded characters (like %20) from being incorrectly double-encoded to %2520 during the version insertion process. See gh-36097 Signed-off-by: Nabil Fawwaz Elqayyim <master@nabilfawwaz.com>
1 parent 1370a83 commit e0db67a

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Default implementation of {@link ApiVersionInserter}.
3434
*
3535
* @author Rossen Stoyanchev
36+
* @author Nabil Fawwaz Elqayyim
3637
* @since 7.0
3738
* @see DefaultApiVersionInserterBuilder
3839
*/
@@ -81,7 +82,7 @@ public URI insertVersion(Object version, URI uri) {
8182
builder.replacePath(null);
8283
pathSegments.forEach(builder::pathSegment);
8384
}
84-
return builder.build().toUri();
85+
return builder.build(true).toUri();
8586
}
8687

8788
private void assertPathSegmentIndex(Integer index, int pathSegmentsSize, URI uri) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client;
18+
19+
import java.net.URI;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Unit tests for {@link DefaultApiVersionInserter}.
27+
*
28+
* @author Nabil Fawwaz Elqayyim
29+
*/
30+
class DefaultApiVersionInsertersTests {
31+
32+
@Test
33+
void insertVersionPreservesExistingEncoding() {
34+
URI uri = URI.create("http://localhost/test?foo=%20");
35+
DefaultApiVersionInserter inserter = (DefaultApiVersionInserter) ApiVersionInserter.usePathSegment(0);
36+
37+
URI result = inserter.insertVersion("1", uri);
38+
39+
assertThat(result.toString()).isEqualTo("http://localhost/1/test?foo=%20");
40+
}
41+
42+
@Test
43+
void insertVersionAsQueryParamPreservesEncoding() {
44+
URI uri = URI.create("http://localhost/test?foo=%20");
45+
DefaultApiVersionInserter inserter = (DefaultApiVersionInserter) ApiVersionInserter.useQueryParam("v");
46+
47+
URI result = inserter.insertVersion("1", uri);
48+
49+
assertThat(result.toString()).isEqualTo("http://localhost/test?foo=%20&v=1");
50+
}
51+
52+
}

0 commit comments

Comments
 (0)