From ec587a100422233462b9c80a321ff8ece63a20c7 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 14 Dec 2023 14:09:45 -0500 Subject: [PATCH 1/2] - code linting Signed-off-by: Vincent Biret --- .../kiota/http/ParametersNameDecodingHandlerTest.java | 4 ++-- .../com/microsoft/kiota/serialization/JsonParseNodeTests.java | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/ParametersNameDecodingHandlerTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/ParametersNameDecodingHandlerTest.java index bb28dc55f..f555ff457 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/ParametersNameDecodingHandlerTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/ParametersNameDecodingHandlerTest.java @@ -18,7 +18,7 @@ import java.io.IOException; import java.util.stream.Stream; -public class ParametersNameDecodingHandlerTest { +class ParametersNameDecodingHandlerTest { private static Stream originalAndExpectedUrls() { return Stream.of( @@ -34,7 +34,7 @@ private static Stream originalAndExpectedUrls() { @ParameterizedTest @MethodSource("originalAndExpectedUrls") - public void defaultParameterNameDecodingHandlerOnlyDecodesNamesNotValues( + void defaultParameterNameDecodingHandlerOnlyDecodesNamesNotValues( String original, String expectedResult) throws IOException { Interceptor[] interceptors = new Interceptor[] { diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java index 6c28ba7ab..6f19e7ac2 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java @@ -9,12 +9,10 @@ class JsonParseNodeTests { private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory(); - private static final JsonSerializationWriterFactory _serializationWriterFactory = - new JsonSerializationWriterFactory(); private static final String contentType = "application/json"; @Test - void ItDDoesNotFailForGetChildElementOnMissingKey() throws UnsupportedEncodingException { + void itDDoesNotFailForGetChildElementOnMissingKey() throws UnsupportedEncodingException { final var initialString = "{displayName\": \"Microsoft Teams Meeting\"}"; final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8")); final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse); From b8a48f261cb519f804d6a1b263cff4bdab1149c9 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 14 Dec 2023 14:21:11 -0500 Subject: [PATCH 2/2] - fixes uri encoding issue with token replacement Signed-off-by: Vincent Biret --- CHANGELOG.md | 6 ++++ .../http/middleware/UrlReplaceHandler.java | 28 ++++++------------- .../kiota/http/UrlReplaceHandlerTest.java | 22 ++++++++++++++- gradle.properties | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ef469f4e..0f276d6ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +## [0.11.2] - 2023-12-14 + +### Changed + +- Fixed a bug where the URI replacement middleware would mangle base64 encoded IDs. + ## [0.11.1] - 2023-12-14 ### Changed diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/UrlReplaceHandler.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/UrlReplaceHandler.java index 2444acb07..cf2d2bbaa 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/UrlReplaceHandler.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/UrlReplaceHandler.java @@ -12,8 +12,6 @@ import okhttp3.Response; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; import java.util.Map; import java.util.Objects; @@ -103,23 +101,15 @@ public void setUrlReplaceHandlerOption( * @return the request with the updated url. */ @Nonnull public static Request replaceRequestUrl( - @Nonnull Request request, @Nonnull Map replacementPairs) { - Request.Builder builder = request.newBuilder(); - try { - // Decoding the url since Request.url is encoded by default. - String replacedUrl = - URLDecoder.decode( - request.url().toString(), - "UTF-8"); // Using decode(String,String) method to maintain source - // compatibility with Java 8 - for (Map.Entry entry : replacementPairs.entrySet()) { - replacedUrl = replacedUrl.replace(entry.getKey(), entry.getValue()); - } - builder.url(replacedUrl); - return builder.build(); - - } catch (UnsupportedEncodingException e) { - return request; // This should never happen since "UTF-8" is a supported encoding. + @Nonnull final Request request, @Nonnull final Map replacementPairs) { + final Request.Builder builder = request.newBuilder(); + // Decoding the url since Request.url is encoded by default. + String replacedUrl = request.url().toString(); + // compatibility with Java 8 + for (Map.Entry entry : replacementPairs.entrySet()) { + replacedUrl = replacedUrl.replace(entry.getKey(), entry.getValue()); } + builder.url(replacedUrl); + return builder.build(); } } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/UrlReplaceHandlerTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/UrlReplaceHandlerTest.java index d4abb9844..21b49281c 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/UrlReplaceHandlerTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/UrlReplaceHandlerTest.java @@ -57,7 +57,9 @@ void testUrlReplaceHandler_default_url() throws IOException { @Test void testUrlReplaceHandler_multiple_pairs() throws IOException { defaultReplacementPairs.put("/users/TokenToReplace", "/me"); - defaultReplacementPairs.put("{secondToken}", "expectedValue"); + defaultReplacementPairs.put("%7BsecondToken%7D", "expectedValue"); + // we're not decoding the URL to avoid mangling base64 encoded ids like + // events/messages/etc...so the source has to be encoded String customUrl = "https://graph.microsoft.com/beta/users/TokenToReplace/{secondToken}"; // using // special @@ -76,4 +78,22 @@ void testUrlReplaceHandler_multiple_pairs() throws IOException { assertNotNull(response); assertEquals(expectedNewUrl, response.request().url().toString()); } + + @Test + void testItDoesNotReplaceTokensInThePath() throws IOException { + defaultReplacementPairs.put("/users/TokenToReplace", "/me"); + String customUrl = + "https://graph.microsoft.com/v1.0/users/TokenToReplace/calendar/events/AAMkADM4Njk4YzMzLTc3YTktNGQwZi05ZjRjLTJlNTUzYWMxZjAxNABGAAAAAABzQLabgoJzT6ewNe8DeydoBwBoFwNVrXtLQ5PleemFoZ87AAAAAAENAABoFwNVrXtLQ5PleemFoZ87AAA33T-eAAA%3D"; + Interceptor[] interceptors = + new Interceptor[] { + new UrlReplaceHandler(new UrlReplaceHandlerOption(defaultReplacementPairs)) + }; + final OkHttpClient client = KiotaClientFactory.create(interceptors).build(); + final Request request = new Request.Builder().url(customUrl).build(); + final Response response = client.newCall(request).execute(); + assertNotNull(response); + assertEquals( + "https://graph.microsoft.com/v1.0/me/calendar/events/AAMkADM4Njk4YzMzLTc3YTktNGQwZi05ZjRjLTJlNTUzYWMxZjAxNABGAAAAAABzQLabgoJzT6ewNe8DeydoBwBoFwNVrXtLQ5PleemFoZ87AAAAAAENAABoFwNVrXtLQ5PleemFoZ87AAA33T-eAAA%3D", + response.request().url().toString()); + } } diff --git a/gradle.properties b/gradle.properties index 3f6494f76..cc63848f8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,7 +26,7 @@ org.gradle.caching=true mavenGroupId = com.microsoft.kiota mavenMajorVersion = 0 mavenMinorVersion = 11 -mavenPatchVersion = 1 +mavenPatchVersion = 2 mavenArtifactSuffix = #These values are used to run functional tests