microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
700 stars 248 forks source link

Serializing an email message with file attachments does not serialize everything #2638

Closed RogerTimmerman closed 2 months ago

RogerTimmerman commented 2 months ago

Describe the bug

Hi, We recently upgraded our application from Microsoft Graph SDK v4 to v5 and found an annoying issue in v5 in serialization of email messages with file-attachments to Json string format after receipt of an email via the Graph API's. The serialization is performed and stored and later deserialize the Json to a email message again and e.g. forward that email including extra information.

  1. Using JsonSerializer at least the contentbytes (the most important part) of the file is missing from the attachments property in the Json output string. Note that serializing in SDK v4, did deliver the complete email content.
  2. Using KiotaJsonSerializer all email properties are missing in Json output string except the attachments property (however does contain the contentbytes)

The actual questions are:

Replies are appreciated, With regards Roger Timmerman

We are using Microsoft graph SDK 5.56.0 (Microsoft.Graph.Core 3.1.15) and related dependencies.

Read email message code part: string[] properties = new string[] { "BccRecipients","Body", "CcRecipients", "CreatedDateTime", "From", "HasAttachments", "Id", "LastModifiedDateTime", "ReceivedDateTime", "ReplyTo", "Sender", "SentDateTime", "Subject", "ToRecipients" }; Task.Run(async () => { retrievedMessage = await _graphClient.Users[accountName] .Messages[msGraphMessageId] .GetAsync(requestConfiguration => requestConfiguration .QueryParameters.Select = properties); }).Wait(RequestTimeoutMs);

Inspecting the retrievedMessage object reveals all properties (including file content) are there.

Expected behavior

The expected behavior is that alike SDK v4, Json output contains all relevant properties

How to reproduce

        string[] properties = new string[] { "BccRecipients","Body", "CcRecipients", "CreatedDateTime", "From", "HasAttachments", "Id", "LastModifiedDateTime", 
            "ReceivedDateTime", "ReplyTo", "Sender", "SentDateTime", "Subject", "ToRecipients" };
        Task.Run(async () =>
        {
            retrievedMessage = await _graphClient.Users[accountName]
            .Messages[msGraphMessageId]
            .GetAsync(requestConfiguration => requestConfiguration
                      .QueryParameters.Select = properties);
        }).Wait(RequestTimeoutMs);

        string json;
        json = JsonSerializer.Serialize(retrievedMessage);

// or: json = KiotaJsonSerializer.SerializeAsStringAsync(retrievedMessage).Result;

SDK Version

SDK 5.56.0 (Microsoft.Graph.Core 3.1.15)

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ``` Examples of serialization (v5 using JsonSerializer, v5 KiotaJsonSerializer, v4 using JsonSerializer): - Serialization in SDK 5 using JsonSerializer (file content is missing) { "Attachments": [ { "ContentType": "text/plain", "IsInline": false, "LastModifiedDateTime": "2024-08-22T09:35:01+00:00", "Name": "small-attachment.txt", "Size": 340, "AdditionalData": { "@odata.mediaContentType": "text/plain" }, "BackingStore": { "InitializationCompleted": false, "ReturnOnlyChangedValues": false }, "Id": "AQMkAGMzNjQ3OWJlLWRjNWItNDA2Yy04YjgxLWFiZWJmYmFiMmQ4NQBGAAADAXcZWMvdbEaSDZn7g6eWdAcAyJ0XzcbX2UiDcSve6RCRbQAAAgEMAAAAyJ0XzcbX2UiDcSve6RCRbQADejg3jAAAAAESABAA5Ba9GDKzzEiqXUYQ4M_hGA==", "OdataType": "#microsoft.graph.fileAttachment" } ], "BccRecipients": [], "Body": { "AdditionalData": {}, "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "Content": "\r\n

Test with Small attachment

", "ContentType": 1, "OdataType": null }, "BodyPreview": null, "CcRecipients": [], "ConversationId": null, "ConversationIndex": null, "Extensions": null, "Flag": null, "From": { "AdditionalData": {}, "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "EmailAddress": { "AdditionalData": {}, "Address": "myaddress@mycompany.com", "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "Name": "My Name", "OdataType": null }, "OdataType": null }, "HasAttachments": true, "Importance": null, "InferenceClassification": null, "InternetMessageHeaders": null, "InternetMessageId": null, "IsDeliveryReceiptRequested": null, "IsDraft": null, "IsRead": null, "IsReadReceiptRequested": null, "MultiValueExtendedProperties": null, "ParentFolderId": null, "ReceivedDateTime": "2024-08-22T09:35:01+00:00", "ReplyTo": [], "Sender": { "AdditionalData": {}, "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "EmailAddress": { "AdditionalData": {}, "Address": "myaddress@mycompany.com", "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "Name": "My Name", "OdataType": null }, "OdataType": null }, "SentDateTime": "2024-08-22T09:34:47+00:00", "SingleValueExtendedProperties": null, "Subject": "Small attachment", "ToRecipients": [ { "AdditionalData": {}, "BackingStore": { "InitializationCompleted": false, "ReturnOnlyChangedValues": false }, "EmailAddress": { "AdditionalData": {}, "Address": "hisaddress@mycompany.com", "BackingStore": { "InitializationCompleted": false, "ReturnOnlyChangedValues": false }, "Name": "His Name", "OdataType": null }, "OdataType": null } ], "UniqueBody": null, "WebLink": null, "Categories": null, "ChangeKey": null, "CreatedDateTime": "2024-08-22T09:35:01+00:00", "LastModifiedDateTime": "2024-08-22T09:35:17+00:00", "AdditionalData": { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('hisaddress%40mycompany.com')/messages(bccRecipients,body,ccRecipients,createdDateTime,from,hasAttachments,id,lastModifiedDateTime,receivedDateTime,replyTo,sender,sentDateTime,subject,toRecipients)/$entity", "@odata.etag": "W/\"CQAAABYAAADInRfNxtfZSINxK97pEJFtAAN54mtE\"" }, "BackingStore": { "InitializationCompleted": true, "ReturnOnlyChangedValues": false }, "Id": "AQMkAGMzNjQ3OWJlLWRjNWItNDA2Yy04YjgxLWFiZWJmYmFiMmQ4NQBGAAADAXcZWMvdbEaSDZn7g6eWdAcAyJ0XzcbX2UiDcSve6RCRbQAAAgEMAAAAyJ0XzcbX2UiDcSve6RCRbQADejg3jAAAAA==", "OdataType": "#microsoft.graph.message" } - Serialization in SDK 5 using KiotaJsonSerializer (most properties are missing) { "attachments": [ { "id": "AQMkAGMzNjQ3OWJlLWRjNWItNDA2Yy04YjgxLWFiZWJmYmFiMmQ4NQBGAAADAXcZWMvdbEaSDZn7g6eWdAcAyJ0XzcbX2UiDcSve6RCRbQAAAgEMAAAAyJ0XzcbX2UiDcSve6RCRbQADejg3jAAAAAESABAA5Ba9GDKzzEiqXUYQ4M_hGA==", "@odata.type": "#microsoft.graph.fileAttachment", "@odata.mediaContentType": "text/plain", "contentType": "text/plain", "isInline": false, "lastModifiedDateTime": "2024-08-22T09:35:01+00:00", "name": "small-attachment.txt", "size": 340, "contentBytes": "U21hbGwgYXR0YWNobWVudA==", "contentId": "321DC8B86A0C41498D776B1374091089@eurprd07.prod.outlook.com" } ], "sender": { "emailAddress": { "address": "myaddress@mycompany.com", "name": "My Name" } } } - Serialization in SDK 4 using JsonSerializer (everything is present) { "bccRecipients": [], "body": { "content": "\r\n

Test with Small attachment

", "contentType": "Html" }, "ccRecipients": [], "from": { "emailAddress": { "address": "myaddress@mycompany.com", "name": "My Name" } }, "hasAttachments": true, "receivedDateTime": "2024-08-22T09:35:01+00:00", "replyTo": [], "sender": { "emailAddress": { "address": "myaddress@mycompany.com", "name": "My Name" } }, "sentDateTime": "2024-08-22T09:34:47+00:00", "subject": "Small attachment", "toRecipients": [ { "emailAddress": { "address": "hisaddress@mycompany.com", "name": "His Name" } } ], "attachments": [ { "contentBytes": "U21hbGwgYXR0YWNobWVudA==", "contentId": "321DC8B86A0C41498D776B1374091089@eurprd07.prod.outlook.com", "contentType": "text/plain", "isInline": false, "lastModifiedDateTime": "2024-08-22T09:35:01+00:00", "name": "small-attachment.txt", "size": 340, "id": "AQMkAGMzNjQ3OWJlLWRjNWItNDA2Yy04YjgxLWFiZWJmYmFiMmQ4NQBGAAADAXcZWMvdbEaSDZn7g6eWdAcAyJ0XzcbX2UiDcSve6RCRbQAAAgEMAAAAyJ0XzcbX2UiDcSve6RCRbQADejg3jAAAAAESABAA5Ba9GDKzzEiqXUYQ4M_hGA==", "@odata.type": "#microsoft.graph.fileAttachment", "@odata.mediaContentType": "text/plain" } ], "createdDateTime": "2024-08-22T09:35:01+00:00", "lastModifiedDateTime": "2024-08-22T09:35:17+00:00", "id": "AQMkAGMzNjQ3OWJlLWRjNWItNDA2Yy04YjgxLWFiZWJmYmFiMmQ4NQBGAAADAXcZWMvdbEaSDZn7g6eWdAcAyJ0XzcbX2UiDcSve6RCRbQAAAgEMAAAAyJ0XzcbX2UiDcSve6RCRbQADejg3jAAAAA==", "@odata.type": "microsoft.graph.message", "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('hisaddress%40mycompany.com')/messages(bccRecipients,body,ccRecipients,createdDateTime,from,hasAttachments,id,lastModifiedDateTime,receivedDateTime,replyTo,sender,sentDateTime,subject,toRecipients)/$entity", "@odata.etag": "W/\"CQAAABYAAADInRfNxtfZSINxK97pEJFtAAN54mtE\"" } ```

Configuration

OS: Windows server 2019 64 bits

Other information

No response

andrueastman commented 2 months ago

Thanks for raising this @RogerTimmerman

This is related to the issue at https://github.com/microsoft/kiota-dotnet/issues/310 and resolved with https://github.com/microsoft/kiota-dotnet/pull/311

Any chance you can confirm that the code below works if you include the latest version of https://www.nuget.org/packages/Microsoft.Kiota.Abstractions/ (This will be included automatically in the next SDK version) the following works out for you?

var jsonString = await KiotaSerializer.SerializeAsStringAsync("application/json", result, false);
RogerTimmerman commented 2 months ago

Hi @andrueastman Thanks for the response, I will check as soon as possible. Roger

RogerTimmerman commented 2 months ago

@andrueastman Hi Andrew,

I now use a newer Microsoft.Kiota.Abstractions (>= 1.12.0) assembly and it works, Emails are serialized and deserialized properly, with and without attachments.

Thanks for the guidance With regards Roger Timmerman