Skip to content

Commit ee2203a

Browse files
authored
Fixes #2858: Add Virtual method for creating Delta response (#2859)
* Fixes #2858: Add Virtual method for creating Delta response * Update the publicApi
1 parent 651c83d commit ee2203a

5 files changed

Lines changed: 82 additions & 32 deletions

File tree

src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataResourceSerializer.cs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,29 @@ public virtual string CreateETag(ResourceContext resourceContext)
461461
return null;
462462
}
463463

464+
/// <summary>
465+
/// Creates the <see cref="ODataNestedResourceInfo"/> to be written while writing this delta nested property (complex or entity).
466+
/// </summary>
467+
/// <param name="property">The property for which the nested resource info is being created.</param>
468+
/// <param name="resourceContext">The context for the property instance being written.</param>
469+
/// <returns>The nested resource info to be written. Returns 'null' will omit this property serialization.</returns>
470+
/// <remarks>It enables customer to get more control by overriding this method. </remarks>
471+
public virtual ODataNestedResourceInfo CreateDeltaNestedResourceInfo(IEdmProperty property, ResourceContext resourceContext)
472+
{
473+
if (property == null)
474+
{
475+
throw Error.ArgumentNull(nameof(property));
476+
}
477+
478+
return property.Type != null ?
479+
new ODataNestedResourceInfo
480+
{
481+
IsCollection = property.Type.IsCollection(),
482+
Name = property.Name
483+
} :
484+
null;
485+
}
486+
464487
/// <summary>
465488
/// Creates the <see cref="ODataNestedResourceInfo"/> to be written while writing this dynamic complex property.
466489
/// </summary>
@@ -705,15 +728,14 @@ internal void WriteDeltaComplexProperties(SelectExpandNode selectExpandNode,
705728

706729
foreach (KeyValuePair<IEdmStructuralProperty, PathSelectItem> complexProperty in complexProperties)
707730
{
708-
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
709-
{
710-
IsCollection = complexProperty.Key.Type.IsCollection(),
711-
Name = complexProperty.Key.Name
712-
};
731+
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(complexProperty.Key, resourceContext);
713732

714-
writer.WriteStart(nestedResourceInfo);
715-
WriteDeltaComplexAndExpandedNavigationProperty(complexProperty.Key, null, resourceContext, writer);
716-
writer.WriteEnd();
733+
if (nestedResourceInfo != null)
734+
{
735+
writer.WriteStart(nestedResourceInfo);
736+
WriteDeltaComplexAndExpandedNavigationProperty(complexProperty.Key, null, resourceContext, writer);
737+
writer.WriteEnd();
738+
}
717739
}
718740
}
719741

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

733755
foreach (KeyValuePair<IEdmNavigationProperty, Type> navigationProperty in navigationProperties)
734756
{
735-
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
736-
{
737-
IsCollection = navigationProperty.Key.Type.IsCollection(),
738-
Name = navigationProperty.Key.Name
739-
};
757+
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(navigationProperty.Key, resourceContext);
740758

741-
writer.WriteStart(nestedResourceInfo);
742-
WriteDeltaComplexAndExpandedNavigationProperty(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
743-
writer.WriteEnd();
759+
if (nestedResourceInfo != null)
760+
{
761+
writer.WriteStart(nestedResourceInfo);
762+
WriteDeltaComplexAndExpandedNavigationProperty(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
763+
writer.WriteEnd();
764+
}
744765
}
745766
}
746767

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

761782
foreach (KeyValuePair<IEdmNavigationProperty, Type> navigationProperty in navigationProperties)
762783
{
763-
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
764-
{
765-
IsCollection = navigationProperty.Key.Type.IsCollection(),
766-
Name = navigationProperty.Key.Name
767-
};
784+
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(navigationProperty.Key, resourceContext);
768785

769-
await writer.WriteStartAsync(nestedResourceInfo);
770-
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
771-
await writer.WriteEndAsync();
786+
if (nestedResourceInfo != null)
787+
{
788+
await writer.WriteStartAsync(nestedResourceInfo);
789+
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(navigationProperty.Key, null, resourceContext, writer, navigationProperty.Value);
790+
await writer.WriteEndAsync();
791+
}
772792
}
773793
}
774794

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

794814
foreach (KeyValuePair<IEdmStructuralProperty, PathSelectItem> complexProperty in complexProperties)
795815
{
796-
ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
797-
{
798-
IsCollection = complexProperty.Key.Type.IsCollection(),
799-
Name = complexProperty.Key.Name
800-
};
816+
ODataNestedResourceInfo nestedResourceInfo = CreateDeltaNestedResourceInfo(complexProperty.Key, resourceContext);
801817

802-
await writer.WriteStartAsync(nestedResourceInfo);
803-
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(complexProperty.Key, null, resourceContext, writer);
804-
await writer.WriteEndAsync();
818+
if (nestedResourceInfo != null)
819+
{
820+
await writer.WriteStartAsync(nestedResourceInfo);
821+
await WriteDeltaComplexAndExpandedNavigationPropertyAsync(complexProperty.Key, null, resourceContext, writer);
822+
await writer.WriteEndAsync();
823+
}
805824
}
806825
}
807826

test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Formatter/Serialization/ODataResourceSerializerTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ public void WriteObjectInline_WritesODataEntryFrom_CreateResource()
212212
writer.Verify();
213213
}
214214

215+
[Fact]
216+
public void WriteDeltaObjectInline_Calls_CreateDeltaNestedResourceInfo_ForEachSelectedComplexProperty()
217+
{
218+
// Arrange
219+
SelectExpandNode selectExpandNode = new SelectExpandNode
220+
{
221+
SelectedComplexTypeProperties = new Dictionary<IEdmStructuralProperty, PathSelectItem>
222+
{
223+
{ new Mock<IEdmStructuralProperty>().Object, null },
224+
{ new Mock<IEdmStructuralProperty>().Object, null }
225+
}
226+
};
227+
228+
Mock<ODataWriter> writer = new Mock<ODataWriter>();
229+
Mock<ODataResourceSerializer> serializer = new Mock<ODataResourceSerializer>(_serializerProvider);
230+
serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<ResourceContext>())).Returns(selectExpandNode);
231+
serializer.CallBase = true;
232+
233+
serializer.Setup(s => s.CreateDeltaNestedResourceInfo(selectExpandNode.SelectedComplexTypeProperties.ElementAt(0).Key, It.IsAny<ResourceContext>())).Verifiable();
234+
serializer.Setup(s => s.CreateDeltaNestedResourceInfo(selectExpandNode.SelectedComplexTypeProperties.ElementAt(1).Key, It.IsAny<ResourceContext>())).Verifiable();
235+
236+
// Act
237+
serializer.Object.WriteDeltaObjectInline(_customer, _customerType, writer.Object, _writeContext);
238+
239+
// Assert
240+
serializer.Verify();
241+
}
242+
215243
[Fact]
216244
public void WriteObjectInline_Calls_CreateComplexNestedResourceInfo_ForEachSelectedComplexProperty()
217245
{

test/UnitTest/Microsoft.AspNet.OData.Test/PublicApi/Microsoft.AspNet.OData.PublicApi.bsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,6 +3706,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
37063706
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
37073707
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
37083708
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
3709+
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
37093710
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
37103711
public virtual string CreateETag (ResourceContext resourceContext)
37113712
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)

test/UnitTest/Microsoft.AspNetCore.OData.Test/PublicApi/Microsoft.AspNetCore.OData.PublicApi.bsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3909,6 +3909,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
39093909
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
39103910
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
39113911
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
3912+
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
39123913
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
39133914
public virtual string CreateETag (ResourceContext resourceContext)
39143915
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)

test/UnitTest/Microsoft.AspNetCore.OData.Test/PublicApi/Microsoft.AspNetCore3x.OData.PublicApi.bsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,6 +4108,7 @@ public class Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSeriali
41084108
public virtual void AppendInstanceAnnotations (Microsoft.OData.ODataResourceBase resource, ResourceContext resourceContext)
41094109
public virtual Microsoft.OData.ODataNestedResourceInfo CreateComplexNestedResourceInfo (Microsoft.OData.Edm.IEdmStructuralProperty complexProperty, Microsoft.OData.UriParser.PathSelectItem pathSelectItem, ResourceContext resourceContext)
41104110
public virtual Microsoft.OData.ODataDeletedResource CreateDeletedResource (SelectExpandNode selectExpandNode, ResourceContext resourceContext)
4111+
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDeltaNestedResourceInfo (Microsoft.OData.Edm.IEdmProperty property, ResourceContext resourceContext)
41114112
public virtual Microsoft.OData.ODataNestedResourceInfo CreateDynamicComplexNestedResourceInfo (string propertyName, object propertyValue, Microsoft.OData.Edm.IEdmTypeReference edmType, ResourceContext resourceContext)
41124113
public virtual string CreateETag (ResourceContext resourceContext)
41134114
public virtual Microsoft.OData.ODataNestedResourceInfo CreateNavigationLink (Microsoft.OData.Edm.IEdmNavigationProperty navigationProperty, ResourceContext resourceContext)

0 commit comments

Comments
 (0)