Skip to content

Commit

Permalink
bulk ops serialization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ElizabethOkerio committed Aug 11, 2022
1 parent 8298f80 commit e493e2a
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ internal ODataSerializer GetODataPayloadSerializerImpl(Type type, Func<IEdmModel
{
return _rootContainer.GetRequiredService<ODataMetadataSerializer>();
}
else if (TypeHelper.IsTypeAssignableFrom(typeof(IDeltaSet), type))
{
return _rootContainer.GetRequiredService<ODataDeltaFeedSerializer>();
}

// Get the model. Using a Func<IEdmModel> to delay evaluation of the model
// until after the above checks have passed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
//------------------------------------------------------------------------------

using System;
using System.CodeDom;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNet.OData.Builder;
Expand All @@ -25,6 +28,7 @@ namespace Microsoft.AspNet.OData.Formatter.Serialization
public class ODataDeltaFeedSerializer : ODataEdmTypeSerializer
{
private const string DeltaFeed = "deltafeed";
IEdmStructuredTypeReference _elementType;

/// <summary>
/// Initializes a new instance of <see cref="ODataDeltaFeedSerializer"/>.
Expand Down Expand Up @@ -60,6 +64,7 @@ public override void WriteObject(object graph, Type type, ODataMessageWriter mes
}

IEdmTypeReference feedType = writeContext.GetEdmType(graph, type);

Contract.Assert(feedType != null);

IEdmEntityTypeReference entityType = GetResourceType(feedType).AsEntity();
Expand Down Expand Up @@ -93,6 +98,7 @@ public override async Task WriteObjectAsync(object graph, Type type, ODataMessag
}

IEdmTypeReference feedType = writeContext.GetEdmType(graph, type);

Contract.Assert(feedType != null);

IEdmEntityTypeReference entityType = GetResourceType(feedType).AsEntity();
Expand Down Expand Up @@ -186,6 +192,7 @@ private void WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, OData
Contract.Assert(feedType != null);

IEdmStructuredTypeReference elementType = GetResourceType(feedType);
_elementType = elementType;

if (elementType.IsComplex())
{
Expand Down Expand Up @@ -234,13 +241,31 @@ private void WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, OData
}

lastResource = entry;
IEdmChangedObject edmChangedObject = entry as IEdmChangedObject;
if (edmChangedObject == null)

EdmDeltaEntityKind deltaEntityKind;
if (writeContext.IsUntyped)
{
IEdmChangedObject edmChangedObject = entry as IEdmChangedObject;
if (edmChangedObject == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
}

deltaEntityKind = edmChangedObject.DeltaKind;
}
else
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
IDeltaSetItem deltaSetItem = entry as IDeltaSetItem;

if (deltaSetItem == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
}

deltaEntityKind = deltaSetItem.DeltaKind;
}

switch (edmChangedObject.DeltaKind)
switch (deltaEntityKind)
{
case EdmDeltaEntityKind.DeletedEntry:
WriteDeltaDeletedEntry(entry, writer, writeContext);
Expand All @@ -254,6 +279,7 @@ private void WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, OData
case EdmDeltaEntityKind.Entry:
{
ODataResourceSerializer entrySerializer = SerializerProvider.GetEdmTypeSerializer(elementType) as ODataResourceSerializer;

if (entrySerializer == null)
{
throw new SerializationException(
Expand Down Expand Up @@ -289,6 +315,7 @@ private async Task WriteFeedAsync(IEnumerable enumerable, IEdmTypeReference feed
Contract.Assert(feedType != null);

IEdmStructuredTypeReference elementType = GetResourceType(feedType);
_elementType = elementType;

if (elementType.IsComplex())
{
Expand Down Expand Up @@ -337,13 +364,32 @@ private async Task WriteFeedAsync(IEnumerable enumerable, IEdmTypeReference feed
}

lastResource = entry;
IEdmChangedObject edmChangedObject = entry as IEdmChangedObject;
if (edmChangedObject == null)

EdmDeltaEntityKind deltaEntityKind;
if (writeContext.IsUntyped)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
IEdmChangedObject edmChangedObject = entry as IEdmChangedObject;
if (edmChangedObject == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
}

deltaEntityKind = edmChangedObject.DeltaKind;
}
else
{
IDeltaSetItem deltaSetItem = entry as IDeltaSetItem;

if (deltaSetItem == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, enumerable.GetType().FullName));
}

deltaEntityKind = deltaSetItem.DeltaKind;
}

switch (edmChangedObject.DeltaKind)

switch (deltaEntityKind)
{
case EdmDeltaEntityKind.DeletedEntry:
await WriteDeltaDeletedEntryAsync(entry, writer, writeContext);
Expand Down Expand Up @@ -438,15 +484,23 @@ public virtual ODataDeltaResourceSet CreateODataDeltaFeed(IEnumerable feedInstan
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual void WriteDeltaDeletedEntry(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeletedResource deletedResource = GetDeletedResource(graph);
ODataResourceSerializer serializer = SerializerProvider.GetEdmTypeSerializer(_elementType) as ODataResourceSerializer;
ResourceContext resourceContext = serializer.GetResourceContext(graph, writeContext);
SelectExpandNode selectExpandNode = serializer.CreateSelectExpandNode(resourceContext);

if (deletedResource != null)
if (selectExpandNode != null)
{
writer.WriteStart(deletedResource);
writer.WriteEnd();
ODataDeletedResource deletedResource = GetDeletedResource(graph, resourceContext, serializer, selectExpandNode, writeContext.IsUntyped);

if (deletedResource != null)
{
writer.WriteStart(deletedResource);
serializer.WriteDeltaComplexProperties(selectExpandNode, resourceContext, writer);
writer.WriteEnd();
}
}
}

Expand All @@ -456,14 +510,22 @@ public virtual void WriteDeltaDeletedEntry(object graph, ODataWriter writer, ODa
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual async Task WriteDeltaDeletedEntryAsync(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeletedResource deletedResource = GetDeletedResource(graph);
if (deletedResource != null)
ODataResourceSerializer serializer = SerializerProvider.GetEdmTypeSerializer(_elementType) as ODataResourceSerializer;
ResourceContext resourceContext = serializer.GetResourceContext(graph, writeContext);
SelectExpandNode selectExpandNode = serializer.CreateSelectExpandNode(resourceContext);

if (selectExpandNode != null)
{
await writer.WriteStartAsync(deletedResource);
await writer.WriteEndAsync();
ODataDeletedResource deletedResource = GetDeletedResource(graph, resourceContext, serializer, selectExpandNode, writeContext.IsUntyped);

if (deletedResource != null)
{
await writer.WriteStartAsync(deletedResource);
await writer.WriteEndAsync();
}
}
}

Expand All @@ -473,7 +535,7 @@ public virtual async Task WriteDeltaDeletedEntryAsync(object graph, ODataWriter
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual void WriteDeltaDeletedLink(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeltaDeletedLink deltaDeletedLink = GetDeletedLink(graph);
Expand All @@ -489,7 +551,7 @@ public virtual void WriteDeltaDeletedLink(object graph, ODataWriter writer, ODat
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual async Task WriteDeltaDeletedLinkAsync(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeltaDeletedLink deltaDeletedLink = GetDeletedLink(graph);
Expand All @@ -505,7 +567,7 @@ public virtual async Task WriteDeltaDeletedLinkAsync(object graph, ODataWriter w
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public virtual void WriteDeltaLink(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeltaLink deltaLink = GetDeltaLink(graph);
Expand All @@ -521,7 +583,7 @@ public virtual void WriteDeltaLink(object graph, ODataWriter writer, ODataSerial
/// </summary>
/// <param name="graph">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>
/// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
public async Task WriteDeltaLinkAsync(object graph, ODataWriter writer, ODataSerializerContext writeContext)
{
ODataDeltaLink deltaLink = GetDeltaLink(graph);
Expand All @@ -531,22 +593,42 @@ public async Task WriteDeltaLinkAsync(object graph, ODataWriter writer, ODataSer
}
}

private ODataDeletedResource GetDeletedResource(object graph)

private ODataDeletedResource GetDeletedResource(object graph, ResourceContext resourceContext, ODataResourceSerializer serializer, SelectExpandNode selectExpandNode, bool isUntyped)
{
EdmDeltaDeletedEntityObject edmDeltaDeletedEntity = graph as EdmDeltaDeletedEntityObject;
if (edmDeltaDeletedEntity == null)
string navigationSource;
ODataDeletedResource deletedResource = serializer.CreateDeletedResource(selectExpandNode, resourceContext);

if (isUntyped)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
EdmDeltaDeletedEntityObject edmDeltaDeletedEntity = graph as EdmDeltaDeletedEntityObject;
if (edmDeltaDeletedEntity == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
}

deletedResource.Id = StringToUri(edmDeltaDeletedEntity.Id ?? string.Empty);
deletedResource.Reason = edmDeltaDeletedEntity.Reason;
navigationSource = edmDeltaDeletedEntity.NavigationSource.Name;
}
else
{
IDeltaDeletedEntityObject deltaDeletedEntity = graph as IDeltaDeletedEntityObject;
if (deltaDeletedEntity == null)
{
throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName));
}

Uri id = StringToUri(edmDeltaDeletedEntity.Id);
ODataDeletedResource deletedResource = new ODataDeletedResource(id, edmDeltaDeletedEntity.Reason);
deletedResource.Id = deltaDeletedEntity.Id;
deletedResource.Reason = deltaDeletedEntity.Reason;
navigationSource = deltaDeletedEntity.NavigationSource;
}

if (edmDeltaDeletedEntity.NavigationSource != null)
if (navigationSource != null)
{
ODataResourceSerializationInfo serializationInfo = new ODataResourceSerializationInfo
{
NavigationSourceName = edmDeltaDeletedEntity.NavigationSource.Name
NavigationSourceName = navigationSource
};
deletedResource.SetSerializationInfo(serializationInfo);
}
Expand Down
Loading

0 comments on commit e493e2a

Please sign in to comment.