Open ianef opened 1 month ago
Hi @ianef, thanks for reaching out. Apologies for this experience. This will be taken into consideration.
A temporary work-around would be:
$serializationWriter = new JsonSerializationWriter();
$serializationWriter->writeObjectValue("", $message);
$jsonString = $serializationWriter->getSerializedContent()->getContents();
Many thanks @Ndiritu I'll have a go with your code. I didn't realise you could instantiate the writers like that. I hacked around a bit and and came up with this serializer helper, but ideally I'd like to use native PHP serialisation not JSON.
class EntityJsonSerializer
{
public static function serialize(Parsable $model): string
{
$writer = SerializationWriterFactoryRegistry::getDefaultInstance()->getSerializationWriter('application/json');
$writer->writeObjectValue(null, $model);
return (string)$writer->getSerializedContent();
}
/**
* @param class-string<\Microsoft\Kiota\Abstractions\Serialization\Parsable> $entityClass
*/
public static function deserialize(string $jsonModel, string $entityClass): Parsable
{
$node = ParseNodeFactoryRegistry::getDefaultInstance()->getRootParseNode('application/json', Utils::streamFor($jsonModel));
$result = $node->getObjectValue([$entityClass, 'createFromDiscriminatorValue']);
if ($result === null) {
throw SerializerException::couldNotDeserialize($entityClass);
}
return $result;
}
}
I did wonder if we created an ArraySerializationWriter and added that to the BaseGraphClient constructor, then call this in the Entity class serailize and unserialize methods to return or set the backing store array of actual property values.
Describe the bug
Native serialisation and deserialisation of models no longer works in v2.
In the msgraph-sdk-php-core readme.md file it states:
Sadly this is no longer the case. As soon as any nested object is added to a model, serialisation fails as the embedded closures cannot be serialised.
Admittedly this text is obviously written for V1 as it references Graph Beta, but I don't think it has been intentional to remove the ability to natively serialise models. If that is the case, I think that is a bad move. I don't think there in anything in the upgrade.md file that suggests you can no longer natively serialize and, importantly, deserialize models. No examples exist describing how to do this using the SDK.
Take for example the Message model, in it's basic form you can serialise it, however as soon as you add an ItemBody element you can't because the backing store now contains a subscriber for changes to the ItemBody, which is implemented as a closure. This prevents the Message model from being serialised using native PHP serialize/unserialize methods.
My usage case is that I need to offload the sending of messages to a background task. The messages are created when forms are submitted etc. and saved to a scheduler list which picks them up via a command line background task, and then performs the actual sending of the Message object.
There are already issues that hint towards this problem but not to the actual cause of the error: https://github.com/microsoftgraph/msgraph-sdk-php/issues/1471 https://github.com/microsoftgraph/msgraph-sdk-php/issues/1470 https://github.com/microsoftgraph/msgraph-sdk-php/issues/1452
Expected behavior
Native serialize and unserialize methods should work with all SDK models.
How to reproduce
This simple code will crash when attempting to serialise the Message object:
SDK Version
v2.12.0
Latest version known to work for scenario above?
v1
Known Workarounds
None.
Debug output
Click to expand log
object(Exception)#705 (7) { ["message":protected]=> string(41) "Serialization of 'Closure' is not allowed" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(64) "redacted.php" ["line":protected]=> int(220) ["trace":"Exception":private]=> array(5) { ... } ```Configuration
No response
Other information
No response