Skip to content

Commit

Permalink
update based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ElizabethOkerio committed Oct 14, 2022
1 parent f414b57 commit 069902c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ internal async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpa
}

private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProperty, SelectExpandClause selectExpandClause,
ResourceContext resourceContext, ODataWriter writer, Type type = null)
ResourceContext resourceContext, ODataWriter writer, Type navigationPropertyType = null)
{
Contract.Assert(edmProperty != null);
Contract.Assert(resourceContext != null);
Expand Down Expand Up @@ -376,7 +376,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;
nestedWriteContext.Type = navigationPropertyType;

// write object.

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

private async Task WriteDeltaComplexAndExpandedNavigationPropertyAsync(IEdmProperty edmProperty, SelectExpandClause selectExpandClause,
ResourceContext resourceContext, ODataWriter writer, Type type = null)
ResourceContext resourceContext, ODataWriter writer, Type navigationPropertyType = null)
{
Contract.Assert(edmProperty != null);
Contract.Assert(resourceContext != null);
Expand Down Expand Up @@ -434,7 +434,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;
nestedWriteContext.Type = navigationPropertyType;

// write object.

Expand Down Expand Up @@ -648,7 +648,6 @@ private void WriteResource(object graph, ODataWriter writer, ODataSerializerCont
WriteDynamicComplexProperties(resourceContext, writer);
WriteNavigationLinks(selectExpandNode, resourceContext, writer);
WriteExpandedNavigationProperties(selectExpandNode, resourceContext, writer);
WriteNestedNavigationProperties(selectExpandNode, resourceContext, writer);
WriteReferencedNavigationProperties(selectExpandNode, resourceContext, writer);
writer.WriteEnd();
}
Expand Down Expand Up @@ -691,7 +690,6 @@ await writer.WriteEntityReferenceLinkAsync(new ODataEntityReferenceLink
await WriteDynamicComplexPropertiesAsync(resourceContext, writer);
await WriteNavigationLinksAsync(selectExpandNode, resourceContext, writer);
await WriteExpandedNavigationPropertiesAsync(selectExpandNode, resourceContext, writer);
await WriteNestedNavigationPropertiesAsync(selectExpandNode, resourceContext, writer);
await WriteReferencedNavigationPropertiesAsync(selectExpandNode, resourceContext, writer);
await writer.WriteEndAsync();
}
Expand Down Expand Up @@ -737,12 +735,12 @@ public virtual SelectExpandNode CreateSelectExpandNode(ResourceContext resourceC
/// <returns>The created <see cref="ODataResource"/>.</returns>
public virtual ODataResource CreateResource(SelectExpandNode selectExpandNode, ResourceContext resourceContext)
{
ODataResource resource = CreateResourceBase(selectExpandNode, resourceContext, false) as ODataResource;
ODataResource resource = CreateResource(selectExpandNode, resourceContext, false) as ODataResource;
return resource;
}


private ODataResourceBase CreateResourceBase(SelectExpandNode selectExpandNode, ResourceContext resourceContext, bool isDeletedResource)
private ODataResourceBase CreateResource(SelectExpandNode selectExpandNode, ResourceContext resourceContext, bool isDeletedResource)
{
if (selectExpandNode == null)
{
Expand Down Expand Up @@ -880,7 +878,7 @@ private ODataResourceBase CreateResourceBase(SelectExpandNode selectExpandNode,
/// <returns>The created <see cref="ODataDeletedResource"/>.</returns>
public virtual ODataDeletedResource CreateDeletedResource(SelectExpandNode selectExpandNode, ResourceContext resourceContext)
{
ODataDeletedResource resource = CreateResourceBase(selectExpandNode, resourceContext, true) as ODataDeletedResource;
ODataDeletedResource resource = CreateResource(selectExpandNode, resourceContext, true) as ODataDeletedResource;

return resource;
}
Expand Down Expand Up @@ -1121,7 +1119,7 @@ private void WriteNavigationLinks(SelectExpandNode selectExpandNode, ResourceCon
Contract.Assert(selectExpandNode != null);
Contract.Assert(resourceContext != null);

if (selectExpandNode.SelectedNavigationProperties == null || resourceContext.IsPostRequest)
if (selectExpandNode.SelectedNavigationProperties == null)
{
return;
}
Expand All @@ -1142,7 +1140,7 @@ private async Task WriteNavigationLinksAsync(SelectExpandNode selectExpandNode,
Contract.Assert(selectExpandNode != null);
Contract.Assert(resourceContext != null);

if (selectExpandNode.SelectedNavigationProperties == null || resourceContext.IsPostRequest)
if (selectExpandNode.SelectedNavigationProperties == null)
{
return;
}
Expand Down Expand Up @@ -1390,6 +1388,18 @@ private IEnumerable<KeyValuePair<IEdmStructuralProperty, PathSelectItem>> GetPro
{
if (changedProperties == null || changedProperties.Contains(complexProperty.Key.Name))
{
IEdmTypeReference type = complexProperty.Key == null ? null : complexProperty.Key.Type;

if (type != null && resourceContext.EdmModel != null)
{
Type clrType = EdmLibHelpers.GetClrType(type.AsStructured(), resourceContext.EdmModel);

if (clrType != null && clrType == typeof(ODataIdContainer))
{
continue;
}
}

yield return complexProperty;
}
}
Expand Down Expand Up @@ -1435,32 +1445,6 @@ private IEnumerable<KeyValuePair<IEdmNavigationProperty, Type>> GetNavigationPro
}
}
}
// Serializing nested navigation properties from a deep insert request.
// We currently don't deserialize Deep insert nested resources as Delta<T> but as T. If this was to change in the future, logic in this method will have to change.
else if (resourceContext.IsPostRequest)
{
object instance = resourceContext.ResourceInstance;
PropertyInfo[] properties = instance.GetType().GetProperties();
Dictionary<string, object> propertyNamesAndValues = new Dictionary<string, object>();

foreach (PropertyInfo propertyInfo in properties)
{
string name = propertyInfo.Name;
object value = propertyInfo.GetValue(instance);
propertyNamesAndValues.Add(name, value);
}

foreach (IEdmNavigationProperty navigationProperty in navigationProperties)
{
if (propertyNamesAndValues.TryGetValue(navigationProperty.Name, out object obj))
{
if (obj != null)
{
yield return new KeyValuePair<IEdmNavigationProperty, Type>(navigationProperty, obj.GetType());
}
}
}
}
}

private void WriteExpandedNavigationProperties(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
Expand Down Expand Up @@ -1653,58 +1637,6 @@ await writer.WriteStartAsync(new ODataResourceSet
}
}

private void WriteNestedNavigationProperties(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
{
Debug.Assert(resourceContext != null, "resourceContext != null");
Debug.Assert(writer != null, "writer != null");

if (!resourceContext.IsPostRequest)
{
return;
}

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);
WriteComplexAndExpandedNavigationProperty(navigationProperty.Key, null, resourceContext, writer);
writer.WriteEnd();
}
}

private async Task WriteNestedNavigationPropertiesAsync(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
{
Debug.Assert(resourceContext != null, "resourceContext != null");
Debug.Assert(writer != null, "writer != null");

if (!resourceContext.IsPostRequest)
{
return;
}

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 WriteComplexAndExpandedNavigationPropertyAsync(navigationProperty.Key, null, resourceContext, writer);
await writer.WriteEndAsync();
}
}

private IEnumerable<ODataNestedResourceInfo> CreateNavigationLinks(
IEnumerable<IEdmNavigationProperty> navigationProperties, ResourceContext resourceContext)
{
Expand Down
12 changes: 0 additions & 12 deletions src/Microsoft.AspNet.OData.Shared/ResourceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ public IEdmModel EdmModel
}
}

internal bool IsPostRequest
{
get
{
#if NETCORE
return Request == null ? false : String.Equals(Request.Method, "post", StringComparison.OrdinalIgnoreCase);
#else
return Request == null ? false : Request.Method == HttpMethod.Post;
#endif
}
}

/// <summary>
/// Gets or sets the <see cref="IEdmNavigationSource"/> to which this instance belongs.
/// </summary>
Expand Down

0 comments on commit 069902c

Please sign in to comment.