Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/url replacement #941

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -103,23 +101,15 @@ public void setUrlReplaceHandlerOption(
* @return the request with the updated url.
*/
@Nonnull public static Request replaceRequestUrl(
@Nonnull Request request, @Nonnull Map<String, String> 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<String, String> 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<String, String> 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<String, String> entry : replacementPairs.entrySet()) {
replacedUrl = replacedUrl.replace(entry.getKey(), entry.getValue());
}
builder.url(replacedUrl);
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.io.IOException;
import java.util.stream.Stream;

public class ParametersNameDecodingHandlerTest {
class ParametersNameDecodingHandlerTest {

private static Stream<Arguments> originalAndExpectedUrls() {
return Stream.of(
Expand All @@ -34,7 +34,7 @@ private static Stream<Arguments> originalAndExpectedUrls() {

@ParameterizedTest
@MethodSource("originalAndExpectedUrls")
public void defaultParameterNameDecodingHandlerOnlyDecodesNamesNotValues(
void defaultParameterNameDecodingHandlerOnlyDecodesNamesNotValues(
String original, String expectedResult) throws IOException {
Interceptor[] interceptors =
new Interceptor[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down