microsoftgraph / msgraph-sdk-php

Microsoft Graph Library for PHP.
Other
567 stars 143 forks source link

Error in using sdk in Symfony with caching #1471

Open SweetSallyBe opened 6 months ago

SweetSallyBe commented 6 months ago

When I get a model from the graph, I get the correct model. But I'm using caching in Symfony to save requests.

return $this->cache->get($cacheKey, function (ItemInterface $item) use ($type, $email) {
            $item->expiresAfter(self::CACHE_EXPIRES_IN_SECONDS_SHORT);
        try {
            $requestConfig
                = new GroupsRequestBuilderGetRequestConfiguration(queryParameters: GroupsRequestBuilderGetRequestConfiguration::createQueryParameters(filter: 'startsWith(mail,\''
                . $email . '\')'));

            $groups = $this->graphClient->groups()->get($requestConfig)->wait()->getValue();
            if ($groups) {
                foreach ($groups as $group) {
                    if ($group instanceof \Microsoft\Graph\Generated\Models\Group && $group->getMail() == $email) {
                        return $group;
                    }
                }
            }

            return null;
        } catch (ApiException $exception) {
            $this->handleException($exception);
        }

        });

In my local machine, this gives no trouble, when testing this. On my remote machine, I get an error

In DefaultMarshaller.php line 50:
Serialization of 'Closure' is not allowed 

It seems the serialization does not work well on this model?

Any suggestions? Tim

ianef commented 2 months ago

I'm experiencing a similar issue. In my case I'm serializing messages to offload the delivery via a command line background task. The message can't be serialised. This is because the backing store adds subscriptions when objects are set on the message, such as ItemBody and FileAttachments. The subscriptions are closures and can't be serialised.

Here's test version of how I used to serialize my messages:

    $message = new Message();
    $message->setSubject('Test');
    $body = new ItemBody();
    $body->setContentType(new BodyType(BodyType::HTML));
    $body->setContent('<p>Test</p>');
    $message->setBody($body); // Causes a subscription to be set on ItemBody
    $test = serialize($message); // This would be stored for the background task.

This bombs at the last line because the wrapped SDK Message cannot be serialised as the ItemBody backing store has a subscription attached for the message which is a closure and can't be serialised.

Ndiritu commented 2 months ago

@SweetSallyBe sorry about this experience. Please try the workarounds suggested in https://github.com/microsoftgraph/msgraph-sdk-php/issues/1556