msgraph-sdk-java-core icon indicating copy to clipboard operation
msgraph-sdk-java-core copied to clipboard

Unable overide Any default Interceptor Behaviour

Open raghucha opened this issue 1 year ago • 2 comments

Describe the bug

Currently in ms-graph-java sdk , we are unable to override any Default interceptors . I have updated the test in GraphTelemetryHandlerTest to the below


    @Test
    void arrayInterceptorsTest() throws IOException {
        final String expectedCore = CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + CoreConstants.Headers.VERSION;

        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());

            }
        }

        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));
    }

    private static @NotNull RetryHandler getDisabledRetryHandler() {
        RetryHandlerOption retryHandlerOption = new RetryHandlerOption(
            (delay, executionCount, request, response) -> false, 0, 0);
        RetryHandler retryHandler = new RetryHandler(retryHandlerOption);
        return retryHandler;
    }

These tests fail .As the Default Interceptors are taking precedence

Expected behavior

When ever you create interceptors ,the user sent interceptors should take precedence over the Default implementation

How to reproduce

 public static void main(String[] args) throws Exception {


        // The client credentials flow requires that you request the
        // /.default scope, and pre-configure your permissions on the
        // app registration in Azure. An administrator must grant consent
        // to those permissions beforehand.
        final String[] scopes = new String[]{"https://graph.microsoft.com/.default"};

        final ClientSecretCredential tokenCredential = new ClientSecretCredentialBuilder()
            .clientId(clientId)
            .tenantId(tenantId)
            .clientSecret(clientSecret)
            .build();

        if (null == scopes || null == tokenCredential) {
            throw new Exception("Unexpected error");
        }

        AzureIdentityAuthenticationProvider authenticationProvider = new AzureIdentityAuthenticationProvider(
            tokenCredential, new String[0], scopes);
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.redactHeader("authorization");
        logging.setLevel(Level.BODY);

        OkHttpClient httpClient = GraphClientFactory.create(logging, getDisabledRetryHandler()).build();

        for (Interceptor interceptor : httpClient.interceptors()) {
            if(interceptor instanceof  RetryHandler){
                RetryHandler retryInterceptor = (RetryHandler)interceptor;

        //The custom values are overridden with default values 
               int retry =  retryInterceptor.getRetryOptions().maxRetries();
               Long maxDelay = retryInterceptor.getRetryOptions().delay();

            }

        }

        final GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider, httpClient);


    }

    private static @NotNull RetryHandler getDisabledRetryHandler() {
        RetryHandlerOption retryHandlerOption = new RetryHandlerOption(
            (delay, executionCount, request, response) -> false, 0, 0);
        RetryHandler retryHandler = new RetryHandler(retryHandlerOption);
        return retryHandler;
    }

SDK Version

6.16.0

Latest version known to work for scenario above?

No response

Known Workarounds

there are no known work arounds

Debug output

Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

raghucha avatar Sep 30 '24 21:09 raghucha

I have created a fix in the PR https://github.com/microsoftgraph/msgraph-sdk-java-core/pull/1758

raghucha avatar Sep 30 '24 23:09 raghucha

@Ndiritu could you please review this at the earliest

raghucha avatar Sep 30 '24 23:09 raghucha