kiota-java icon indicating copy to clipboard operation
kiota-java copied to clipboard

Serialization methods overloads to disable the "only changed properties behaviour"

Open baywet opened this issue 1 year ago • 4 comments

We recently got very similar questions: https://github.com/microsoftgraph/msgraph-sdk-java/issues/1886 https://github.com/microsoftgraph/msgraph-sdk-java/issues/1879

People generally want to:

  1. get something from the service
  2. serialize it to store it locally, in a document db, etc...

While our serialization helpers are helpful, they fall short of one aspect: they don't allow the caller to specify "serialize everything" (as opposed to serializing only the changed properties).

My suggestion here is to:

  1. add an overload that'd accept a boolean parameter "serializeOnlyCHangedValues" default false
  2. have the existing implementations call into that overload, with default false (the behaviour will change)
  3. add a routine that crawls the object tree whenever the value is false, and whenever it finds a backed model, flips the serialize only changed values from the backing store.
  4. once the serialization is done, crawl the tree again to restore things.

@sebastienlevert to provide input on this one before we start any work, and if we get an agreement here, I'll replicate the issue to the other repos.

baywet avatar Mar 20 '24 12:03 baywet

Just so I understand. When serializing models (across languages) we only serialize what has changed when the backing store is enabled, right? If this is the case, I agree we should have options to "deep" serialize vs only what is in the backing store.

If it's the case without the backing store, then I think this is a bug, though...

sebastienlevert avatar Mar 22 '24 19:03 sebastienlevert

this is only present with the backing store. And we don't want to give people the option, but rather have a consistent behaviour. When they change a collection (set the collection again), we should consider all items within the collection changed, regardless of where they came from. (this is broken) If they only insert/remove items to the collection, then only individual items should be considered changed. (that aspect works today)

baywet avatar Mar 22 '24 19:03 baywet

Just to make it clear, can you provide a tiny example of before / after of a collection that needs to be serialized?

sebastienlevert avatar Mar 25 '24 13:03 sebastienlevert

fixing the broken serialization of collections

var joeUser = client.users.byId("[email protected]").get();
var userResponse = client.users.get();
userResponse.SetValue(new User[] { joeUser });
KiotaJsonSerialization.serializeAsString(userReponse);

results in

{
   "value": []
}

when it should result in

{
   "value": [{"displayName": "Joe Smith", "email": "[email protected]"}]
}

(that illustrates the broken part)

Option to serialize everything when things didn't change

var userResponse = client.users.get();
KiotaJsonSerialization.serializeAsString(userReponse);

results in

{}

when it should result in

{
   "value": [{"displayName": "Joe Smith", "email": "[email protected]"}]
}

baywet avatar Apr 15 '24 17:04 baywet

For everyone's context, we recently went through this in dotnet and should replicate the changes here. https://github.com/microsoft/kiota-dotnet/pull/367 https://github.com/microsoft/kiota-dotnet/pull/311

baywet avatar Sep 10 '24 13:09 baywet