From d5982d2e2c34c564fd8ac7ace1e30780d6ae725d Mon Sep 17 00:00:00 2001 From: Nabil Fawwaz Elqayyim Date: Tue, 6 Jan 2026 01:56:27 +0700 Subject: [PATCH] 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. Closes gh-36080 Signed-off-by: Nabil Fawwaz Elqayyim --- .../web/client/DefaultApiVersionInserter.java | 3 +- .../DefaultApiVersionInsertersTests.java | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java b/spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java index a625063943f0..ce0d3dfb866c 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java @@ -33,6 +33,7 @@ * Default implementation of {@link ApiVersionInserter}. * * @author Rossen Stoyanchev + * @author Nabil Fawwaz Elqayyim * @since 7.0 * @see DefaultApiVersionInserterBuilder */ @@ -81,7 +82,7 @@ public URI insertVersion(Object version, URI uri) { builder.replacePath(null); pathSegments.forEach(builder::pathSegment); } - return builder.build().toUri(); + return builder.build(true).toUri(); } private void assertPathSegmentIndex(Integer index, int pathSegmentsSize, URI uri) { diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java new file mode 100644 index 000000000000..8ccb567fb08a --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.client; + +import java.net.URI; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link DefaultApiVersionInserter}. + * + * @author Nabil Fawwaz Elqayyim + */ +class DefaultApiVersionInsertersTests { + + @Test + void insertVersionPreservesExistingEncoding() { + URI uri = URI.create("http://localhost/test?foo=%20"); + DefaultApiVersionInserter inserter = (DefaultApiVersionInserter) ApiVersionInserter.usePathSegment(0); + + URI result = inserter.insertVersion("1", uri); + + assertThat(result.toString()).isEqualTo("http://localhost/1/test?foo=%20"); + } + + @Test + void insertVersionAsQueryParamPreservesEncoding() { + URI uri = URI.create("http://localhost/test?foo=%20"); + DefaultApiVersionInserter inserter = (DefaultApiVersionInserter) ApiVersionInserter.useQueryParam("v"); + + URI result = inserter.insertVersion("1", uri); + + assertThat(result.toString()).isEqualTo("http://localhost/test?foo=%20&v=1"); + } + +}