Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deltas #1365

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Fix deltas #1365

wants to merge 6 commits into from

Conversation

mikepizzo
Copy link
Member

Fixes #1360

-only write properties in deltaset items that have been set
-support writing additional properties for deleted entities
-nested resource sets that aren't deltasets should be written w/o @delta
-factored out ODataDeletedResourceSerializer
-support writing delta payloads w/out knowing navigation source (i.e., when serializing results from a function)
Leverage more common functionality in ODataResourceSerializer.
@mikepizzo mikepizzo marked this pull request as ready for review December 3, 2024 21:15
@@ -75,7 +75,7 @@ public async Task DeltaSet_WithDeletedAndODataId_IsSerializedSuccessfully()
{'ID':2,'Name':'Employee2','[email protected]':[{'@id':'Friends(1)'}]}
]}";

string expectedResponse = "{\"@context\":\"http://localhost/convention/$metadata#Employees/$delta\",\"value\":[{\"ID\":1,\"Name\":\"Employee1\",\"Friends@delta\":[{\"@removed\":{\"reason\":\"changed\"},\"@id\":\"http://host/service/Friends(1)\"}]},{\"ID\":2,\"Name\":\"Employee2\",\"Friends@delta\":[{\"Id\":1}]}]}";
string expectedResponse = "{\"@context\":\"http://localhost/convention/$metadata#Employees/$delta\",\"value\":[{\"ID\":1,\"Name\":\"Employee1\",\"Friends@delta\":[{\"@removed\":{\"reason\":\"changed\"},\"@id\":\"http://host/service/Friends(1)\",\"id\":1}]},{\"ID\":2,\"Name\":\"Employee2\",\"Friends@delta\":[{\"Id\":1}]}]}";
Copy link
Member

@xuzhg xuzhg Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dose the expected response look different? and since it contains @id, why does it contain "id" again? #ByDesign

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we weren't returning properties of a deleted resource even if they were set by the service, which is why id was not being returned (which was a bug).

In this case, both the @id and the id were explicitly set in the controller (they were both set in the request payload which is echoed in the response payload), so they should both be returned in the response.

Change ODataDeletedResourceSerializer to derive from ODataResourceSerializer
@mikepizzo mikepizzo requested a review from xuzhg December 11, 2024 00:07
// Preserve setting of OmitODataPrefix
if (writerSettings.Version.GetValueOrDefault() == ODataVersion.V4)
{
writerSettings.SetOmitODataPrefix(writerSettings.GetOmitODataPrefix(ODataVersion.V4), ODataVersion.V401);
Copy link
Member

@xuzhg xuzhg Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we enable it using configuration? not hard-coded?
By default, it's ok to enable the configuration, but at least , maybe we need a way to let customer change the default? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code just fixes a bug where we weren't honoring the writing of the odata prefix in cases where we did change the version. I'd like to separately track if the user can override serializing deltas as 4.01, as this is existing behavior, and changing that behavior is outside of the scope of this PR.

Error.Format(SRResources.TypeCannotBeSerialized, expectedType.ToTraceString()));
}
await serializer.WriteResourceContent(writer, selectExpandNode, resourceContext, /*isDelta*/ true);
await writer.WriteEndAsync().ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we call 'ConfigureAwait' for all await?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently always use ConfigureAwait(false) to improve perf and avoid possible deadlocks because we are a library and not an app.

private bool WriteDeltaDeletedResourceAsyncIsOverridden()
{
MethodInfo method = GetType().GetMethod("WriteDeltaDeletedResourceAsync", new Type[] { typeof(object), typeof(ODataWriter), typeof(ODataSerializerContext) });
Contract.Assert(method != null, "WriteDeltaDeletedResourceAsync is not defined.");
Copy link
Member

@xuzhg xuzhg Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. can we leave a details comment for this method?
  2. Moreover, what about if customer creates/customize his own ODataDeletedResourceSerilizer? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Added comments
  2. This is only here for backward compatibility. If the user customizes their own ODataDeletedResourceSerializer, they can provide whatever custom logic they need.

/// ODataSerializer for serializing instances of <see cref="IEdmDeltaDeletedResourceObject"/>/>
/// </summary>
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Relies on many ODataLib classes.")]
public class ODataDeletedResourceSerializer : ODataResourceSerializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see to register this serializer into the DI? Will you do it later or miss that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regular method of using the EdmTypeSerializer doesn't work because it doesn't differentiate between writing a resource and writing a deleted resource. So, I added code to the only place the DeletedResourceSerializer is created, which is from the DeltaResourceSetSerializer, to use DI, and registered the ODataDeletedResourceSerailizer in AddDefaultApiServices. Note that DeltaResourceSetSerilizer falls back to directly creating the ODataDeletedResoruceSerializer if GetService returns null, for example, if the service isn't using our AddDefaultWebApiServices.

Copy link
Member

@xuzhg xuzhg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I leave couple comments, but overall, it looks good to me except the DI related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Address issues serializing a delta response payload from a function
2 participants