Skip to content

Commit 50cdd4c

Browse files
committed
updates
1 parent 82918ec commit 50cdd4c

File tree

5 files changed

+174
-18
lines changed

5 files changed

+174
-18
lines changed

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

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ internal bool IsUntyped
172172
get
173173
{ if (_isUntyped == null)
174174
{
175-
_isUntyped = typeof(IEdmObject).IsAssignableFrom(Type);
175+
_isUntyped = typeof(IEdmObject).IsAssignableFrom(Type) || typeof(EdmChangedObjectCollection).IsAssignableFrom(Type);
176176
}
177177

178178
return _isUntyped.Value;

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/BulkOperation/BulkInsertController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public ITestActionResult Get()
198198
return Ok(Employees.AsQueryable());
199199
}
200200

201+
[EnableQuery]
201202
public ITestActionResult Get(int key)
202203
{
203204
var emp = Employees.SingleOrDefault(e => e.ID == key);
@@ -235,6 +236,7 @@ public ITestActionResult GetUnTypedFriends(int key)
235236

236237
[ODataRoute("Employees")]
237238
[HttpPatch]
239+
[EnableQuery]
238240
public ITestActionResult PatchEmployees([FromBody] DeltaSet<Employee> coll)
239241
{
240242
InitEmployees();
@@ -358,6 +360,7 @@ public ITestActionResult PatchUnTypedEmployees([FromBody] EdmChangedObjectCollec
358360

359361

360362
[ODataRoute("Employees({key})")]
363+
[EnableQuery]
361364
public ITestActionResult Patch(int key, [FromBody] Delta<Employee> delta)
362365
{
363366
InitEmployees();

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/BulkOperation/BulkInsertDataModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Microsoft.Test.E2E.AspNet.OData.BulkInsert
1212
{
13+
[AutoExpand]
1314
public class Employee
1415
{
1516
[Key]
@@ -19,7 +20,6 @@ public class Employee
1920
public Gender Gender { get; set; }
2021
public AccessLevel AccessLevel { get; set; }
2122

22-
[AutoExpand]
2323
public List<Friend> Friends { get; set; }
2424

2525
public List<NewFriend> NewFriends { get; set; }
@@ -67,9 +67,9 @@ public class Friend
6767
{
6868
[Key]
6969
public int Id { get; set; }
70-
[Required]
70+
7171
public string Name { get; set; }
72-
[Range(10, 20)]
72+
7373
public int Age { get; set; }
7474

7575
public List<Order> Orders { get; set; }

0 commit comments

Comments
 (0)