Skip to content

Simple Batching Example throwing ArrayIndexOutOfBoundsException #1943

@dan-drewes

Description

@dan-drewes

I cannot get the simple batching example from https://learn.microsoft.com/en-us/graph/sdks/batch-requests?tabs=java to work.
An ArrayIndexOutOfBoundsException is thrown when calling

// Send the batch request content to the /$batch endpoint
        final BatchResponseContent batchResponseContent = Objects.requireNonNull(
            graphClient.getBatchRequestBuilder().post(batchRequestContent, null));

I am updating from 5.64 to 6.6 and it did not work in any of the 6.x versions for me.
Am I missing some required configuration or is this a bug?

Expected behavior

Graph API executes the batch request and returns some kind of response.

Actual behavior

java.lang.ArrayIndexOutOfBoundsException: 0
at com.microsoft.kiota.http.OkHttpRequestAdapter$1.contentLength(OkHttpRequestAdapter.java:923)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:48)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.HeadersInspectionHandler.intercept(HeadersInspectionHandler.java:70)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.UserAgentHandler.intercept(UserAgentHandler.java:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler.intercept(ParametersNameDecodingHandler.java:73)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.RetryHandler.intercept(RetryHandler.java:226)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.RedirectHandler.intercept(RedirectHandler.java:158)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler.intercept(GraphTelemetryHandler.java:64)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.microsoft.kiota.http.middleware.UrlReplaceHandler.intercept(UrlReplaceHandler.java:72)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
at com.microsoft.kiota.http.OkHttpRequestAdapter.getHttpResponseMessage(OkHttpRequestAdapter.java:721)
at com.microsoft.kiota.http.OkHttpRequestAdapter.sendPrimitive(OkHttpRequestAdapter.java:342)
at com.microsoft.graph.core.requests.BatchRequestBuilder.post(BatchRequestBuilder.java:52)
at com.microsoft.graph.serviceclient.CustomBatchRequestBuilder.post(CustomBatchRequestBuilder.java:41)
at ....
when calling

final BatchResponseContent batchResponseContent = Objects.requireNonNull(
            graphClient.getBatchRequestBuilder().post(batchRequestContent, null));

Steps to reproduce the behavior

Maven pom.xml

<dependency>
	      <groupId>com.microsoft.graph</groupId>
	      <artifactId>microsoft-graph</artifactId>
	      <version>6.6.0</version>
</dependency>
<dependency>
	      <groupId>com.azure</groupId>
	      <artifactId>azure-identity</artifactId>
	      <version>1.12.0</version>
</dependency>

Code:

ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(connectionProperties.getMstClientId()).clientSecret(connectionProperties.getMstClientSecret())
                .tenantId(connectionProperties.getMstTenantId()).build();

        // Build the auth provider using the client secret credential
        String[] scopes = new String[] { "https://graph.microsoft.com/.default" };

        GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);


// Create the batch request content with the steps
final BatchRequestContent batchRequestContent = new BatchRequestContent(
    graphClient);

// Use the Graph client to generate the requestInformation object for GET /me
final RequestInformation meRequestInformation = graphClient.me()
    .toGetRequestInformation();

final ZoneOffset localTimeZone = OffsetDateTime.now().getOffset();
final OffsetDateTime today = OffsetDateTime.of(LocalDate.now(),
    LocalTime.MIDNIGHT, localTimeZone);
final OffsetDateTime tomorrow = today.plusDays(1);

// Use the Graph client to generate the requestInformation for
// GET /me/calendarView?startDateTime="start"&endDateTime="end"
RequestInformation calenderViewRequestInformation = graphClient.me()
    .calendarView().toGetRequestInformation(requestConfiguration -> {
        requestConfiguration.queryParameters.startDateTime = today.toString();
        requestConfiguration.queryParameters.endDateTime = tomorrow.toString();
    });

// Add the requestInformation objects to the batch request content
final String meRequestId = batchRequestContent
    .addBatchRequestStep(meRequestInformation);
final String calendarViewRequestStepId = batchRequestContent
    .addBatchRequestStep(calenderViewRequestInformation);

// Send the batch request content to the /$batch endpoint
final BatchResponseContent batchResponseContent = Objects.requireNonNull(
    graphClient.getBatchRequestBuilder().post(batchRequestContent, null));

// Get the user response using the id assigned to the request
final User me = batchResponseContent.getResponseById(meRequestId,
    User::createFromDiscriminatorValue);
System.out.println(String.format("Hello %s!", me.getDisplayName()));

// Get the calendar view response by id
final EventCollectionResponse eventsResponse = Objects.requireNonNull(
    batchResponseContent.getResponseById(calendarViewRequestStepId,
        EventCollectionResponse::createFromDiscriminatorValue));
System.out.println(String.format("You have %d events on your calendar today",
    Objects.requireNonNull(eventsResponse.getValue()).size()));

Metadata

Metadata

Assignees

Labels

type:bugA broken experience

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions