Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes incorrect type name and factory method name generation in aliased scenario #5496

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where untyped not imports in TypeScripts would not be erasable. [microsoft/kiota-typescript#1381](https://github.com/microsoft/kiota-typescript/issues/1381)
- Updated schema link in plugin manifest to the correct url. [#5441](https://github.com/microsoft/kiota/issues/5441)
- Removed registration of default serialization and deserialization classes in client constructor. [#5478](https://github.com/microsoft/kiota/pull/5478)
- Fixed incorrect type name generation in aliased scenario in TS due to broad searching of types in root namespaces. [#5404](https://github.com/microsoft/kiota/issues/5404)

## [1.18.0] - 2024-09-05

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
public const string TYPE_DURATION = "Duration";
#pragma warning restore CA1707 // Remove the underscores

internal void WriteAutoGeneratedStart(LanguageWriter writer)

Check warning on line 43 in src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteAutoGeneratedStart' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 43 in src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteAutoGeneratedStart' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
writer.WriteLine("/* tslint:disable */");
writer.WriteLine("/* eslint-disable */");
writer.WriteLine("// Generated by Microsoft Kiota");
}
internal void WriteAutoGeneratedEnd(LanguageWriter writer)

Check warning on line 49 in src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteAutoGeneratedEnd' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 49 in src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs

View workflow job for this annotation

GitHub Actions / Build

Make 'WriteAutoGeneratedEnd' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
writer.WriteLine("/* tslint:enable */");
writer.WriteLine("/* eslint-enable */");
Expand Down Expand Up @@ -295,9 +295,9 @@
throw new InvalidOperationException($"Unable to find factory method for {targetClassType}");
}

private static CodeFunction? GetFactoryMethod(CodeInterface definitionClass, string factoryMethodName)
internal static CodeFunction? GetFactoryMethod(CodeInterface definitionClass, string factoryMethodName)
{
return definitionClass.GetImmediateParentOfType<CodeNamespace>(definitionClass)?.FindChildByName<CodeFunction>(factoryMethodName);
return definitionClass.GetImmediateParentOfType<CodeFile>(definitionClass)?.FindChildByName<CodeFunction>(factoryMethodName);
}

public string GetDeserializationMethodName(CodeTypeBase codeType, CodeMethod method, bool? IsCollection = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,82 @@ public async Task AliasesDuplicateUsingSymbolsAsync()
Assert.DoesNotContain(modelInterface.Usings, x => x.Declaration?.TypeDefinition == source2Interface);
Assert.DoesNotContain(modelInterface.Usings, x => x.Declaration?.TypeDefinition == source1Interface);
}

[Fact]
public async Task AliasesDuplicateUsingSymbolsAsyncWithAliasedReferencesToParentNamespaceAsync()
{
var generationConfiguration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript };
var source1 = TestHelper.CreateModelClassInModelsNamespace(generationConfiguration, root, "source");
var modelsNS = root.FindNamespaceByName(generationConfiguration.ModelsNamespaceName);
Assert.NotNull(modelsNS);
var submodelsNS = modelsNS.AddNamespace($"{generationConfiguration.ModelsNamespaceName}.submodels");
var source2 = TestHelper.CreateModelClass(submodelsNS, "source");
var model = TestHelper.CreateModelClass(submodelsNS, "model");

var source1Factory = new CodeMethod
{
Name = "factory",
Kind = CodeMethodKind.Factory,
IsAsync = false,
IsStatic = true,
ReturnType = new CodeType { Name = "source", TypeDefinition = source2, },
};
source1.AddMethod(source1Factory);

var using1 = new CodeUsing
{
Name = modelsNS.Name,
Declaration = new CodeType
{
Name = source1.Name,
TypeDefinition = source1,
IsExternal = false,
}
};

var source2Factory = new CodeMethod
{
Name = "factory",
Kind = CodeMethodKind.Factory,
IsAsync = false,
IsStatic = true,
ReturnType = new CodeType { Name = "source", TypeDefinition = source2 },
};

source2.AddMethod(source2Factory);
var using2 = new CodeUsing
{
Name = submodelsNS.Name,
Declaration = new CodeType
{
Name = source2.Name,
TypeDefinition = source2,
IsExternal = false,
}
};
model.AddUsing(using1);
var property1 = new CodeProperty { Name = "source1", Type = new CodeType { TypeDefinition = source1, } };
model.AddProperty(property1);
var property2 = new CodeProperty { Name = "source2", Type = new CodeType { TypeDefinition = source2, } };
model.AddProperty(property2);
model.AddUsing(using2);
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);

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

var modelCodeFile = submodelsNS.FindChildByName<CodeFile>(IndexFileName, false);
Assert.NotNull(modelCodeFile);
var source2Interface = modelCodeFile.Interfaces.First(x => x.Name == source1.Name.ToFirstCharacterUpperCase());

var result1 = Kiota.Builder.Writers.TypeScript.TypeScriptConventionService.GetFactoryMethod(source1Interface, "createSourceFromDiscriminatorValue");
Assert.NotNull(result1);
var result2 = Kiota.Builder.Writers.TypeScript.TypeScriptConventionService.GetFactoryMethod(source2Interface, "createSourceFromDiscriminatorValue");
Assert.NotNull(result2);
Assert.NotEqual(result1, result2);// they should be different discriminators despite having the same name
}

[Fact]
public async Task DoesNotKeepCancellationParametersInRequestExecutorsAsync()
{
Expand Down
Loading