Browse Source

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>
pull/36125/head
Nabil Fawwaz Elqayyim 4 weeks ago committed by rstoyanchev
parent
commit
e0db67aa3b
  1. 3
      spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserter.java
  2. 52
      spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java

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

@ -33,6 +33,7 @@ import org.springframework.web.util.UriComponentsBuilder; @@ -33,6 +33,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* Default implementation of {@link ApiVersionInserter}.
*
* @author Rossen Stoyanchev
* @author Nabil Fawwaz Elqayyim
* @since 7.0
* @see DefaultApiVersionInserterBuilder
*/
@ -81,7 +82,7 @@ final class DefaultApiVersionInserter implements ApiVersionInserter { @@ -81,7 +82,7 @@ final class DefaultApiVersionInserter implements ApiVersionInserter {
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) {

52
spring-web/src/test/java/org/springframework/web/client/DefaultApiVersionInsertersTests.java

@ -0,0 +1,52 @@ @@ -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");
}
}
Loading…
Cancel
Save