microsoft / kiota-serialization-json-dotnet

Kiota serialization provider implementation with System.Text.Json
https://aka.ms/kiota/docs
MIT License
23 stars 27 forks source link

Unable to serialise CallRecord types #217

Closed cowlinb6 closed 5 months ago

cowlinb6 commented 5 months ago

Similar issue to #212 but with CallRecord types.

When attempting to serialise a CallRecord type:

var callRecord = await _graphServiceClient.Communications.CallRecords[id]
    .GetAsync((config) =>
    {
        config.QueryParameters.Expand = new string[] { "sessions($expand=segments)" };
    });
var raw = KiotaJsonSerializer.SerializeAsString<CallRecord>(callRecord);

This fails with the error below:

System.InvalidOperationException: AdditionalData can not be null
   at Microsoft.Graph.Models.Entity.get_AdditionalData()
   at Microsoft.Graph.Models.Entity.Serialize(ISerializationWriter writer)
   at Microsoft.Graph.Models.CallRecords.CallRecord.Serialize(ISerializationWriter writer)
   at Microsoft.Kiota.Serialization.Json.JsonSerializationWriter.WriteObjectValue[T](String key, T value, IParsable[] additionalValuesToMerge)
   at Microsoft.Kiota.Abstractions.Serialization.KiotaSerializer.SerializeAsStream[T](String contentType, T value)
   at Microsoft.Kiota.Abstractions.Serialization.KiotaSerializer.SerializeAsString[T](String contentType, T value)
   at Microsoft.Kiota.Abstractions.Serialization.KiotaJsonSerializer.SerializeAsString[T](T value)
   at TeamsPbxTest.GraphClient.GetCallRecord(String id) in C:\Users\Ben\source\code\TeamsPbxTest\TeamsPbxTest\GraphClient.cs:line 177

How should I be serialising this type?

andrueastman commented 5 months ago

Thanks for raising this @cowlinb6

I suspect the error you're seeing is related to https://github.com/microsoft/kiota/pull/4514. However, AdditionalData should be automatically initialized when the object is created in the constructor. Do you still get the error if you add the line below before serializing?

callRecord.AdditionalData = new();
cowlinb6 commented 5 months ago

If I add this:

callRecord.AdditionalData = new Dictionary<string, object>();
var raw = KiotaJsonSerializer.SerializeAsString<CallRecord>(callRecord);

Then it fixes the exception but I only get this in the response:

image

andrueastman commented 5 months ago

This is probably because of the backingStore. We're looking to enhance the KiotaJsonSerializer to have the option to set the flag for you.

Does the following work for you?

var callRecord = await _graphServiceClient.Communications.CallRecords[id]
    .GetAsync((config) =>
    {
        config.QueryParameters.Expand = new string[] { "sessions($expand=segments)" };
    });
callRecord.BackingStore.InitializationCompleted = false;
var raw = KiotaJsonSerializer.SerializeAsString<CallRecord>(callRecord);
cowlinb6 commented 5 months ago

Yes this does work.

Thanks @andrueastman for the quick response on this

andrueastman commented 5 months ago

Thanks for confirming. We'll close this one for now.

We're looking to enhance the KiotaJsonSerializer to have the option to set the flag for you.

Currently tracked for all languages via https://github.com/microsoft/kiota-java/issues/1131