@@ -202,12 +202,13 @@ private void WriteDeltaResource(object graph, ODataWriter writer, ODataSerialize
202
202
{
203
203
writer . WriteStart ( resource ) ;
204
204
WriteDeltaComplexProperties ( selectExpandNode , resourceContext , writer ) ;
205
+ WriteDeltaNavigationProperties ( selectExpandNode , resourceContext , writer ) ;
205
206
//TODO: Need to add support to write Navigation Links, etc. using Delta Writer
206
207
//https://github.com/OData/odata.net/issues/155
207
208
//CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
208
209
//WriteDynamicComplexProperties(resourceContext, writer);
209
210
//WriteNavigationLinks(selectExpandNode.SelectedNavigationProperties, resourceContext, writer);
210
- //WriteExpandedNavigationProperties(selectExpandNode.ExpandedNavigationProperties , resourceContext, writer);
211
+ //WriteExpandedNavigationProperties(selectExpandNode, resourceContext, writer);
211
212
212
213
writer . WriteEnd ( ) ;
213
214
}
@@ -226,6 +227,7 @@ private async Task WriteDeltaResourceAsync(object graph, ODataWriter writer, ODa
226
227
{
227
228
await writer . WriteStartAsync ( resource ) ;
228
229
await WriteDeltaComplexPropertiesAsync ( selectExpandNode , resourceContext , writer ) ;
230
+ await WriteDeltaNavigationPropertiesAsync ( selectExpandNode , resourceContext , writer ) ;
229
231
//TODO: Need to add support to write Navigation Links, etc. using Delta Writer
230
232
//https://github.com/OData/odata.net/issues/155
231
233
//CLEANUP: merge delta logic with regular logic; requires common base between ODataWriter and ODataDeltaWriter
@@ -275,6 +277,48 @@ internal void WriteDeltaComplexProperties(SelectExpandNode selectExpandNode,
275
277
}
276
278
}
277
279
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
+
278
322
private async Task WriteDeltaComplexPropertiesAsync ( SelectExpandNode selectExpandNode ,
279
323
ResourceContext resourceContext , ODataWriter writer )
280
324
{
@@ -298,7 +342,7 @@ private async Task WriteDeltaComplexPropertiesAsync(SelectExpandNode selectExpan
298
342
}
299
343
300
344
private void WriteDeltaComplexAndExpandedNavigationProperty ( IEdmProperty edmProperty , SelectExpandClause selectExpandClause ,
301
- ResourceContext resourceContext , ODataWriter writer )
345
+ ResourceContext resourceContext , ODataWriter writer , Type type = null )
302
346
{
303
347
Contract . Assert ( edmProperty != null ) ;
304
348
Contract . Assert ( resourceContext != null ) ;
@@ -331,6 +375,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
331
375
{
332
376
// create the serializer context for the complex and expanded item.
333
377
ODataSerializerContext nestedWriteContext = new ODataSerializerContext ( resourceContext , selectExpandClause , edmProperty ) ;
378
+ nestedWriteContext . Type = type ;
334
379
335
380
// write object.
336
381
@@ -355,7 +400,7 @@ private void WriteDeltaComplexAndExpandedNavigationProperty(IEdmProperty edmProp
355
400
}
356
401
357
402
private async Task WriteDeltaComplexAndExpandedNavigationPropertyAsync ( IEdmProperty edmProperty , SelectExpandClause selectExpandClause ,
358
- ResourceContext resourceContext , ODataWriter writer )
403
+ ResourceContext resourceContext , ODataWriter writer , Type type = null )
359
404
{
360
405
Contract . Assert ( edmProperty != null ) ;
361
406
Contract . Assert ( resourceContext != null ) ;
@@ -388,6 +433,7 @@ await writer.WriteStartAsync(new ODataResourceSet
388
433
{
389
434
// create the serializer context for the complex and expanded item.
390
435
ODataSerializerContext nestedWriteContext = new ODataSerializerContext ( resourceContext , selectExpandClause , edmProperty ) ;
436
+ nestedWriteContext . Type = type ;
391
437
392
438
// write object.
393
439
@@ -1351,6 +1397,43 @@ private IEnumerable<KeyValuePair<IEdmStructuralProperty, PathSelectItem>> GetPro
1351
1397
}
1352
1398
}
1353
1399
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
+
1354
1437
private void WriteExpandedNavigationProperties ( SelectExpandNode selectExpandNode , ResourceContext resourceContext , ODataWriter writer )
1355
1438
{
1356
1439
Contract . Assert ( resourceContext != null ) ;
0 commit comments