Skip to content

Commit fa646f9

Browse files
committed
refactor(document): simplify GetOperationById using single-pass helper
1 parent 8f94228 commit fa646f9

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -867,32 +867,23 @@ static bool AddToDictionary<TValue>(IDictionary<string, TValue> dict, string key
867867
{
868868
Utils.CheckArgumentNullOrEmpty(operationId);
869869

870-
foreach (var operation in GetAllOperations())
871-
{
872-
if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal))
873-
return operation;
874-
}
875-
return null;
876-
}
877-
878-
private IEnumerable<OpenApiOperation> GetAllOperations()
879-
{
880-
foreach (var operation in GetOperationsFromPathItems(Paths))
881-
yield return operation;
882-
883-
if (Webhooks is not null)
884-
foreach (var operation in GetOperationsFromPathItems(Webhooks))
885-
yield return operation;
870+
return GetOperationByIdFromPathItems(Paths, operationId) ??
871+
(Webhooks is not null ?
872+
GetOperationByIdFromPathItems(Webhooks, operationId) : null);
886873
}
887874

888-
private static IEnumerable<OpenApiOperation> GetOperationsFromPathItems(IDictionary<string, IOpenApiPathItem> pathItems)
875+
private static OpenApiOperation? GetOperationByIdFromPathItems(IDictionary<string, IOpenApiPathItem> pathItems, string operationId)
889876
{
890877
foreach (var pathItem in pathItems.Values)
891878
{
892879
if (pathItem.Operations is null) continue;
893880
foreach (var operation in pathItem.Operations.Values)
894-
yield return operation;
881+
{
882+
if (string.Equals(operation.OperationId, operationId, StringComparison.Ordinal))
883+
return operation;
884+
}
895885
}
886+
return null;
896887
}
897888
}
898889

0 commit comments

Comments
 (0)