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

Unable overide Any default Interceptor Behaviour #1757

Closed
raghucha opened this issue Sep 30, 2024 · 2 comments · Fixed by #1795
Closed

Unable overide Any default Interceptor Behaviour #1757

raghucha opened this issue Sep 30, 2024 · 2 comments · Fixed by #1795
Assignees
Labels
bug Something isn't working

Comments

@raghucha
Copy link

raghucha commented Sep 30, 2024

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
Copy link
Author

I have created a fix in the PR #1758

@raghucha
Copy link
Author

@Ndiritu could you please review this at the earliest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment