Skip to content

Commit

Permalink
Fixes #2858: Add Virtual method for creating Delta response (#2859)
Browse files Browse the repository at this point in the history
* Fixes #2858: Add Virtual method for creating Delta response

* Update the publicApi
  • Loading branch information
xuzhg authored Nov 6, 2024
1 parent 651c83d commit ee2203a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,29 @@ public virtual string CreateETag(ResourceContext resourceContext)
return null;
}

/// <summary>
/// Creates the <see cref="ODataNestedResourceInfo"/> to be written while writing this delta nested property (complex or entity).
/// </summary>
/// <param name="property">The property for which the nested resource info is being created.</param>
/// <param name="resourceContext">The context for the property instance being written.</param>
/// <returns>The nested resource info to be written. Returns 'null' will omit this property serialization.</returns>
/// <remarks>It enables customer to get more control by overriding this method. </remarks>
public virtual ODataNestedResourceInfo CreateDeltaNestedResourceInfo(IEdmProperty property, ResourceContext resourceContext)
{
if (property == null)
{
throw Error.ArgumentNull(nameof(property));
}

return property.Type != null ?
new ODataNestedResourceInfo
{
IsCollection = property.Type.IsCollection(),
Name = property.Name
} :
null;
}

/// <summary>
/// Creates the <see cref="ODataNestedResourceInfo"/> to be written while writing this dynamic complex property.
/// </summary>
Expand Down Expand Up @@ -705,15 +728,14 @@ internal void WriteDeltaComplexProperties(SelectExpandNode selectExpandNode,

foreach (KeyValuePair<IEdmStructuralProperty, PathSelectItem> complexProperty in complexProperties)
{
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
{
IsCollection = complexProperty.Key.Type.IsCollection(),
Name = complexProperty.Key.Name
};
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(complexProperty.Key, resourceContext);

writer.WriteStart(nestedResourceInfo);
WriteDeltaComplexAndExpandedNavigationProperty(complexProperty.Key, null, resourceContext, writer);
writer.WriteEnd();
if (nestedResourceInfo != null)
{
writer.WriteStart(nestedResourceInfo);
WriteDeltaComplexAndExpandedNavigationProperty(complexProperty.Key, null, resourceContext, writer);
writer.WriteEnd();
}
}
}

Expand All @@ -732,15 +754,14 @@ internal void WriteDeltaNavigationProperties(SelectExpandNode selectExpandNode,

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

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

Expand All @@ -760,15 +781,14 @@ internal async Task WriteDeltaNavigationPropertiesAsync(SelectExpandNode selectE

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

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

Expand All @@ -793,15 +813,14 @@ internal async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpa

foreach (KeyValuePair<IEdmStructuralProperty, PathSelectItem> complexProperty in complexProperties)
{
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
{
IsCollection = complexProperty.Key.Type.IsCollection(),
Name = complexProperty.Key.Name
};
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(complexProperty.Key, resourceContext);

await writer.WriteStartAsync(nestedResourceInfo);
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(complexProperty.Key, null, resourceContext, writer);
await writer.WriteEndAsync();
if (nestedResourceInfo != null)
{
await writer.WriteStartAsync(nestedResourceInfo);
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(complexProperty.Key, null, resourceContext, writer);
await writer.WriteEndAsync();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,34 @@ public void WriteObjectInline_WritesODataEntryFrom_CreateResource()
writer.Verify();
}

[Fact]
public void WriteDeltaObjectInline_Calls_CreateDeltaNestedResourceInfo_ForEachSelectedComplexProperty()
{
// Arrange
SelectExpandNode selectExpandNode = new SelectExpandNode
{
SelectedComplexTypeProperties = new Dictionary<IEdmStructuralProperty, PathSelectItem>
{
{ new Mock<IEdmStructuralProperty>().Object, null },
{ new Mock<IEdmStructuralProperty>().Object, null }
}
};

Mock<ODataWriter> writer = new Mock<ODataWriter>();
Mock<ODataResourceSerializer> serializer = new Mock<ODataResourceSerializer>(_serializerProvider);
serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<ResourceContext>())).Returns(selectExpandNode);
serializer.CallBase = true;

serializer.Setup(s => s.CreateDeltaNestedResourceInfo(selectExpandNode.SelectedComplexTypeProperties.ElementAt(0).Key, It.IsAny<ResourceContext>())).Verifiable();
serializer.Setup(s => s.CreateDeltaNestedResourceInfo(selectExpandNode.SelectedComplexTypeProperties.ElementAt(1).Key, It.IsAny<ResourceContext>())).Verifiable();

// Act
serializer.Object.WriteDeltaObjectInline(_customer, _customerType, writer.Object, _writeContext);

// Assert
serializer.Verify();
}

[Fact]
public void WriteObjectInline_Calls_CreateComplexNestedResourceInfo_ForEachSelectedComplexProperty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3706,6 +3706,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
public virtual string CreateETag (ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3909,6 +3909,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
public virtual string CreateETag (ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4108,6 +4108,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
public virtual string CreateETag (ResourceContext resourceContext)
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)
Expand Down

0 comments on commit ee2203a

Please sign in to comment.