From adf470aa76e12920effbc9d8feee5721ecb68101 Mon Sep 17 00:00:00 2001 From: Raghu Sammeta Date: Mon, 30 Sep 2024 19:34:23 -0400 Subject: [PATCH 1/9] fix: issue where custom interceptors would fail to override default interceptors --- .../core/requests/GraphClientFactory.java | 14 +------ .../core/requests/GraphClientFactoryTest.java | 38 +++++++++++++++++++ .../middleware/GraphTelemetryHandlerTest.java | 24 ++++++++---- 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index baa38bd76..f6ca8b236 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -75,19 +75,7 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication */ @Nonnull public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) { - final OkHttpClient.Builder builder = create(graphClientOption); - //Skip adding interceptor if that class of interceptor already exist. - final List appliedInterceptors = new ArrayList<>(); - for(Interceptor interceptor: builder.interceptors()) { - appliedInterceptors.add(interceptor.getClass().toString()); - } - for (Interceptor interceptor:interceptors){ - if(appliedInterceptors.contains(interceptor.getClass().toString())) { - continue; - } - builder.addInterceptor(interceptor); - } - return builder; + return KiotaClientFactory.create(interceptors); } /** diff --git a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java index fcc2972b9..3d8baf288 100644 --- a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java @@ -11,14 +11,22 @@ import java.io.IOException; import java.net.URI; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import com.microsoft.graph.core.CoreConstants; import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; +import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; import com.microsoft.kiota.authentication.AccessTokenProvider; import com.microsoft.kiota.authentication.AllowedHostsValidator; import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; +import com.microsoft.kiota.http.middleware.RedirectHandler; +import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; +import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -42,6 +50,29 @@ void testCreateWithAuthenticationProvider() throws IOException { assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); } + @Test + void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException { + + final Interceptor[] interceptors = {new GraphTelemetryHandler(), getDisabledRetryHandler(), + new RedirectHandler()}; + final OkHttpClient client = GraphClientFactory.create(interceptors).build(); + final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); + final Response response = client.newCall(request).execute(); + + for (Interceptor clientInterceptor : client.interceptors()) { + if (clientInterceptor instanceof RetryHandler) { + RetryHandlerOption retryOptions = ((RetryHandler) clientInterceptor).getRetryOptions(); + Assertions.assertEquals(0, retryOptions.maxRetries()); + Assertions.assertEquals(0, retryOptions.delay()); + + } + + assertTrue(clientInterceptor instanceof GraphTelemetryHandler + || clientInterceptor instanceof RedirectHandler + || clientInterceptor instanceof RetryHandler); + } + } + private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() { final AccessTokenProvider mockAccessTokenProvider = mock(AzureIdentityAccessTokenProvider.class); when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap())) @@ -53,4 +84,11 @@ private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvid .thenReturn(mockAccessTokenProvider); return mockAuthenticationProvider; } + + private static @NotNull RetryHandler getDisabledRetryHandler() { + RetryHandlerOption retryHandlerOption = new RetryHandlerOption( + (delay, executionCount, request, response) -> false, 0, 0); + RetryHandler retryHandler = new RetryHandler(retryHandlerOption); + return retryHandler; + } } diff --git a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java index b03f40615..ef0211476 100644 --- a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java @@ -6,11 +6,15 @@ import com.microsoft.kiota.http.middleware.RedirectHandler; import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -30,22 +34,26 @@ void telemetryHandlerDefaultTests() throws IOException { assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - assertTrue(!response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(CoreConstants.Headers.ANDROID_VERSION_PREFIX)); // Android version is not going to be present on unit tests running on java platform - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + assertTrue(!response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains( + CoreConstants.Headers.ANDROID_VERSION_PREFIX)); // Android version is not going to be present on unit tests running on java platform + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test void arrayInterceptorsTest() throws IOException { final String expectedCore = CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + CoreConstants.Headers.VERSION; - final Interceptor[] interceptors = {new GraphTelemetryHandler(), new RetryHandler(), new RedirectHandler()}; + final Interceptor[] interceptors = {new GraphTelemetryHandler(), new RetryHandler(), + new RedirectHandler()}; final OkHttpClient client = GraphClientFactory.create(interceptors).build(); final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); final Response response = client.newCall(request).execute(); assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test @@ -59,7 +67,8 @@ void arrayInterceptorEmptyTest() throws IOException { assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test @@ -76,7 +85,7 @@ void testClientOptions() throws IOException { graphClientOption.setGraphServiceTargetVersion(serviceLibVer); final String expectedCoreVer = - CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" +coreLibVer; + CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + coreLibVer; final String expectedClientEndpoint = CoreConstants.Headers.JAVA_VERSION_PREFIX + "-" + serviceLibVer + "/" + clientLibVer; @@ -85,7 +94,8 @@ void testClientOptions() throws IOException { final Response response = client.newCall(request).execute(); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCoreVer)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint)); assertTrue(response.request().header(CoreConstants.Headers.CLIENT_REQUEST_ID).contains(requestId)); } } From 667cae662ec46e938d73817e79d58e84d97171e6 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 11:28:56 +0200 Subject: [PATCH 2/9] feat: Support overriding default interceptors via request options --- .../core/requests/GraphClientFactory.java | 49 ++++++++++++++++--- .../core/requests/GraphClientFactoryTest.java | 32 ++++++++++++ 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index f6ca8b236..aed9c92ce 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -3,6 +3,7 @@ import com.microsoft.graph.core.CoreConstants; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; import com.microsoft.graph.core.requests.options.GraphClientOption; +import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; import com.microsoft.kiota.http.KiotaClientFactory; import com.microsoft.kiota.http.middleware.AuthorizationHandler; @@ -58,8 +59,13 @@ public static OkHttpClient.Builder create(@Nonnull List interceptor */ @Nonnull public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) { + return create(authenticationProvider, new RequestOption[0]); + } + + @Nonnull + public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull RequestOption[] requestOptions) { final GraphClientOption graphClientOption = new GraphClientOption(); - final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption); + final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions); final ArrayList interceptorList = new ArrayList<>(Arrays.asList(interceptors)); interceptorList.add(new AuthorizationHandler(authenticationProvider)); graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG); @@ -75,7 +81,13 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication */ @Nonnull public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) { - return KiotaClientFactory.create(interceptors); + var builder = KiotaClientFactory.create(interceptors); + var customInterceptors = builder.interceptors(); + var telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler); + if (!telemetryHandlerExists) { + customInterceptors.add(new GraphTelemetryHandler(graphClientOption)); + } + return builder; } /** @@ -97,9 +109,15 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient */ @Nonnull public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption) { + return create(graphClientOption, new RequestOption[0]); + } + + @Nonnull + public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption(); - return KiotaClientFactory.create(createDefaultGraphInterceptors(options)); + return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions)); } + /** * Creates the default Interceptors for use with Graph. * @@ -108,14 +126,31 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien */ @Nonnull public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption) { - List handlers = new ArrayList<>(); - addDefaultFeatureUsages(graphClientOption); + return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]); + } + + @Nonnull + public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { + Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); + + UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs()); - handlers.add(new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs()))); + for (RequestOption option : requestOptions) { + if (option instanceof UrlReplaceHandlerOption) { + urlReplaceHandlerOption = (UrlReplaceHandlerOption) option; + } + } + + List handlers = new ArrayList<>(); + handlers.add(urlReplaceHandlerOption == null ? + new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())) : + new UrlReplaceHandler(urlReplaceHandlerOption)); handlers.add(new GraphTelemetryHandler(graphClientOption)); - handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors())); + handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions))); + addDefaultFeatureUsages(graphClientOption); return handlers.toArray(new Interceptor[0]); } + //These are the default features used by the Graph Client private static void addDefaultFeatureUsages(GraphClientOption graphClientOption) { graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.RETRY_HANDLER_FLAG); diff --git a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java index 3d8baf288..b6cfa454b 100644 --- a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java @@ -10,15 +10,18 @@ import java.io.IOException; import java.net.URI; +import java.util.ArrayList; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import com.azure.core.http.policy.RetryOptions; import com.microsoft.graph.core.CoreConstants; import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; +import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.authentication.AccessTokenProvider; import com.microsoft.kiota.authentication.AllowedHostsValidator; import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; @@ -50,6 +53,35 @@ void testCreateWithAuthenticationProvider() throws IOException { assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); } + @Test + void testCreateWithAuthenticationProviderAndCustomRequestOptions() throws IOException { + final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = + getMockAuthenticationProvider(); + var requestOptions = new ArrayList(); + requestOptions.add(new RetryHandlerOption(null, 0, 0)); + OkHttpClient graphClient = GraphClientFactory.create(mockAuthenticationProvider, requestOptions.toArray(new RequestOption[0])).addInterceptor(new MockResponseHandler()).build(); + + var interceptors = graphClient.interceptors(); + for (Interceptor interceptor : interceptors) { + if (interceptor instanceof RetryHandler) { + RetryHandlerOption retryOptions = ((RetryHandler) interceptor).getRetryOptions(); + Assertions.assertEquals(0, retryOptions.maxRetries()); + Assertions.assertEquals(0, retryOptions.delay()); + } + } + + Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me") + .addHeader("CustomHeader", "CustomValue").build(); + Response response = graphClient.newCall(request).execute(); + + assertEquals(200, response.code()); + assertNotNull(response.request()); + assertTrue(response.request().headers().names().contains("Authorization")); + assertTrue(response.request().headers().names().contains("CustomHeader")); + assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); + assertEquals("CustomValue", response.request().header("CustomHeader")); + } + @Test void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException { From 5c1cd2fe216f1ff6bf3c3e5833f52451d7338840 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 12:10:45 +0200 Subject: [PATCH 3/9] fix build issues --- spotBugsExcludeFilter.xml | 3 +- .../core/requests/GraphClientFactory.java | 28 +++++++++++++++---- .../core/requests/GraphClientFactoryTest.java | 4 +-- .../middleware/GraphTelemetryHandlerTest.java | 13 +++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/spotBugsExcludeFilter.xml b/spotBugsExcludeFilter.xml index ffb0ea6f2..619fb292d 100644 --- a/spotBugsExcludeFilter.xml +++ b/spotBugsExcludeFilter.xml @@ -64,6 +64,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu + @@ -111,4 +112,4 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu - \ No newline at end of file + diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index aed9c92ce..ec12cf102 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -62,6 +62,12 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication return create(authenticationProvider, new RequestOption[0]); } + /** + * OkHttpClient Builder for Graph with specified AuthenticationProvider and RequestOptions to override default graph interceptors + * @param authenticationProvider the AuthenticationProvider to use for requests. + * @param requestOptions custom request options to override default graph interceptors + * @return an OkHttpClient Builder instance. + */ @Nonnull public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull RequestOption[] requestOptions) { final GraphClientOption graphClientOption = new GraphClientOption(); @@ -81,9 +87,9 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication */ @Nonnull public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) { - var builder = KiotaClientFactory.create(interceptors); - var customInterceptors = builder.interceptors(); - var telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler); + final OkHttpClient.Builder builder = KiotaClientFactory.create(interceptors); + final List customInterceptors = builder.interceptors(); + final boolean telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler); if (!telemetryHandlerExists) { customInterceptors.add(new GraphTelemetryHandler(graphClientOption)); } @@ -112,6 +118,12 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien return create(graphClientOption, new RequestOption[0]); } + /** + * The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors + * @param graphClientOption the GraphClientOption for use in requests. + * @param requestOptions custom request options to override default graph interceptors + * @return an OkHttpClient Builder instance. + */ @Nonnull public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption(); @@ -129,6 +141,12 @@ public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientO return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]); } + /** + * Creates the default Interceptors for use with Graph configured with the provided RequestOptions. + * @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with. + * @param requestOptions custom request options to override default graph interceptors + * @return an array of interceptors. + */ @Nonnull public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); @@ -142,9 +160,7 @@ public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientO } List handlers = new ArrayList<>(); - handlers.add(urlReplaceHandlerOption == null ? - new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())) : - new UrlReplaceHandler(urlReplaceHandlerOption)); + handlers.add(new UrlReplaceHandler(urlReplaceHandlerOption)); handlers.add(new GraphTelemetryHandler(graphClientOption)); handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions))); addDefaultFeatureUsages(graphClientOption); diff --git a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java index b6cfa454b..41372c1d7 100644 --- a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java @@ -16,8 +16,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import com.azure.core.http.policy.RetryOptions; -import com.microsoft.graph.core.CoreConstants; import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; @@ -89,7 +87,7 @@ void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException { new RedirectHandler()}; final OkHttpClient client = GraphClientFactory.create(interceptors).build(); final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); - final Response response = client.newCall(request).execute(); + client.newCall(request).execute(); for (Interceptor clientInterceptor : client.interceptors()) { if (clientInterceptor instanceof RetryHandler) { diff --git a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java index ef0211476..06bf4f9e5 100644 --- a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java @@ -6,13 +6,11 @@ import com.microsoft.kiota.http.middleware.RedirectHandler; import com.microsoft.kiota.http.middleware.RetryHandler; -import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -33,6 +31,8 @@ void telemetryHandlerDefaultTests() throws IOException { final Response response = client.newCall(request).execute(); assertNotNull(response); + assertNotNull(response.request()); + assertNotNull(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME)); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); assertTrue(!response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains( CoreConstants.Headers.ANDROID_VERSION_PREFIX)); // Android version is not going to be present on unit tests running on java platform @@ -51,6 +51,8 @@ void arrayInterceptorsTest() throws IOException { final Response response = client.newCall(request).execute(); assertNotNull(response); + assertNotNull(response.request()); + assertNotNull(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME)); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); assertTrue( response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); @@ -66,6 +68,8 @@ void arrayInterceptorEmptyTest() throws IOException { final Response response = client.newCall(request).execute(); assertNotNull(response); + assertNotNull(response.request()); + assertNotNull(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME)); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); assertTrue( response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); @@ -93,6 +97,9 @@ void testClientOptions() throws IOException { final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); final Response response = client.newCall(request).execute(); + assertNotNull(response); + assertNotNull(response.request()); + assertNotNull(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME)); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCoreVer)); assertTrue( response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint)); From a54fc4ccbf11e0eb8b75e50048caea805cd69503 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 12:20:26 +0200 Subject: [PATCH 4/9] Use final method parameter references --- .../core/requests/GraphClientFactory.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index ec12cf102..bcfe3db12 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -37,7 +37,7 @@ public static OkHttpClient.Builder create() { * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors) { + public static OkHttpClient.Builder create(@Nonnull final Interceptor... interceptors) { return create(new GraphClientOption(), interceptors); } @@ -47,7 +47,7 @@ public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors) * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull List interceptors) { + public static OkHttpClient.Builder create(@Nonnull final List interceptors) { return create(new GraphClientOption(), interceptors.toArray(new Interceptor[0])); } @@ -58,7 +58,7 @@ public static OkHttpClient.Builder create(@Nonnull List interceptor * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) { + public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) { return create(authenticationProvider, new RequestOption[0]); } @@ -69,7 +69,7 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull RequestOption[] requestOptions) { + public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) { final GraphClientOption graphClientOption = new GraphClientOption(); final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions); final ArrayList interceptorList = new ArrayList<>(Arrays.asList(interceptors)); @@ -86,7 +86,7 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) { + public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final Interceptor... interceptors) { final OkHttpClient.Builder builder = KiotaClientFactory.create(interceptors); final List customInterceptors = builder.interceptors(); final boolean telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler); @@ -103,7 +103,7 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull List interceptors) { + public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final List interceptors) { return create(graphClientOption, interceptors.toArray(new Interceptor[0])); } @@ -114,7 +114,7 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption) { + public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) { return create(graphClientOption, new RequestOption[0]); } @@ -125,7 +125,7 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { + public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) { GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption(); return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions)); } @@ -137,7 +137,7 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien * @return an array of interceptors. */ @Nonnull - public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption) { + public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption) { return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]); } @@ -148,7 +148,7 @@ public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientO * @return an array of interceptors. */ @Nonnull - public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption, @Nonnull RequestOption[] requestOptions) { + public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs()); From 5b20cf7e1fe031b7b8aa1a50c9018f263736e937 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 16:48:35 +0200 Subject: [PATCH 5/9] Refactor + add tests --- .../core/requests/GraphClientFactory.java | 32 ++++++--- .../core/requests/GraphClientFactoryTest.java | 71 +++++++++++++++++++ 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index bcfe3db12..c006371c5 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -71,7 +71,9 @@ public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenti @Nonnull public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) { final GraphClientOption graphClientOption = new GraphClientOption(); - final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions); + final List requestOptionsList = new ArrayList<>(Arrays.asList(requestOptions)); + requestOptionsList.add(graphClientOption); + final Interceptor[] interceptors = createDefaultGraphInterceptors(requestOptionsList.toArray(new RequestOption[0])); final ArrayList interceptorList = new ArrayList<>(Arrays.asList(interceptors)); interceptorList.add(new AuthorizationHandler(authenticationProvider)); graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG); @@ -115,19 +117,17 @@ public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graph */ @Nonnull public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) { - return create(graphClientOption, new RequestOption[0]); + return KiotaClientFactory.create(createDefaultGraphInterceptors(graphClientOption)); } /** * The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors - * @param graphClientOption the GraphClientOption for use in requests. * @param requestOptions custom request options to override default graph interceptors * @return an OkHttpClient Builder instance. */ @Nonnull - public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) { - GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption(); - return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions)); + public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) { + return KiotaClientFactory.create(createDefaultGraphInterceptors(requestOptions)); } /** @@ -138,25 +138,35 @@ public static OkHttpClient.Builder create(@Nullable final GraphClientOption grap */ @Nonnull public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption) { - return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]); + return getDefaultGraphInterceptors(new RequestOption[]{ graphClientOption }).toArray(new Interceptor[0]); } /** * Creates the default Interceptors for use with Graph configured with the provided RequestOptions. - * @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with. * @param requestOptions custom request options to override default graph interceptors * @return an array of interceptors. */ @Nonnull - public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) { + public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) { Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null"); + return getDefaultGraphInterceptors(requestOptions).toArray(new Interceptor[0]); + } + /** + * Creates the default Interceptors for use with Graph. + * @param requestOptions custom request options to override default graph interceptors + * @return a list of interceptors. + */ + private static List getDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) { + GraphClientOption graphClientOption = new GraphClientOption(); UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs()); - for (RequestOption option : requestOptions) { if (option instanceof UrlReplaceHandlerOption) { urlReplaceHandlerOption = (UrlReplaceHandlerOption) option; } + if (option instanceof GraphClientOption) { + graphClientOption = (GraphClientOption) option; + } } List handlers = new ArrayList<>(); @@ -164,7 +174,7 @@ public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphC handlers.add(new GraphTelemetryHandler(graphClientOption)); handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions))); addDefaultFeatureUsages(graphClientOption); - return handlers.toArray(new Interceptor[0]); + return handlers; } //These are the default features used by the Graph Client diff --git a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java index 41372c1d7..bee692fee 100644 --- a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java @@ -11,6 +11,9 @@ import java.io.IOException; import java.net.URI; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Assertions; @@ -19,12 +22,17 @@ import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; +import com.microsoft.graph.core.requests.options.GraphClientOption; import com.microsoft.kiota.RequestOption; import com.microsoft.kiota.authentication.AccessTokenProvider; import com.microsoft.kiota.authentication.AllowedHostsValidator; import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; +import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; +import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; import com.microsoft.kiota.http.middleware.RedirectHandler; import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.UrlReplaceHandler; +import com.microsoft.kiota.http.middleware.UserAgentHandler; import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import okhttp3.Interceptor; @@ -36,6 +44,51 @@ class GraphClientFactoryTest { private static final String ACCESS_TOKEN_STRING = "token"; + @Test + void testDefaultCreate() { + final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(); + assertDefaultHandlersPresent(clientBuilder.interceptors()); + } + + @Test + void testCreateWithCustomInterceptorsAddsTelemetry() { + final OkHttpClient.Builder clientBuilder = GraphClientFactory.create( + new RetryHandler(), new RedirectHandler() + ); + + assertEquals(3, clientBuilder.interceptors().size()); + + for (Interceptor interceptor : clientBuilder.interceptors()) { + assertTrue( + interceptor instanceof GraphTelemetryHandler + || interceptor instanceof RetryHandler + || interceptor instanceof RedirectHandler + ); + } + } + + @Test + void testCreateWithGraphClientOption() { + final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(new GraphClientOption()); + assertDefaultHandlersPresent(clientBuilder.interceptors()); + } + + @Test + void testCreateDefaultInterceptorsWithCustomOptions() { + Interceptor[] interceptors = GraphClientFactory.createDefaultGraphInterceptors( + new RequestOption[] {new RetryHandlerOption(null, 0, 0)} + ); + assertDefaultHandlersPresent(Arrays.asList(interceptors)); + + for (Interceptor interceptor : interceptors) { + if (interceptor instanceof RetryHandler) { + RetryHandlerOption retryOptions = ((RetryHandler) interceptor).getRetryOptions(); + Assertions.assertEquals(0, retryOptions.maxRetries()); + Assertions.assertEquals(0, retryOptions.delay()); + } + } + } + @Test void testCreateWithAuthenticationProvider() throws IOException { final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = @@ -103,6 +156,24 @@ void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException { } } + private void assertDefaultHandlersPresent(final List interceptors) { + HashSet> expectedInterceptors = new HashSet<>( + Arrays.asList( + GraphTelemetryHandler.class, + RetryHandler.class, + UrlReplaceHandler.class, + UserAgentHandler.class, + RedirectHandler.class, + ParametersNameDecodingHandler.class, + HeadersInspectionHandler.class + ) + ); + + for (Interceptor interceptor : interceptors) { + assertTrue(expectedInterceptors.contains(interceptor.getClass())); + } + } + private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() { final AccessTokenProvider mockAccessTokenProvider = mock(AzureIdentityAccessTokenProvider.class); when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap())) From b75c471d17436712d7e6cc8e32e606af200670e7 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 17:10:38 +0200 Subject: [PATCH 6/9] feat: add GraphClientFactory method using TokenCredential --- .../core/requests/GraphClientFactory.java | 23 +++++++++++++++++++ .../core/requests/GraphClientFactoryTest.java | 19 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index c006371c5..54e70202d 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -1,6 +1,8 @@ package com.microsoft.graph.core.requests; +import com.azure.core.credential.TokenCredential; import com.microsoft.graph.core.CoreConstants; +import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; import com.microsoft.graph.core.requests.options.GraphClientOption; import com.microsoft.kiota.RequestOption; @@ -51,6 +53,27 @@ public static OkHttpClient.Builder create(@Nonnull final List inter return create(new GraphClientOption(), interceptors.toArray(new Interceptor[0])); } + /** + * OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential. + * @param tokenCredential the TokenCredential to use for authentication. + * @return an OkHttpClient Builder instance. + */ + @Nonnull + public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential) { + return create(tokenCredential, new RequestOption[0]); + } + + /** + * OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential and RequestOptions to override default graph interceptors. + * @param tokenCredential the TokenCredential to use for authentication. + * @param requestOptions custom request options to override default graph interceptors + * @return an OkHttpClient Builder instance. + */ + @Nonnull + public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential, @Nonnull final RequestOption[] requestOptions) { + return create(new BaseBearerTokenAuthenticationProvider(new AzureIdentityAccessTokenProvider(tokenCredential)), requestOptions); + } + /** * OkHttpClient Builder for Graph with specified AuthenticationProvider. * Adds an AuthorizationHandler to the OkHttpClient Builder. diff --git a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java index bee692fee..001173c46 100644 --- a/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java @@ -19,6 +19,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import com.azure.core.credential.AccessToken; +import com.azure.core.credential.TokenCredential; import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; @@ -39,6 +41,7 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import reactor.core.publisher.Mono; class GraphClientFactoryTest { @@ -104,6 +107,22 @@ void testCreateWithAuthenticationProvider() throws IOException { assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); } + @Test + void testCreateWithTokenCredential() throws IOException { + final TokenCredential tokenCredential = mock(TokenCredential.class); + when(tokenCredential.getTokenSync(any())).thenReturn(new AccessToken(ACCESS_TOKEN_STRING, null)); + when(tokenCredential.getToken(any())).thenReturn(Mono.just(new AccessToken(ACCESS_TOKEN_STRING, null))); + + final OkHttpClient graphClient = GraphClientFactory.create(tokenCredential).addInterceptor(new MockResponseHandler()).build(); + Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build(); + Response response = graphClient.newCall(request).execute(); + + assertEquals(200, response.code()); + assertNotNull(response.request()); + assertTrue(response.request().headers().names().contains("Authorization")); + assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); + } + @Test void testCreateWithAuthenticationProviderAndCustomRequestOptions() throws IOException { final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = From 061d5ee533d79ee94a486e889d21275008dcd317 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 17:22:51 +0200 Subject: [PATCH 7/9] Improve SonarCloud reliability --- .../com/microsoft/graph/core/requests/GraphClientFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index 54e70202d..d20c9afae 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -140,7 +140,8 @@ public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graph */ @Nonnull public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) { - return KiotaClientFactory.create(createDefaultGraphInterceptors(graphClientOption)); + GraphClientOption option = graphClientOption == null ? new GraphClientOption() : graphClientOption; + return KiotaClientFactory.create(createDefaultGraphInterceptors(option)); } /** From 8bf69f16accde78d8ddaea56ac9890c25b64bbe8 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 12 Nov 2024 17:28:22 +0200 Subject: [PATCH 8/9] disable android-to-kotlin portability warning --- android/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/android/build.gradle b/android/build.gradle index 426aad766..884e2c042 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -55,6 +55,7 @@ android { disable "GradleDependency" disable "NewerVersionAvailable" disable "DuplicatePlatformClasses" // xpp3 added by azure-identity + disable "LambdaLast" } sourceSets { main { From 0a815b3d65c636a1db8ff958bf70349f7e0a55bf Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 19 Nov 2024 12:34:07 +0300 Subject: [PATCH 9/9] Add android lint baseline file and config --- android/build.gradle | 15 ++++++--------- android/lint-baseline.xml | 11 +++++++++++ android/lint.xml | 6 ++++++ 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 android/lint-baseline.xml create mode 100644 android/lint.xml diff --git a/android/build.gradle b/android/build.gradle index 884e2c042..9821c0c24 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -43,20 +43,17 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } + lint { + baseline = file("lint-baseline.xml") + } + lintOptions { textOutput "stdout" checkAllWarnings true warningsAsErrors true - disable "UnusedResources" // Unused will be removed on release - disable "IconExpectedSize" // Using the material icons provided from Google - disable "GoogleAppIndexingApiWarning" // We might want to index our app later - disable "InvalidPackage" // Butterknife, Okio and Realm - disable "ResourceType" // Annotation binding - disable "GradleDependency" - disable "NewerVersionAvailable" - disable "DuplicatePlatformClasses" // xpp3 added by azure-identity - disable "LambdaLast" + lintConfig file("lint.xml") } + sourceSets { main { java.srcDirs = ['../src/main/java'] diff --git a/android/lint-baseline.xml b/android/lint-baseline.xml new file mode 100644 index 000000000..11149ca54 --- /dev/null +++ b/android/lint-baseline.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/android/lint.xml b/android/lint.xml new file mode 100644 index 000000000..0e7a8f716 --- /dev/null +++ b/android/lint.xml @@ -0,0 +1,6 @@ + + + + + +