@@ -202,12 +202,13 @@ private void WriteDeltaResource(object graph, ODataWriter writer, ODataSerialize
202202 {
203203 writer . WriteStart ( resource ) ;
204204 WriteDeltaComplexProperties ( selectExpandNode , resourceContext , writer ) ;
205+ WriteDeltaNavigationProperties ( selectExpandNode , resourceContext , writer ) ;
205206 //TODO: Need to add support to write Navigation Links, etc. using Delta Writer
206207 //https://github.com/OData/odata.net/issues/155
207208 //CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
208209 //WriteDynamicComplexProperties(resourceContext, writer);
209210 //WriteNavigationLinks(selectExpandNode.SelectedNavigationProperties, resourceContext, writer);
210- //WriteExpandedNavigationProperties(selectExpandNode.ExpandedNavigationProperties , resourceContext, writer);
211+ //WriteExpandedNavigationProperties(selectExpandNode, resourceContext, writer);
211212
212213 writer . WriteEnd ( ) ;
213214 }
@@ -226,6 +227,7 @@ private async Task WriteDeltaResourceAsync(object graph, ODataWriter writer, ODa
226227 {
227228 await writer . WriteStartAsync ( resource ) ;
228229 await WriteDeltaComplexPropertiesAsync ( selectExpandNode , resourceContext , writer ) ;
230+ await WriteDeltaNavigationPropertiesAsync ( selectExpandNode , resourceContext , writer ) ;
229231 //TODO: Need to add support to write Navigation Links, etc. using Delta Writer
230232 //https://github.com/OData/odata.net/issues/155
231233 //CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
@@ -275,6 +277,48 @@ internal void WriteDeltaComplexProperties(SelectExpandNode selectExpandNode,
275277 }
276278 }
277279
280+ internal void WriteDeltaNavigationProperties ( SelectExpandNode selectExpandNode , ResourceContext resourceContext , ODataWriter writer )
281+ {
282+ Contract . Assert ( resourceContext != null ) ;
283+ Contract . Assert ( writer != null ) ;
284+
285+ IEnumerable < KeyValuePair < IEdmNavigationProperty , Type > > navigationProperties = GetNavigationPropertiesToWrite ( selectExpandNode , resourceContext ) ;
286+
287+ foreach ( KeyValuePair < IEdmNavigationProperty , Type > navigationProperty in navigationProperties )
288+ {
289+ ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
290+ {
291+ IsCollection = navigationProperty . Key . Type . IsCollection ( ) ,
292+ Name = navigationProperty . Key . Name
293+ } ;
294+
295+ writer . WriteStart ( nestedResourceInfo ) ;
296+ WriteDeltaComplexAndExpandedNavigationProperty ( navigationProperty . Key , null , resourceContext , writer , navigationProperty . Value ) ;
297+ writer . WriteEnd ( ) ;
298+ }
299+ }
300+
301+ internal async Task WriteDeltaNavigationPropertiesAsync ( SelectExpandNode selectExpandNode , ResourceContext resourceContext , ODataWriter writer )
302+ {
303+ Contract . Assert ( resourceContext != null ) ;
304+ Contract . Assert ( writer != null ) ;
305+
306+ IEnumerable < KeyValuePair < IEdmNavigationProperty , Type > > navigationProperties = GetNavigationPropertiesToWrite ( selectExpandNode , resourceContext ) ;
307+
308+ foreach ( KeyValuePair < IEdmNavigationProperty , Type > navigationProperty in navigationProperties )
309+ {
310+ ODataNestedResourceInfo nestedResourceInfo = new ODataNestedResourceInfo
311+ {
312+ IsCollection = navigationProperty . Key . Type . IsCollection ( ) ,
313+ Name = navigationProperty . Key . Name
314+ } ;
315+
316+ await writer . WriteStartAsync ( nestedResourceInfo ) ;
317+ await WriteDeltaComplexAndExpandedNavigationPropertyAsync ( navigationProperty . Key , null , resourceContext , writer , navigationProperty . Value ) ;
318+ await writer . WriteEndAsync ( ) ;
319+ }
320+ }
321+
278322 private async Task WriteDeltaComplexPropertiesAsync ( SelectExpandNode selectExpandNode ,
279323 ResourceContext resourceContext , ODataWriter writer )
280324 {
@@ -298,7 +342,7 @@ private async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpan
298342 }
299343
300344 private void WriteDeltaComplexAndExpandedNavigationProperty ( IEdmProperty edmProperty , SelectExpandClause selectExpandClause ,
301- ResourceContext resourceContext , ODataWriter writer )
345+ ResourceContext resourceContext , ODataWriter writer , Type type = null )
302346 {
303347 Contract . Assert ( edmProperty != null ) ;
304348 Contract . Assert ( resourceContext != null ) ;
@@ -331,6 +375,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
331375 {
332376 // create the serializer context for the complex and expanded item.
333377 ODataSerializerContext nestedWriteContext = new ODataSerializerContext ( resourceContext , selectExpandClause , edmProperty ) ;
378+ nestedWriteContext . Type = type ;
334379
335380 // write object.
336381
@@ -355,7 +400,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
355400 }
356401
357402 private async Task WriteDeltaComplexAndExpandedNavigationPropertyAsync ( IEdmProperty edmProperty , SelectExpandClause selectExpandClause ,
358- ResourceContext resourceContext , ODataWriter writer )
403+ ResourceContext resourceContext , ODataWriter writer , Type type = null )
359404 {
360405 Contract . Assert ( edmProperty != null ) ;
361406 Contract . Assert ( resourceContext != null ) ;
@@ -388,6 +433,7 @@ await writer.WriteStartAsync(new ODataResourceSet
388433 {
389434 // create the serializer context for the complex and expanded item.
390435 ODataSerializerContext nestedWriteContext = new ODataSerializerContext ( resourceContext , selectExpandClause , edmProperty ) ;
436+ nestedWriteContext . Type = type ;
391437
392438 // write object.
393439
@@ -1351,6 +1397,43 @@ private IEnumerable<KeyValuePair<IEdmStructuralProperty, PathSelectItem>> GetPro
13511397 }
13521398 }
13531399
1400+ private IEnumerable < KeyValuePair < IEdmNavigationProperty , Type > > GetNavigationPropertiesToWrite ( SelectExpandNode selectExpandNode , ResourceContext resourceContext )
1401+ {
1402+ ISet < IEdmNavigationProperty > navigationProperties = selectExpandNode . SelectedNavigationProperties ;
1403+
1404+ if ( navigationProperties != null )
1405+ {
1406+ IEnumerable < string > changedProperties = null ;
1407+
1408+ if ( null != resourceContext . ResourceInstance && resourceContext . ResourceInstance is IDelta deltaObject )
1409+ {
1410+ changedProperties = deltaObject . GetChangedPropertyNames ( ) ;
1411+ dynamic delta = deltaObject ;
1412+
1413+ foreach ( IEdmNavigationProperty navigationProperty in navigationProperties )
1414+ {
1415+ Object obj = null ;
1416+ if ( changedProperties == null || changedProperties . Contains ( navigationProperty . Name ) && delta . DeltaNestedResources . TryGetValue ( navigationProperty . Name , out obj ) )
1417+ {
1418+ yield return new KeyValuePair < IEdmNavigationProperty , Type > ( navigationProperty , obj . GetType ( ) ) ;
1419+ }
1420+ }
1421+ }
1422+ else if ( null != resourceContext . EdmObject && resourceContext . EdmObject is IDelta changedObject )
1423+ {
1424+ changedProperties = changedObject . GetChangedPropertyNames ( ) ;
1425+
1426+ foreach ( IEdmNavigationProperty navigationProperty in navigationProperties )
1427+ {
1428+ if ( changedProperties == null || changedProperties . Contains ( navigationProperty . Name ) )
1429+ {
1430+ yield return new KeyValuePair < IEdmNavigationProperty , Type > ( navigationProperty , typeof ( IEdmChangedObject ) ) ;
1431+ }
1432+ }
1433+ }
1434+ }
1435+ }
1436+
13541437 private void WriteExpandedNavigationProperties ( SelectExpandNode selectExpandNode , ResourceContext resourceContext , ODataWriter writer )
13551438 {
13561439 Contract . Assert ( resourceContext != null ) ;
0 commit comments