microsoft / kiota-dotnet

Abstractions library for the Kiota generated SDKs in dotnet
https://aka.ms/kiota/docs
MIT License
40 stars 34 forks source link

Prevent resetting of InMemoryBackingStore when serializing #467

Closed SeverinNauer closed 1 day ago

SeverinNauer commented 1 week ago

Currently the tracked properties in the InMemoryBackingStore get reset when serializing an object. For my purposes I want to create a report of the objects that are going to be sent to the graph api before actually sending them. At the moment this is not easily possible, because I can only serialize them once.

This is a short example of the problem I want to avoid

var group = new Group()
{
    DisplayName = "Test Group"
};
var serialized1 = await group.SerializeAsJsonStringAsync(true);
// {"@odata.type":"#microsoft.graph.group","displayName":"Test Group"}
var serialized2 = await group.SerializeAsJsonStringAsync(true);
// {}

Is there a way to avoid this behaviour? The only workaround I know is to disable (which is not an option for me) or replace the BackingStore implementation. I'm okay with writing an own implementation of the BackingStore but it feels kind of dirty since I basically need to copy past the InMemoryBackingStore. I'm also not sure if I'm going to run into other troubles down the line if I do this.

baywet commented 1 week ago

Hi @SeverinNauer Thank you for using kiota and for reaching out.

This is a more advanced scenario we didn't necessarily designed for.

This is where the reset is happening

https://github.com/microsoft/kiota-dotnet/blob/e366f10cfacdf5dedede0cee8f0492699942e738/src/abstractions/store/BackingStoreSerializationWriterProxyFactory.cs#L27

My suggestion is to create your "own backing store serialization writer factory" for the first step that would effectively not have this second callback (so it doesn't mutate the backing store).

Using that factory you'll be able to get a serialization writer, and write the object for logging purposes.

Then using the "regular pipeline" you'll be able to send the payload to the service.

Let us know if you have any additional comments or questions.

microsoft-github-policy-service[bot] commented 1 week ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.