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

KiotaJsonSerialization.serializeAsString should have some mechanism to override backing store functionality

Open jasonjoh opened this issue 1 year ago • 1 comments

Copied from https://github.com/microsoftgraph/msgraph-sdk-java/issues/2064. There should be some mechanism in KiotaJsonSerialization.serializeAsString to override backing store so that all fields get serialized without requiring a developer to manually muck with the backing store.

Describe the bug

final Subscription subscription = graphClient.subscriptions().post(subscriptionRequest);
final var subscriptionJson = KiotaJsonSerialization.serializeAsString(subscription);

This results in subscriptionJson being {}. This was originally reported in #1879.

The suggested fix to call subscription.getBackingStore().setReturnOnlyChangedValues(false); did not change the result. It should be noted that subscription.getBackingStore().getReturnOnlyChangedValues(); returned false before this anyway. On a whim, I tried calling subscription.getBackingStore().setReturnOnlyChangedValues(true); before serializing, and I got a partial result:

{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#subscriptions/$entity"}

Expected behavior

subscriptionJson should be a full JSON representation of the subscription object returned by the service.

How to reproduce

Code above.

Graph SDK Version

6.13.0

Latest version known to work for scenario above?

No response

Known Workarounds

This works but is way less convenient than using the static methods. It also requires an explicit reference to microsoft-kiota-serialization-json in your dependencies, which I suspect can cause problems down the line as the SDK bumps its versions.

final var writer = new JsonSerializationWriter();
writer.writeObjectValue(null, subscription);
final var bytes = writer.getSerializedContent().readAllBytes();
final var subscriptionJson = new String(bytes, "UTF-8");
writer.close();

jasonjoh avatar Jun 28 '24 16:06 jasonjoh

Thanks for the suggestion. The solution to that would be to:

  1. add a serializeOnlyChangedValues boolean parameter to this method
  2. in the body test if the parsable is a backed model, if so switch the flag. (recursively)
  3. serialize
  4. switch the flag back
  5. add an overload without the parameter, which passes false
  6. reflect all the overloads all the way up to the Json part

Orthogonally, we might have a bug with this flag we should investigate.

Prior work in the space https://github.com/microsoft/kiota-abstractions-dotnet/pull/95 to look at during the bug investigation.

Once this is done, we need to open similar issues for other languages.

baywet avatar Jun 28 '24 17:06 baywet

seems to be a duplicate of https://github.com/microsoft/kiota-java/issues/1131

Ndiritu avatar Sep 23 '24 14:09 Ndiritu