Skip to content

Commit

Permalink
- fixes null ref exception in types trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Nov 29, 2023
1 parent 71962a7 commit 02e5698
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Kiota.Builder/CodeDOM/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public virtual void RenameChildElement(string oldName, string newName)
}
else throw new InvalidOperationException($"The element to rename was not found {oldName}");
}
public void RemoveChildElement<T>(params T[] elements) where T : CodeElement
public void RemoveChildElement<T>(params T[] elements) where T : ICodeElement
{
if (elements == null) return;
RemoveChildElementByName(elements.Select(static x => x.Name).ToArray());
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/CodeDOM/ICodeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ string Name
{
get; set;
}
T GetImmediateParentOfType<T>(CodeElement? item = null);
}
17 changes: 9 additions & 8 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1991,12 +1991,12 @@ private IEnumerable<KeyValuePair<string, CodeType>> GetDiscriminatorMappings(Ope
.Where(static x => x.Value != null)
.Select(static x => KeyValuePair.Create(x.Key, x.Value!));
}
private static IEnumerable<CodeElement> GetAllModels(CodeNamespace currentNamespace)
private static IEnumerable<ITypeDefinition> GetAllModels(CodeNamespace currentNamespace)
{
var classes = currentNamespace.Classes.ToArray();
return classes.Union(classes.SelectMany(GetAllInnerClasses))
.Where(static x => x.IsOfKind(CodeClassKind.Model))
.OfType<CodeElement>()
.OfType<ITypeDefinition>()
.Union(currentNamespace.Enums)
.Union(currentNamespace.Namespaces.SelectMany(static x => GetAllModels(x)));
}
Expand All @@ -2016,7 +2016,7 @@ private void TrimInheritedModels()
var classesInUse = derivedClassesInUse.Union(classesDirectlyInUse).Union(baseOfModelsInUse).ToHashSet();
var reusableClassesDerivationIndex = GetDerivationIndex(reusableModels.OfType<CodeClass>());
var reusableClassesInheritanceIndex = GetInheritanceIndex(allModelClassesIndex);
var relatedModels = classesInUse.SelectMany(x => GetRelatedDefinitions(x, reusableClassesDerivationIndex, reusableClassesInheritanceIndex)).Union(modelsDirectlyInUse.Where(x => x is CodeEnum)).ToHashSet();// re-including models directly in use for enums
var relatedModels = classesInUse.SelectMany(x => GetRelatedDefinitions(x, reusableClassesDerivationIndex, reusableClassesInheritanceIndex)).Union(modelsDirectlyInUse.OfType<CodeEnum>()).ToHashSet();// re-including models directly in use for enums
Parallel.ForEach(reusableModels, parallelOptions, x =>
{
if (relatedModels.Contains(x) || classesInUse.Contains(x)) return;
Expand Down Expand Up @@ -2074,23 +2074,24 @@ private static IEnumerable<CodeClass> GetDerivedDefinitions(ConcurrentDictionary
var currentDerived = modelsInUse.SelectMany(x => models.TryGetValue(x, out var res) ? res : Enumerable.Empty<CodeClass>()).ToArray();
return currentDerived.Union(currentDerived.SelectMany(x => GetDerivedDefinitions(models, [x])));
}
private static IEnumerable<CodeElement> GetRelatedDefinitions(CodeElement currentElement, ConcurrentDictionary<CodeClass, ConcurrentBag<CodeClass>> derivedIndex, ConcurrentDictionary<CodeClass, ConcurrentBag<CodeClass>> inheritanceIndex, ConcurrentDictionary<CodeElement, bool>? visited = null)
private static IEnumerable<ITypeDefinition> GetRelatedDefinitions(ITypeDefinition currentElement, ConcurrentDictionary<CodeClass, ConcurrentBag<CodeClass>> derivedIndex, ConcurrentDictionary<CodeClass, ConcurrentBag<CodeClass>> inheritanceIndex, ConcurrentDictionary<CodeElement, bool>? visited = null)
{
visited ??= new();
if (currentElement is not CodeClass currentClass || !visited.TryAdd(currentClass, true)) return Enumerable.Empty<CodeElement>();
if (currentElement is not CodeClass currentClass || !visited.TryAdd(currentClass, true)) return Enumerable.Empty<ITypeDefinition>();
var propertiesDefinitions = currentClass.Properties
.SelectMany(static x => x.Type.AllTypes)
.Select(static x => x.TypeDefinition!)
.Select(static x => x.TypeDefinition)
.OfType<ITypeDefinition>()
.Where(static x => x is CodeClass || x is CodeEnum)
.SelectMany(x => x is CodeClass classDefinition ?
(inheritanceIndex.TryGetValue(classDefinition, out var res) ? res : Enumerable.Empty<CodeClass>())
.Union(GetDerivedDefinitions(derivedIndex, [classDefinition]))
.Union(new[] { classDefinition })
.OfType<CodeElement>() :
.OfType<ITypeDefinition>() :
new[] { x })
.Distinct()
.ToArray();
var propertiesParentTypes = propertiesDefinitions.OfType<CodeClass>().SelectMany(static x => x.GetInheritanceTree(false, false)).ToArray();
var propertiesParentTypes = propertiesDefinitions.OfType<CodeClass>().SelectMany(static x => x.GetInheritanceTree(false, false)).OfType<ITypeDefinition>().ToArray();
return propertiesDefinitions
.Union(propertiesParentTypes)
.Union(propertiesParentTypes.SelectMany(x => GetRelatedDefinitions(x, derivedIndex, inheritanceIndex, visited)))
Expand Down

0 comments on commit 02e5698

Please sign in to comment.