-
Notifications
You must be signed in to change notification settings - Fork 473
Cleanup ODataIdResolvers and E2E tests #2693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ff209c8
514a74b
be39217
726a175
dd89ba1
8e672a7
c49272d
8a25402
52802f0
7a7b99c
c1142d1
cf5a3e0
9ecd145
2e38d32
092800b
d697dd9
5794f56
eeffcdc
fd0f381
77c1465
f6348b8
a57c245
7d3776b
a0b01c6
63dfd6b
eb85195
777fbb2
a513554
9e1759c
ed795b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //----------------------------------------------------------------------------- | ||
| // <copyright file="ODataPathHelper.cs" company=".NET Foundation"> | ||
| // Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
| // See License.txt in the project root for license information. | ||
| // </copyright> | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OData.UriParser; | ||
|
|
||
| namespace Microsoft.AspNet.OData.Common | ||
| { | ||
| /// <summary> | ||
| /// Helper methods for <see cref="ODataPath"/>. | ||
| /// </summary> | ||
| internal static class ODataPathHelper | ||
| { | ||
| /// <summary> | ||
| /// Get the keys from a <see cref="KeySegment"/>. | ||
KenitoInc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="keySegment">The <see cref="KeySegment"/> to extract the keys.</param> | ||
| /// <returns>Dictionary of keys.</returns> | ||
| public static Dictionary<string, object> KeySegmentAsDictionary(KeySegment keySegment) | ||
| { | ||
| if (keySegment == null) | ||
| { | ||
| throw Error.ArgumentNull(nameof(keySegment)); | ||
| } | ||
|
|
||
| return keySegment.Keys.ToDictionary(d => d.Key, d => d.Value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the position of the next <see cref="KeySegment"/> in a list of <see cref="ODataPathSegment"/>. | ||
| /// </summary> | ||
| /// <param name="pathSegments">List of <see cref="ODataPathSegment"/>.</param> | ||
| /// <param name="currentPosition">Current position in the list of <see cref="ODataPathSegment"/>.</param> | ||
| /// <returns>Position of the next <see cref="KeySegment"/> if it exists, or -1 otherwise.</returns> | ||
| public static int GetNextKeySegmentPosition(IReadOnlyList<ODataPathSegment> pathSegments, int currentPosition) | ||
| { | ||
| if (pathSegments == null) | ||
| { | ||
| throw Error.ArgumentNull(nameof(pathSegments)); | ||
| } | ||
|
|
||
| if (currentPosition < 0 || currentPosition >= pathSegments.Count) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| if (pathSegments[currentPosition] is KeySegment) | ||
KenitoInc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| currentPosition++; | ||
| } | ||
|
|
||
| for (int i = currentPosition; i < pathSegments.Count; i++) | ||
KenitoInc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ODataPathSegment currentSegment = pathSegments[i]; | ||
|
|
||
| if (currentSegment is KeySegment) | ||
| { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,51 +6,84 @@ | |
| //------------------------------------------------------------------------------ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.AspNet.OData.Common; | ||
| using Microsoft.OData.UriParser; | ||
|
|
||
| namespace Microsoft.AspNet.OData.Extensions | ||
| { | ||
| /// <summary> | ||
| /// Extensions method for <see cref="ODataPath"/>. | ||
| /// </summary> | ||
| internal static class ODataPathExtensions | ||
| { | ||
| public static Dictionary<string, object> GetKeys(this ODataPath path) | ||
| /// <summary> | ||
| /// Get keys from the last <see cref="KeySegment"/>. | ||
| /// </summary> | ||
| /// <param name="path"><see cref="ODataPath"/>.</param> | ||
| /// <returns>Dictionary of keys.</returns> | ||
| internal static Dictionary<string, object> GetKeys(this ODataPath path) | ||
| { | ||
| Dictionary<string, object> keys = new Dictionary<string, object>(); | ||
|
|
||
| if (path == null) | ||
| { | ||
| throw Error.ArgumentNull(nameof(path)); | ||
| } | ||
|
|
||
| Dictionary<string, object> keys = new Dictionary<string, object>(); | ||
|
|
||
| // Books(1)/Authors(1000)/Namespace.SpecialAuthor | ||
| if (path.LastSegment is TypeSegment) | ||
| if (path.Count == 0) | ||
| { | ||
| ODataPath pathWithoutLastSegmentCastType = path.TrimEndingTypeSegment(); | ||
|
|
||
| if (pathWithoutLastSegmentCastType.LastSegment is KeySegment) | ||
| { | ||
| keys = GetKeysFromKeySegment(pathWithoutLastSegmentCastType.LastSegment as KeySegment); | ||
| } | ||
| return keys; | ||
| } | ||
| // Books(1)/Authors/Namespace.SpecialAuthor/(1000) | ||
| else if (path.LastSegment is KeySegment) | ||
|
|
||
| List<ODataPathSegment> pathSegments = path.AsList(); | ||
KenitoInc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| KeySegment keySegment = pathSegments.OfType<KeySegment>().LastOrDefault(); | ||
|
|
||
| if (keySegment == null) | ||
| { | ||
| keys = GetKeysFromKeySegment(path.LastSegment as KeySegment); | ||
| return keys; | ||
| } | ||
|
|
||
| keys = ODataPathHelper.KeySegmentAsDictionary(keySegment); | ||
|
|
||
| return keys; | ||
| } | ||
|
|
||
| private static Dictionary<string, object> GetKeysFromKeySegment(KeySegment keySegment) | ||
| /// <summary> | ||
| /// Return the last segment in the path, which is not a <see cref="TypeSegment"/> or <see cref="KeySegment"/>. | ||
| /// </summary> | ||
| /// <param name="path">The <see cref="ODataPath"/>.</param> | ||
| /// <returns>An <see cref="ODataPathSegment"/>.</returns> | ||
| public static ODataPathSegment GetLastNonTypeNonKeySegment(this ODataPath path) | ||
KenitoInc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Dictionary<string, object> keys = new Dictionary<string, object>(); | ||
| if (path == null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check seems unnecessary in an extension method
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a standard process for |
||
| { | ||
| throw Error.ArgumentNull(nameof(path)); | ||
| } | ||
|
|
||
| // If the path is Employees(2)/NewFriends(2)/Namespace.MyNewFriend where Namespace.MyNewFriend is a type segment, | ||
| // This method will return NewFriends NavigationPropertySegment. | ||
|
|
||
| foreach (KeyValuePair<string, object> kvp in keySegment.Keys) | ||
| List<ODataPathSegment> pathSegments = path.AsList(); | ||
| int position = path.Count - 1; | ||
|
|
||
| while (position >= 0 && (pathSegments[position] is TypeSegment || pathSegments[position] is KeySegment)) | ||
| { | ||
| keys.Add(kvp.Key, kvp.Value); | ||
| --position; | ||
| } | ||
|
|
||
| return keys; | ||
| return position < 0 ? null : pathSegments[position]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a list of <see cref="ODataPathSegment"/> in an <see cref="ODataPath"/>. | ||
| /// </summary> | ||
| /// <param name="path">The <see cref="ODataPath"/>.</param> | ||
| /// <returns>List of <see cref="ODataPathSegment"/>.</returns> | ||
| public static List<ODataPathSegment> GetSegments(this ODataPath path) | ||
| { | ||
| return path.AsList(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.