microsoft / kiota-java

Java libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
24 stars 25 forks source link

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

Closed jasonjoh closed 1 week ago

jasonjoh commented 3 months ago

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();
baywet commented 3 months ago

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.

Ndiritu commented 1 week ago

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