Skip to content

Commit 7119d78

Browse files
committed
Cleanup ODataIdResolvers and E2E tests (#2693)
1 parent ee5d4d1 commit 7119d78

File tree

17 files changed

+956
-769
lines changed

17 files changed

+956
-769
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//-----------------------------------------------------------------------------
2+
// <copyright file="ODataPathHelper.cs" company=".NET Foundation">
3+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
4+
// See License.txt in the project root for license information.
5+
// </copyright>
6+
//------------------------------------------------------------------------------
7+
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using Microsoft.OData.UriParser;
11+
12+
namespace Microsoft.AspNet.OData.Common
13+
{
14+
/// <summary>
15+
/// Helper methods for <see cref="ODataPath"/>.
16+
/// </summary>
17+
internal static class ODataPathHelper
18+
{
19+
/// <summary>
20+
/// Get the keys from a <see cref="KeySegment"/>.
21+
/// </summary>
22+
/// <param name="keySegment">The <see cref="KeySegment"/> to extract the keys.</param>
23+
/// <returns>Dictionary of keys.</returns>
24+
public static Dictionary<string, object> KeySegmentAsDictionary(KeySegment keySegment)
25+
{
26+
if (keySegment == null)
27+
{
28+
throw Error.ArgumentNull(nameof(keySegment));
29+
}
30+
31+
return keySegment.Keys.ToDictionary(d => d.Key, d => d.Value);
32+
}
33+
34+
/// <summary>
35+
/// Get the position of the next <see cref="KeySegment"/> in a list of <see cref="ODataPathSegment"/>.
36+
/// </summary>
37+
/// <param name="pathSegments">List of <see cref="ODataPathSegment"/>.</param>
38+
/// <param name="currentPosition">Current position in the list of <see cref="ODataPathSegment"/>.</param>
39+
/// <returns>Position of the next <see cref="KeySegment"/> if it exists, or -1 otherwise.</returns>
40+
public static int GetNextKeySegmentPosition(IReadOnlyList<ODataPathSegment> pathSegments, int currentPosition)
41+
{
42+
if (pathSegments == null)
43+
{
44+
throw Error.ArgumentNull(nameof(pathSegments));
45+
}
46+
47+
if (currentPosition < 0 || currentPosition >= pathSegments.Count)
48+
{
49+
return -1;
50+
}
51+
52+
if (pathSegments[currentPosition] is KeySegment)
53+
{
54+
currentPosition++;
55+
}
56+
57+
for (int i = currentPosition; i < pathSegments.Count; i++)
58+
{
59+
ODataPathSegment currentSegment = pathSegments[i];
60+
61+
if (currentSegment is KeySegment)
62+
{
63+
return i;
64+
}
65+
}
66+
67+
return -1;
68+
}
69+
}
70+
}

src/Microsoft.AspNet.OData.Shared/Extensions/ODataPathExtensions.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,84 @@
66
//------------------------------------------------------------------------------
77

88
using System.Collections.Generic;
9+
using System.Linq;
910
using Microsoft.AspNet.OData.Common;
1011
using Microsoft.OData.UriParser;
1112

1213
namespace Microsoft.AspNet.OData.Extensions
1314
{
15+
/// <summary>
16+
/// Extensions method for <see cref="ODataPath"/>.
17+
/// </summary>
1418
internal static class ODataPathExtensions
1519
{
16-
public static Dictionary<string, object> GetKeys(this ODataPath path)
20+
/// <summary>
21+
/// Get keys from the last <see cref="KeySegment"/>.
22+
/// </summary>
23+
/// <param name="path"><see cref="ODataPath"/>.</param>
24+
/// <returns>Dictionary of keys.</returns>
25+
internal static Dictionary<string, object> GetKeys(this ODataPath path)
1726
{
27+
Dictionary<string, object> keys = new Dictionary<string, object>();
28+
1829
if (path == null)
1930
{
2031
throw Error.ArgumentNull(nameof(path));
2132
}
2233

23-
Dictionary<string, object> keys = new Dictionary<string, object>();
24-
25-
// Books(1)/Authors(1000)/Namespace.SpecialAuthor
26-
if (path.LastSegment is TypeSegment)
34+
if (path.Count == 0)
2735
{
28-
ODataPath pathWithoutLastSegmentCastType = path.TrimEndingTypeSegment();
29-
30-
if (pathWithoutLastSegmentCastType.LastSegment is KeySegment)
31-
{
32-
keys = GetKeysFromKeySegment(pathWithoutLastSegmentCastType.LastSegment as KeySegment);
33-
}
36+
return keys;
3437
}
35-
// Books(1)/Authors/Namespace.SpecialAuthor/(1000)
36-
else if (path.LastSegment is KeySegment)
38+
39+
List<ODataPathSegment> pathSegments = path.AsList();
40+
41+
KeySegment keySegment = pathSegments.OfType<KeySegment>().LastOrDefault();
42+
43+
if (keySegment == null)
3744
{
38-
keys = GetKeysFromKeySegment(path.LastSegment as KeySegment);
45+
return keys;
3946
}
4047

48+
keys = ODataPathHelper.KeySegmentAsDictionary(keySegment);
49+
4150
return keys;
4251
}
4352

44-
private static Dictionary<string, object> GetKeysFromKeySegment(KeySegment keySegment)
53+
/// <summary>
54+
/// Return the last segment in the path, which is not a <see cref="TypeSegment"/> or <see cref="KeySegment"/>.
55+
/// </summary>
56+
/// <param name="path">The <see cref="ODataPath"/>.</param>
57+
/// <returns>An <see cref="ODataPathSegment"/>.</returns>
58+
public static ODataPathSegment GetLastNonTypeNonKeySegment(this ODataPath path)
4559
{
46-
Dictionary<string, object> keys = new Dictionary<string, object>();
60+
if (path == null)
61+
{
62+
throw Error.ArgumentNull(nameof(path));
63+
}
64+
65+
// If the path is Employees(2)/NewFriends(2)/Namespace.MyNewFriend where Namespace.MyNewFriend is a type segment,
66+
// This method will return NewFriends NavigationPropertySegment.
4767

48-
foreach (KeyValuePair<string, object> kvp in keySegment.Keys)
68+
List<ODataPathSegment> pathSegments = path.AsList();
69+
int position = path.Count - 1;
70+
71+
while (position >= 0 && (pathSegments[position] is TypeSegment || pathSegments[position] is KeySegment))
4972
{
50-
keys.Add(kvp.Key, kvp.Value);
73+
--position;
5174
}
5275

53-
return keys;
76+
return position < 0 ? null : pathSegments[position];
77+
}
78+
79+
/// <summary>
80+
/// Returns a list of <see cref="ODataPathSegment"/> in an <see cref="ODataPath"/>.
81+
/// </summary>
82+
/// <param name="path">The <see cref="ODataPath"/>.</param>
83+
/// <returns>List of <see cref="ODataPathSegment"/>.</returns>
84+
public static List<ODataPathSegment> GetSegments(this ODataPath path)
85+
{
86+
return path.AsList();
5487
}
5588
}
5689
}

src/Microsoft.AspNet.OData.Shared/Microsoft.AspNet.OData.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Compile Include="$(MSBuildThisFileDirectory)ClrEnumMemberAnnotation.cs" />
2626
<Compile Include="$(MSBuildThisFileDirectory)Common\CollectionExtensions.cs" />
2727
<Compile Include="$(MSBuildThisFileDirectory)Common\ListWrapperCollection.cs" />
28+
<Compile Include="$(MSBuildThisFileDirectory)Common\ODataPathHelper.cs" />
2829
<Compile Include="$(MSBuildThisFileDirectory)Common\PropertyHelper.cs" />
2930
<Compile Include="$(MSBuildThisFileDirectory)Common\TaskHelpers.cs" />
3031
<Compile Include="$(MSBuildThisFileDirectory)Common\Error.cs" />

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNetCore/Microsoft.Test.E2E.AspNetCore.OData.csproj

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,30 +1402,21 @@
14021402
<Compile Include="..\OpenType\TypedTest.cs">
14031403
<Link>OpenType\TypedTest.cs</Link>
14041404
</Compile>
1405-
<Compile Include="..\BulkOperation\BulkInsertDataModel.cs">
1406-
<Link>BulkOperation\BulkInsertDataModel.cs</Link>
1405+
<Compile Include="..\BulkOperation\BulkOperationTest.cs">
1406+
<Link>BulkOperation\BulkOperationTest.cs</Link>
14071407
</Compile>
1408-
<Compile Include="..\BulkOperation\BulkInsertEdmModel.cs">
1409-
<Link>BulkOperation\BulkInsertEdmModel.cs</Link>
1408+
<Compile Include="..\BulkOperation\BulkOperationDataModel.cs">
1409+
<Link>BulkOperation\BulkOperationDataModel.cs</Link>
14101410
</Compile>
1411-
<Compile Include="..\BulkOperation\BulkInsertController.cs">
1412-
<Link>BulkOperation\BulkInsertController.cs</Link>
1411+
<Compile Include="..\BulkOperation\BulkOperationEdmModel.cs">
1412+
<Link>BulkOperation\BulkOperationEdmModel.cs</Link>
1413+
</Compile>
1414+
<Compile Include="..\BulkOperation\BulkOperationController.cs">
1415+
<Link>BulkOperation\BulkOperationController.cs</Link>
14131416
</Compile>
14141417
<Compile Include="..\BulkOperation\BulkOperationPatchHandlers.cs">
14151418
<Link>BulkOperation\BulkOperationPatchHandlers.cs</Link>
14161419
</Compile>
1417-
<Compile Include="..\BulkOperation\BulkInsertDataModel.cs">
1418-
<Link>BulkOperation\BulkInsertDataModel.cs</Link>
1419-
</Compile>
1420-
<Compile Include="..\BulkOperation\BulkInsertEdmModel.cs">
1421-
<Link>BulkOperation\BulkInsertEdmModel.cs</Link>
1422-
</Compile>
1423-
<Compile Include="..\BulkOperation\BulkInsertController.cs">
1424-
<Link>BulkOperation\BulkInsertController.cs</Link>
1425-
</Compile>
1426-
<Compile Include="..\BulkOperation\BulkOperationPatchHandlers.cs">
1427-
<Link>BulkOperation\BulkOperationPatchHandlers.cs</Link>
1428-
</Compile>
14291420
<Compile Include="..\ParameterAlias\ParameterAliasDataSource.cs">
14301421
<Link>ParameterAlias\ParameterAliasDataSource.cs</Link>
14311422
</Compile>

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNetCore3x/BulkOperation/EFTests/BulkOperationControllerEF.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@
1313
using Microsoft.AspNet.OData.Extensions;
1414
using Microsoft.AspNet.OData.Routing;
1515
using Microsoft.EntityFrameworkCore;
16-
using Microsoft.Test.E2E.AspNet.OData.BulkOperation;
1716
using Microsoft.Test.E2E.AspNet.OData.Common.Controllers;
1817
using Xunit;
1918
using static Microsoft.Test.E2E.AspNet.OData.BulkOperation.APIHandlerFactoryEF;
2019

21-
namespace Microsoft.Test.E2E.AspNet.OData.BulkInsert
20+
namespace Microsoft.Test.E2E.AspNet.OData.BulkOperation
2221
{
2322
public class EmployeesControllerEF : TestODataController
2423
{
2524
public EmployeesControllerEF()
2625
{
27-
26+
2827
}
29-
28+
3029
public static List<Employee> employees;
3130
public static List<Friend> friends;
3231

3332
internal DbSet<Employee> GenerateData(EmployeeDBContext context)
34-
{
33+
{
3534
if (context.Employees.Any())
3635
{
3736
return context.Employees;
@@ -59,7 +58,7 @@ internal DbSet<Friend> GenerateDataOrders(EmployeeDBContext context)
5958
}
6059

6160
friends = new List<Friend>();
62-
friends.Add(new Friend { Id = 1, Age = 10 , Orders = new List<Order>() { new Order { Id = 1, Price = 5 }, new Order { Id = 2, Price = 5 } } });
61+
friends.Add(new Friend { Id = 1, Age = 10, Orders = new List<Order>() { new Order { Id = 1, Price = 5 }, new Order { Id = 2, Price = 5 } } });
6362
friends.Add(new Friend { Id = 2, Age = 20, Orders = new List<Order>() { new Order { Id = 10, Price = 5 }, new Order { Id = 20, Price = 5 } } });
6463
friends.Add(new Friend { Id = 3, Age = 30, Orders = new List<Order>() { new Order { Id = 3, Price = 5 }, new Order { Id = 4, Price = 5 } } });
6564
friends.Add(new Friend { Id = 4, Age = 40, Orders = new List<Order>() { new Order { Id = 30, Price = 5 }, new Order { Id = 40, Price = 5 } } });

0 commit comments

Comments
 (0)