Batch requests erroneously omit empty arrays

Open
#212 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
api

Research direction

Start with Microsoft\Graph\Core\Requests\BatchRequestBuilder::postAsync() and toPostRequestInformation(), then follow setContentFromParsable() to the JSON serialization writer. Reproduce the batch output shown in the issue and inspect JsonSerializationWriter.php around lines 355-363. Done means an empty otherMails array remains in the serialized batch request.

Written by the indexing model from the issue text.

Description

status:waiting-for-triage type:bug
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
Dominant language
PHP
Stars
16
Forks
8
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoftgraph/msgraph-sdk-php-core

All issues in microsoftgraph/msgraph-sdk-php-core

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.