From e28c230c526a944d8a8a46c03be3320dcbd2d6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Habinshuti?= Date: Tue, 23 Jan 2024 11:30:43 +0300 Subject: [PATCH] Optimize `ODataPathExtensions.GetNavigationSource` (#1161) * Optimize GetNavigationSource source * Use >= 0 instead of > -1 --- .../Routing/ODataPathExtensions.cs | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Routing/ODataPathExtensions.cs b/src/Microsoft.AspNetCore.OData/Routing/ODataPathExtensions.cs index e11951499..a41af0ff6 100644 --- a/src/Microsoft.AspNetCore.OData/Routing/ODataPathExtensions.cs +++ b/src/Microsoft.AspNetCore.OData/Routing/ODataPathExtensions.cs @@ -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; } ///