Skip to content

Commit

Permalink
- fixes failing unit tests for typescript models grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Dec 5, 2023
1 parent 2aea146 commit 6f2ea96
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 89 deletions.
11 changes: 7 additions & 4 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ private static void GroupReusableModelsInSingleFile(CodeElement currentElement)
codeNamespace.RemoveChildElement(enums);
}
RemoveSelfReferencingUsingForFile(targetFile, codeNamespace);
AliasCollidingSymbols(targetFile.GetChildElements(true).SelectMany(GetUsingsFromCodeElement).Distinct(), targetFile.Name);
var childElements = targetFile.GetChildElements(true).ToArray();
AliasCollidingSymbols(childElements.SelectMany(GetUsingsFromCodeElement).Distinct(), childElements.Select(static x => x.Name).ToHashSet(StringComparer.OrdinalIgnoreCase));
}
CrawlTree(currentElement, GroupReusableModelsInSingleFile);
}
Expand Down Expand Up @@ -332,6 +333,10 @@ private static void RemoveSelfReferencingUsings(CodeElement currentElement)
}

private static void AliasCollidingSymbols(IEnumerable<CodeUsing> usings, string currentSymbolName)
{
AliasCollidingSymbols(usings, new HashSet<string>(StringComparer.OrdinalIgnoreCase) { currentSymbolName });
}
private static void AliasCollidingSymbols(IEnumerable<CodeUsing> usings, HashSet<string> currentSymbolNames)
{
var enumeratedUsings = usings.ToArray();
var duplicatedSymbolsUsings = enumeratedUsings.Where(static x => !x.IsExternal)
Expand All @@ -342,9 +347,7 @@ private static void AliasCollidingSymbols(IEnumerable<CodeUsing> usings, string
.SelectMany(static x => x)
.Union(enumeratedUsings
.Where(static x => !x.IsExternal && x.Declaration != null)
.Where(x => x.Declaration!
.Name
.Equals(currentSymbolName, StringComparison.OrdinalIgnoreCase)))
.Where(x => currentSymbolNames.Contains(x.Declaration!.Name)))
.ToArray();
foreach (var usingElement in duplicatedSymbolsUsings)
usingElement.Alias = (usingElement.Declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ public async Task AddsUsingsForErrorTypesForRequestExecutor()
public async Task EscapesReservedKeywords()
{
var generationConfiguration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript };
var model = TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "break");
TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "break");
await ILanguageRefiner.Refine(generationConfiguration, root);
var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var interFaceModel = codeFile.Interfaces.First(x => "BreakEscaped".Equals(x.Name, StringComparison.Ordinal));
Assert.NotEqual("break", interFaceModel.Name);
Expand Down Expand Up @@ -352,7 +353,8 @@ public async Task CorrectsCoreType()

await ILanguageRefiner.Refine(generationConfiguration, root);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);

var interFaceModel = codeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
Expand Down Expand Up @@ -387,7 +389,8 @@ public async Task ReplacesDateTimeOffsetByNativeType()
}).First();
await ILanguageRefiner.Refine(generationConfiguration, root);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var modelInterface = codeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
Assert.NotEmpty(modelInterface.StartBlock.Usings);
Expand All @@ -409,7 +412,8 @@ public async Task ReplacesGuidsByRespectiveType()
}).First();
await ILanguageRefiner.Refine(generationConfiguration, root);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var modelInterface = codeFile.Interfaces.First(x => x.Name.Equals(model.Name, StringComparison.OrdinalIgnoreCase));
Assert.NotEmpty(modelInterface.StartBlock.Usings);
Expand All @@ -431,7 +435,8 @@ public async Task ReplacesDateOnlyByNativeType()
}).First();
await ILanguageRefiner.Refine(generationConfiguration, root);
Assert.NotEmpty(model.StartBlock.Usings);
var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var modelInterface = codeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
Assert.NotEmpty(modelInterface.StartBlock.Usings);
Expand All @@ -453,13 +458,15 @@ public async Task ReplacesTimeOnlyByNativeType()
await ILanguageRefiner.Refine(generationConfiguration, root);
Assert.NotEmpty(model.StartBlock.Usings);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var modelInterface = codeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
Assert.NotEmpty(modelInterface.StartBlock.Usings);
Assert.Equal("TimeOnly", modelInterface.Properties.First(x => x.Name == codeProperty.Name).Type.Name);

}
private const string IndexFileName = "index";
[Fact]
public async Task ReplacesDurationByNativeType()
{
Expand All @@ -475,7 +482,8 @@ public async Task ReplacesDurationByNativeType()
}).First();
await ILanguageRefiner.Refine(generationConfiguration, root);
Assert.NotEmpty(model.StartBlock.Usings);
var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
var modelInterface = codeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
Assert.NotEmpty(modelInterface.StartBlock.Usings);
Expand Down Expand Up @@ -561,26 +569,14 @@ public async Task AliasesDuplicateUsingSymbols()
model.AddUsing(using2);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);

var modelCodeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelCodeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(modelCodeFile);
var modelInterface = modelCodeFile.Interfaces.First(x => x.Name == model.Name.ToFirstCharacterUpperCase());
var source1Interface = modelCodeFile.Interfaces.First(x => x.Name == source1.Name.ToFirstCharacterUpperCase());
var source2Interface = modelCodeFile.Interfaces.First(x => x.Name == source2.Name.ToFirstCharacterUpperCase());

var source1CodeFile = modelsNS.FindChildByName<CodeFile>(source1.Name.ToFirstCharacterUpperCase());
Assert.NotNull(source1CodeFile);
var source1Interface = source1CodeFile.Interfaces.First(x => x.Name == source1.Name.ToFirstCharacterUpperCase());

var source2CodeFile = submodelsNS.FindChildByName<CodeFile>(source2.Name.ToFirstCharacterUpperCase());
Assert.NotNull(source2CodeFile);
var source2Interface = source2CodeFile.Interfaces.First(x => x.Name == source2.Name.ToFirstCharacterUpperCase());

var modelUsing1 = modelInterface.Usings.First(x => x.Declaration?.TypeDefinition == source2Interface);
var modelUsing2 = modelInterface.Usings.First(x => x.Declaration?.TypeDefinition == source1Interface);
Assert.NotNull(modelUsing1.Declaration);
Assert.NotNull(modelUsing2.Declaration);
Assert.Equal(modelUsing1.Declaration.Name, modelUsing2.Declaration.Name);
Assert.NotEmpty(modelUsing1.Alias);
Assert.NotEmpty(modelUsing2.Alias);
Assert.NotEqual(modelUsing1.Alias, modelUsing2.Alias);
Assert.Empty(modelInterface.Usings.Where(x => x.Declaration?.TypeDefinition == source2Interface));
Assert.Empty(modelInterface.Usings.Where(x => x.Declaration?.TypeDefinition == source1Interface));
}
[Fact]
public async Task DoesNotKeepCancellationParametersInRequestExecutors()
Expand Down Expand Up @@ -620,11 +616,12 @@ public async Task DoesNotKeepCancellationParametersInRequestExecutors()
public async Task AddsModelInterfaceForAModelClass()
{
var generationConfiguration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript };
var model = TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "modelA");
TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "modelA");

await ILanguageRefiner.Refine(generationConfiguration, root);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile);
Assert.Contains(codeFile.Interfaces, static x => "ModelA".Equals(x.Name, StringComparison.OrdinalIgnoreCase));
}
Expand Down Expand Up @@ -778,7 +775,8 @@ public async Task GeneratesCodeFiles()

await ILanguageRefiner.Refine(generationConfiguration, root);

var codeFile = root.FindChildByName<CodeFile>(model.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(codeFile); // codefile exists

// model , interface, deserializer, serializer should be direct descendant of the codefile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public async Task WritesAutoGenerationStart()
var parentClass = TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "parentClass", true);
TestHelper.AddSerializationPropertiesToModelClass(parentClass);
await ILanguageRefiner.Refine(generationConfiguration, root);
var codeFile = root.FindChildByName<CodeFile>(parentClass.Name.ToFirstCharacterUpperCase());
var modelsNS = root.FindChildByName<CodeNamespace>(generationConfiguration.ModelsNamespaceName);
var codeFile = modelsNS.FindChildByName<CodeFile>("index", false);
WriteCode(writer, codeFile);

var result = tw.ToString();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ReplacesImportsSubNamespace()
};
declaration.AddUsings(nUsing);
var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS);
Assert.Equal("./messages/message", result.Item3);
Assert.Equal("./messages/", result.Item3);
}
[Fact]
public void ReplacesImportsParentNamespace()
Expand Down Expand Up @@ -118,8 +118,8 @@ public void ReplacesImportsInOtherTrunk()
declaration2.AddUsings(nUsing2);
var result = importManager.GetRelativeImportPathForUsing(nUsing, usedRangeNS1);
var result2 = importManager.GetRelativeImportPathForUsing(nUsing2, usedRangeNS2);
Assert.Equal("../../../../workbookRange", result.Item3);
Assert.Equal("../../workbookRange", result2.Item3);
Assert.Equal("../../../../", result.Item3);
Assert.Equal("../../", result2.Item3);
}
[Fact]
public void ReplacesImportsSameNamespace()
Expand All @@ -141,7 +141,7 @@ public void ReplacesImportsSameNamespace()
};
declaration.AddUsings(nUsing);
var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS);
Assert.Equal("./message", result.Item3);
Assert.Equal("./", result.Item3);
}

[Fact]
Expand All @@ -165,6 +165,6 @@ public void ReplacesImportsSameNamespaceIndex()
};
declaration.AddUsings(nUsing);
var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS);
Assert.Equal("./index", result.Item3);
Assert.Equal("./", result.Item3);
}
}

0 comments on commit 6f2ea96

Please sign in to comment.