From 7c5a5b7479251c77c13a5afaef835a6558845726 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 29 Nov 2023 09:33:29 -0500 Subject: [PATCH] - fixes concurrency issue with inheritance index Signed-off-by: Vincent Biret --- src/Kiota.Builder/KiotaBuilder.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index e8fa656c1c..af0c2f02db 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -2033,23 +2033,23 @@ private void TrimInheritedModels() foreach (var leafNamespace in FindLeafNamespaces(modelsNamespace)) RemoveEmptyNamespaces(leafNamespace, modelsNamespace); } - private ConcurrentDictionary> GetDerivationIndex(IEnumerable models) + private ConcurrentDictionary> GetDerivationIndex(IEnumerable models) { - var result = new ConcurrentDictionary>(); + var result = new ConcurrentDictionary>(); Parallel.ForEach(models, parallelOptions, x => { - if (x.BaseClass is CodeClass parentClass && !result.TryAdd(parentClass, new() { x })) + if (x.BaseClass is CodeClass parentClass && !result.TryAdd(parentClass, [x])) result[parentClass].Add(x); }); return result; } - private ConcurrentDictionary> GetInheritanceIndex(ConcurrentDictionary> derivedIndex) + private ConcurrentDictionary> GetInheritanceIndex(ConcurrentDictionary> derivedIndex) { - var result = new ConcurrentDictionary>(); + var result = new ConcurrentDictionary>(); Parallel.ForEach(derivedIndex, parallelOptions, entry => { foreach (var derivedClass in entry.Value) - if (!result.TryAdd(derivedClass, new() { entry.Key })) + if (!result.TryAdd(derivedClass, [entry.Key])) result[derivedClass].Add(entry.Key); }); return result; @@ -2069,12 +2069,12 @@ private static void RemoveEmptyNamespaces(CodeNamespace currentNamespace, CodeNa parentNamespace.RemoveChildElement(currentNamespace); RemoveEmptyNamespaces(parentNamespace, stopAtNamespace); } - private static IEnumerable GetDerivedDefinitions(ConcurrentDictionary> models, CodeClass[] modelsInUse) + private static IEnumerable GetDerivedDefinitions(ConcurrentDictionary> models, CodeClass[] modelsInUse) { var currentDerived = modelsInUse.SelectMany(x => models.TryGetValue(x, out var res) ? res : Enumerable.Empty()).ToArray(); - return currentDerived.Union(currentDerived.SelectMany(x => GetDerivedDefinitions(models, new CodeClass[] { x }))); + return currentDerived.Union(currentDerived.SelectMany(x => GetDerivedDefinitions(models, [x]))); } - private static IEnumerable GetRelatedDefinitions(CodeElement currentElement, ConcurrentDictionary> derivedIndex, ConcurrentDictionary> inheritanceIndex, ConcurrentDictionary? visited = null) + private static IEnumerable GetRelatedDefinitions(CodeElement currentElement, ConcurrentDictionary> derivedIndex, ConcurrentDictionary> inheritanceIndex, ConcurrentDictionary? visited = null) { visited ??= new(); if (currentElement is not CodeClass currentClass || !visited.TryAdd(currentClass, true)) return Enumerable.Empty(); @@ -2084,7 +2084,7 @@ private static IEnumerable GetRelatedDefinitions(CodeElement curren .Where(static x => x is CodeClass || x is CodeEnum) .SelectMany(x => x is CodeClass classDefinition ? (inheritanceIndex.TryGetValue(classDefinition, out var res) ? res : Enumerable.Empty()) - .Union(GetDerivedDefinitions(derivedIndex, new CodeClass[] { classDefinition })) + .Union(GetDerivedDefinitions(derivedIndex, [classDefinition])) .Union(new[] { classDefinition }) .OfType() : new[] { x })