Skip to content

Batch requests erroneously omit empty arrays #212

@kkatpcc

Description

@kkatpcc

Describe the bug

When using this SDK (composer require microsoft/microsoft-graph-core) to perform JSON batching, the serialization process omits empty arrays. This behavior can cause requests to indicate success (204 response code) even though the desired operation did not occur.

Expected behavior

As a prime example, clearing a user's otherMails attribute requires supplying an empty array (a null value will cause an error here) and that empty array should be transmitted as part of the request.

Making such a direct (non-batch) request retains the empty array (otherMails: []) and the call works as expected. A batch version of such a request should do the same, instead of omitting the empty array.

How to reproduce

Here is a basic code example (using composer require microsoft/microsoft-graph) that illustrates the difference:

<?php

require_once('vendor/autoload.php');

$graphServiceClient = new Microsoft\Graph\GraphServiceClient(new Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext('TenantId','clientId','clientSecret'));

$batch = [];
$requestBody = new Microsoft\Graph\Generated\Models\User();
$requestBody->setOtherMails([]);
$batch[] = $graphServiceClient->me()->toPatchRequestInformation($requestBody);

$batchRequestContent = new Microsoft\Graph\Core\Requests\BatchRequestContent($batch);
$batchRequestBuilder = new Microsoft\Graph\BatchRequestBuilder($graphServiceClient->getRequestAdapter());

error_log('after batch serialization');
print_r(array_map(fn($r) => $r->body, json_decode($batchRequestBuilder->toPostRequestInformation($batchRequestContent)->content->getContents())->requests));

error_log('original batch content');
print_r(array_map(fn($b) => json_decode($b->content->getContents()), $batch));

Example output:

after batch serialization
Array
(
    [0] => stdClass Object
        (
            [@odata.type] => #microsoft.graph.user
        )

)
original batch content
Array
(
    [0] => stdClass Object
        (
            [@odata.type] => #microsoft.graph.user
            [otherMails] => Array
                (
                )

        )

)

SDK Version

2.3

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

No response

Configuration

No response

Other information

Trace of the problem:

  1. Microsoft\Graph\Core\Requests\BatchRequestBuilder->postAsync() calls $this->toPostRequestInformation()
  2. which calls $requestInfo->setContentFromParsable() using the supplied requestAdapter
  3. which calls $requestAdapter->getSerializationWriterFactory()->getSerializationWriter()
  4. which is an instance of Microsoft\Kiota\Serialization\Json\JsonSerializationWriter
  5. which omits empty arrays

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:waiting-for-triageAn issue that is yet to be reviewed or assignedtype:bugA broken experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions