Skip to content

Commit

Permalink
Optimize ODataPathExtensions.GetNavigationSource (#1161)
Browse files Browse the repository at this point in the history
* Optimize GetNavigationSource source

* Use >= 0 instead of > -1
  • Loading branch information
habbes committed Jan 23, 2024
1 parent 1b37400 commit e28c230
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions src/Microsoft.AspNetCore.OData/Routing/ODataPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,58 @@ public static IEdmNavigationSource GetNavigationSource(this ODataPath path)
throw Error.ArgumentNull(nameof(path));
}

ODataPathNavigationSourceHandler handler = new ODataPathNavigationSourceHandler();
path.WalkWith(handler);
return handler.NavigationSource;
for (int i = path.Count - 1; i >= 0; --i)
{
ODataPathSegment segment = path[i];
if (segment is EntitySetSegment entitySetSegment)
{
return entitySetSegment.EntitySet;
}

if (segment is KeySegment keySegment)
{
return keySegment.NavigationSource;
}

if (segment is NavigationPropertyLinkSegment navigationPropertyLinkSegment)
{
return navigationPropertyLinkSegment.NavigationSource;
}

if (segment is NavigationPropertySegment navigationPropertySegment)
{
return navigationPropertySegment.NavigationSource;
}

if (segment is OperationImportSegment operationImportSegment)
{
return operationImportSegment.EntitySet;
}

if (segment is OperationSegment operationSegment)
{
return operationSegment.EntitySet;
}

if (segment is SingletonSegment singleton)
{
return singleton.Singleton;
}

if (segment is TypeSegment typeSegment)
{
return typeSegment.NavigationSource;
}

if (segment is PropertySegment)
{
continue;
}

return null;
}

return null;
}

/// <summary>
Expand Down

0 comments on commit e28c230

Please sign in to comment.