From 517bb168e2ddd1f4bcaac9c4b35025a2301274b8 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Wed, 3 Jul 2024 10:27:25 +0300 Subject: [PATCH] Adds missing TS imports --- CHANGELOG.md | 1 + .../Refiners/TypeScriptRefiner.cs | 3 +- .../TypeScriptLanguageRefinerTests.cs | 50 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2136947087..484ca91ea8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Ensures HashSet properties in `KiotaLock` maintain IgnoreCase comparer across runs [#4916](https://github.com/microsoft/kiota/issues/4916) - Dropped `client base url set to` message when generating plugins. [#4905](https://github.com/microsoft/kiota/issues/4905) - Emit `[GeneratedCode]` attribute for C# types. [#4907](https://github.com/microsoft/kiota/issues/4907) +- Fixes missing imports for `UntypedNode` for method parameter and return value scenarios. [#4925](https://github.com/microsoft/kiota/issues/4925) ## [1.15.0] - 2024-06-06 diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 5e6c9673ca..92a5618a92 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -528,7 +528,8 @@ private static bool HasMultipartBody(CodeMethod m) => AbstractionsPackageName, true, "BackingStore", "BackedModel"), new (static x => x is CodeMethod m && HasMultipartBody(m), AbstractionsPackageName, MultipartBodyClassName, $"serialize{MultipartBodyClassName}"), - new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase), + new (static x => (x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) + || (x is CodeMethod method && (method.Parameters.Any(param => param.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) || method.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase))), AbstractionsPackageName, KiotaBuilder.UntypedNodeName, "createUntypedNodeFromDiscriminatorValue"), }; private const string MultipartBodyClassName = "MultipartBody"; diff --git a/tests/Kiota.Builder.Tests/Refiners/TypeScriptLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/TypeScriptLanguageRefinerTests.cs index 0c7178dd7d..8da35ab461 100644 --- a/tests/Kiota.Builder.Tests/Refiners/TypeScriptLanguageRefinerTests.cs +++ b/tests/Kiota.Builder.Tests/Refiners/TypeScriptLanguageRefinerTests.cs @@ -840,5 +840,55 @@ public async Task AddsUsingForUntypedNode() Assert.Single(nodeUsing); Assert.Equal("@microsoft/kiota-abstractions", nodeUsing[0].Declaration.Name); } + [Fact] + public async Task AddsUsingForUntypedNodeInReturnType() + { + var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First(); + var model = new CodeMethod + { + Name = "getAsync", + ReturnType = new CodeType + { + Name = KiotaBuilder.UntypedNodeName + } + }; + requestBuilderClass.AddMethod(model); + await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root); + Assert.Equal(KiotaBuilder.UntypedNodeName, model.ReturnType.Name);// type is renamed + Assert.NotEmpty(requestBuilderClass.StartBlock.Usings); + var nodeUsing = requestBuilderClass.StartBlock.Usings.Where(static declaredUsing => declaredUsing.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToArray(); + Assert.Single(nodeUsing); + Assert.Equal("@microsoft/kiota-abstractions", nodeUsing[0].Declaration.Name); + } + [Fact] + public async Task AddsUsingForUntypedNodeInMethodParameter() + { + var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First(); + var method = new CodeMethod + { + Name = "getAsync", + ReturnType = new CodeType + { + Name = "string", + IsExternal = true + } + }; + method.AddParameter(new CodeParameter() + { + Name = "jsonData", + Type = new CodeType() + { + Name = KiotaBuilder.UntypedNodeName, + IsExternal = true + } + }); + requestBuilderClass.AddMethod(method); + await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root); + Assert.Equal(KiotaBuilder.UntypedNodeName, method.Parameters.First().Type.Name);// type is renamed + Assert.NotEmpty(requestBuilderClass.StartBlock.Usings); + var nodeUsing = requestBuilderClass.StartBlock.Usings.Where(static declaredUsing => declaredUsing.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToArray(); + Assert.Single(nodeUsing); + Assert.Equal("@microsoft/kiota-abstractions", nodeUsing[0].Declaration.Name); + } #endregion }