Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreejithpin committed Oct 25, 2021
1 parent 82918ec commit 50cdd4c
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,13 @@ private void WriteDeltaResource(object graph, ODataWriter writer, ODataSerialize
{
writer.WriteStart(resource);
WriteDeltaComplexProperties(selectExpandNode, resourceContext, writer);
WriteDeltaNavigationProperties(selectExpandNode, resourceContext, writer);
//TODO: Need to add support to write Navigation Links, etc. using Delta Writer
//https://github.com/OData/odata.net/issues/155
//CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
//WriteDynamicComplexProperties(resourceContext, writer);
//WriteNavigationLinks(selectExpandNode.SelectedNavigationProperties, resourceContext, writer);
//WriteExpandedNavigationProperties(selectExpandNode.ExpandedNavigationProperties, resourceContext, writer);
//WriteExpandedNavigationProperties(selectExpandNode, resourceContext, writer);

writer.WriteEnd();
}
Expand All @@ -226,6 +227,7 @@ private async Task WriteDeltaResourceAsync(object graph, ODataWriter writer, ODa
{
await writer.WriteStartAsync(resource);
await WriteDeltaComplexPropertiesAsync(selectExpandNode, resourceContext, writer);
await WriteDeltaNavigationPropertiesAsync(selectExpandNode, resourceContext, writer);
//TODO: Need to add support to write Navigation Links, etc. using Delta Writer
//https://github.com/OData/odata.net/issues/155
//CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
Expand Down Expand Up @@ -275,6 +277,48 @@ internal void WriteDeltaComplexProperties(SelectExpandNode selectExpandNode,
}
}

internal void WriteDeltaNavigationProperties(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
{
Contract.Assert(resourceContext != null);
Contract.Assert(writer != null);

IEnumerable<KeyValuePair<IEdmNavigationProperty, Type>> navigationProperties = GetNavigationPropertiesToWrite(selectExpandNode, resourceContext);

foreach (KeyValuePair<IEdmNavigationProperty, Type> navigationProperty in navigationProperties)
{
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
{
IsCollection = navigationProperty.Key.Type.IsCollection(),
Name = navigationProperty.Key.Name
};

writer.WriteStart(nestedResourceInfo);
WriteDeltaComplexAndExpandedNavigationProperty(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
writer.WriteEnd();
}
}

internal async Task WriteDeltaNavigationPropertiesAsync(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
{
Contract.Assert(resourceContext != null);
Contract.Assert(writer != null);

IEnumerable<KeyValuePair<IEdmNavigationProperty, Type>> navigationProperties = GetNavigationPropertiesToWrite(selectExpandNode, resourceContext);

foreach (KeyValuePair<IEdmNavigationProperty, Type> navigationProperty in navigationProperties)
{
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
{
IsCollection = navigationProperty.Key.Type.IsCollection(),
Name = navigationProperty.Key.Name
};

await writer.WriteStartAsync(nestedResourceInfo);
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
await writer.WriteEndAsync();
}
}

private async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpandNode,
ResourceContext resourceContext, ODataWriter writer)
{
Expand All @@ -298,7 +342,7 @@ private async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpan
}

private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProperty, SelectExpandClause selectExpandClause,
ResourceContext resourceContext, ODataWriter writer)
ResourceContext resourceContext, ODataWriter writer, Type type = null)
{
Contract.Assert(edmProperty != null);
Contract.Assert(resourceContext != null);
Expand Down Expand Up @@ -331,6 +375,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
{
// create the serializer context for the complex and expanded item.
ODataSerializerContext nestedWriteContext = new ODataSerializerContext(resourceContext, selectExpandClause, edmProperty);
nestedWriteContext.Type = type;

// write object.

Expand All @@ -355,7 +400,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
}

private async Task WriteDeltaComplexAndExpandedNavigationPropertyAsync(IEdmProperty edmProperty, SelectExpandClause selectExpandClause,
ResourceContext resourceContext, ODataWriter writer)
ResourceContext resourceContext, ODataWriter writer, Type type = null)
{
Contract.Assert(edmProperty != null);
Contract.Assert(resourceContext != null);
Expand Down Expand Up @@ -388,6 +433,7 @@ await writer.WriteStartAsync(new ODataResourceSet
{
// create the serializer context for the complex and expanded item.
ODataSerializerContext nestedWriteContext = new ODataSerializerContext(resourceContext, selectExpandClause, edmProperty);
nestedWriteContext.Type = type;

// write object.

Expand Down Expand Up @@ -1351,6 +1397,43 @@ private IEnumerable<KeyValuePair<IEdmStructuralProperty, PathSelectItem>> GetPro
}
}

private IEnumerable<KeyValuePair<IEdmNavigationProperty, Type>> GetNavigationPropertiesToWrite(SelectExpandNode selectExpandNode, ResourceContext resourceContext)
{
ISet<IEdmNavigationProperty> navigationProperties = selectExpandNode.SelectedNavigationProperties;

if (navigationProperties != null)
{
IEnumerable<string> changedProperties = null;

if (null != resourceContext.ResourceInstance && resourceContext.ResourceInstance is IDelta deltaObject)
{
changedProperties = deltaObject.GetChangedPropertyNames();
dynamic delta = deltaObject;

foreach (IEdmNavigationProperty navigationProperty in navigationProperties)
{
Object obj = null;
if (changedProperties == null || changedProperties.Contains(navigationProperty.Name) && delta.DeltaNestedResources.TryGetValue(navigationProperty.Name, out obj))
{
yield return new KeyValuePair<IEdmNavigationProperty, Type>(navigationProperty, obj.GetType());
}
}
}
else if(null != resourceContext.EdmObject && resourceContext.EdmObject is IDelta changedObject)
{
changedProperties = changedObject.GetChangedPropertyNames();

foreach (IEdmNavigationProperty navigationProperty in navigationProperties)
{
if (changedProperties == null || changedProperties.Contains(navigationProperty.Name))
{
yield return new KeyValuePair<IEdmNavigationProperty, Type>(navigationProperty, typeof(IEdmChangedObject));
}
}
}
}
}

private void WriteExpandedNavigationProperties(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
{
Contract.Assert(resourceContext != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal bool IsUntyped
get
{ if (_isUntyped == null)
{
_isUntyped = typeof(IEdmObject).IsAssignableFrom(Type);
_isUntyped = typeof(IEdmObject).IsAssignableFrom(Type) || typeof(EdmChangedObjectCollection).IsAssignableFrom(Type);
}

return _isUntyped.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public ITestActionResult Get()
return Ok(Employees.AsQueryable());
}

[EnableQuery]
public ITestActionResult Get(int key)
{
var emp = Employees.SingleOrDefault(e => e.ID == key);
Expand Down Expand Up @@ -235,6 +236,7 @@ public ITestActionResult GetUnTypedFriends(int key)

[ODataRoute("Employees")]
[HttpPatch]
[EnableQuery]
public ITestActionResult PatchEmployees([FromBody] DeltaSet<Employee> coll)
{
InitEmployees();
Expand Down Expand Up @@ -358,6 +360,7 @@ public ITestActionResult PatchUnTypedEmployees([FromBody] EdmChangedObjectCollec


[ODataRoute("Employees({key})")]
[EnableQuery]
public ITestActionResult Patch(int key, [FromBody] Delta<Employee> delta)
{
InitEmployees();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Microsoft.Test.E2E.AspNet.OData.BulkInsert
{
[AutoExpand]
public class Employee
{
[Key]
Expand All @@ -19,7 +20,6 @@ public class Employee
public Gender Gender { get; set; }
public AccessLevel AccessLevel { get; set; }

[AutoExpand]
public List<Friend> Friends { get; set; }

public List<NewFriend> NewFriends { get; set; }
Expand Down Expand Up @@ -67,9 +67,9 @@ public class Friend
{
[Key]
public int Id { get; set; }
[Required]

public string Name { get; set; }
[Range(10, 20)]

public int Age { get; set; }

public List<Order> Orders { get; set; }
Expand Down
Loading

0 comments on commit 50cdd4c

Please sign in to comment.