Skip to content

Commit

Permalink
Fix Writing Delta Responses
Browse files Browse the repository at this point in the history
-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)
  • Loading branch information
mikepizzo committed Dec 3, 2024
1 parent 3813ee7 commit 95b79d7
Show file tree
Hide file tree
Showing 10 changed files with 671 additions and 124 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.Abstracts;
Expand Down Expand Up @@ -56,10 +57,6 @@ public override async Task WriteObjectAsync(object graph, Type type, ODataMessag
}

IEdmEntitySetBase entitySet = writeContext.NavigationSource as IEdmEntitySetBase;
if (entitySet == null)
{
throw new SerializationException(SRResources.EntitySetMissingDuringSerialization);
}

IEdmTypeReference feedType = writeContext.GetEdmType(graph, type);
Contract.Assert(feedType != null);
Expand Down Expand Up @@ -162,11 +159,19 @@ private async Task WriteDeltaResourceSetAsync(IEnumerable enumerable, IEdmTypeRe
}

lastResource = item;
DeltaItemKind kind = GetDelteItemKind(item);
DeltaItemKind kind = GetDeltaItemKind(item);
switch (kind)
{
case DeltaItemKind.DeletedResource:
await WriteDeltaDeletedResourceAsync(item, writer, writeContext).ConfigureAwait(false);
// hack. if the WriteDeltaDeletedResourceAsync isn't overridden, call the new version
if (WriteDeltaDeletedResourceAsyncIsOverridden())
{
await WriteDeltaDeletedResourceAsync(item, writer, writeContext).ConfigureAwait(false);
}
else
{
await WriteDeletedResourceAsync(item, elementType, writer, writeContext).ConfigureAwait(false);
}
break;
case DeltaItemKind.DeltaDeletedLink:
await WriteDeltaDeletedLinkAsync(item, writer, writeContext).ConfigureAwait(false);
Expand Down Expand Up @@ -212,7 +217,7 @@ await entrySerializer.WriteDeltaObjectInlineAsync(item, elementType, writer, wri
/// <param name="writeContext">The serializer context.</param>
/// <returns>The function that generates the NextLink from an object.</returns>
/// <returns></returns>
internal static Func<object, Uri> GetNextLinkGenerator(ODataDeltaResourceSet deltaResourceSet, IEnumerable enumerable, ODataSerializerContext writeContext)
internal static Func<object, Uri> GetNextLinkGenerator(ODataResourceSetBase deltaResourceSet, IEnumerable enumerable, ODataSerializerContext writeContext)
{
return ODataResourceSetSerializer.GetNextLinkGenerator(deltaResourceSet, enumerable, writeContext);
}
Expand Down Expand Up @@ -266,6 +271,8 @@ public virtual ODataDeltaResourceSet CreateODataDeltaResourceSet(IEnumerable fee
/// <param name="value">The object to be written.</param>
/// <param name="writer">The <see cref="ODataDeltaWriter" /> to be used for writing.</param>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
[Obsolete("WriteDeltaDeletedResourceAsync(object, ODataWriter, ODataSerializerContext) is Deprecated and will be removed in the next version." +
"Please use WriteDeletedResourceAsync(object, IEdmEntityTypeReference, ODataWriter, ODataSerializerContext)")]
public virtual async Task WriteDeltaDeletedResourceAsync(object value, ODataWriter writer, ODataSerializerContext writeContext)
{
if (writer == null)
Expand Down Expand Up @@ -304,6 +311,27 @@ public virtual async Task WriteDeltaDeletedResourceAsync(object value, ODataWrit
}
}

/// <summary>
/// Writes the given deltaDeletedEntry specified by the parameter graph as a part of an existing OData message using the given
/// messageWriter and the writeContext.
/// </summary>
/// <param name="value">The object to be written.</param>
/// <param name="expectedType">The expected type of the deleted resource.</param>
/// <param name="writer">The <see cref="ODataDeltaWriter" /> to be used for writing.</param>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual async Task WriteDeletedResourceAsync(object value, IEdmStructuredTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}

//todo:use serializer provider
ODataDeletedResourceSerializer deletedResourceSerializer = new ODataDeletedResourceSerializer(SerializerProvider);

await deletedResourceSerializer.WriteObjectInlineAsync(value, expectedType, writer, writeContext);
}

/// <summary>
/// Writes the given deltaDeletedLink specified by the parameter graph as a part of an existing OData message using the given
/// messageWriter and the writeContext.
Expand Down Expand Up @@ -382,7 +410,7 @@ public virtual async Task WriteDeltaLinkAsync(object value, ODataWriter writer,
}
}

internal DeltaItemKind GetDelteItemKind(object item)
internal DeltaItemKind GetDeltaItemKind(object item)
{
IEdmChangedObject edmChangedObject = item as IEdmChangedObject;
if (edmChangedObject != null)
Expand Down Expand Up @@ -414,4 +442,11 @@ private static IEdmStructuredTypeReference GetResourceType(IEdmTypeReference fee
string message = Error.Format(SRResources.CannotWriteType, typeof(ODataDeltaResourceSetSerializer).Name, feedType.FullName());
throw new SerializationException(message);
}

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

0 comments on commit 95b79d7

Please sign in to comment.