From 44f735b580fec5c5efca542ba3f5ae8265b73513 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 18 Dec 2024 13:51:31 +0300 Subject: [PATCH] fix: Add strconv import only for types that strconv can parse to string --- src/Kiota.Builder/Refiners/GoRefiner.cs | 5 ++- .../Refiners/GoLanguageRefinerTests.cs | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 2754d7e818..234cb625bf 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -148,7 +148,7 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken AddConstructorsForDefaultValues( generatedCode, true, - true, //forcing add as constructors are required for by factories + true, //forcing add as constructors are required for by factories new[] { CodeClassKind.RequestConfiguration }); cancellationToken.ThrowIfCancellationRequested(); MakeModelPropertiesNullable( @@ -744,6 +744,9 @@ private static void AddErrorAndStringsImportForEnums(CodeElement currentElement) "Duration", "TimeOnly", "DateOnly", + "TimeSpan", + "Time", + "ISODuration", "string", "UUID", "Guid" diff --git a/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs index 478560e00b..8d60a649fa 100644 --- a/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs +++ b/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs @@ -1303,5 +1303,46 @@ public async Task AddsUsingForUntypedNodeInMethodParameterAsync() Assert.Single(nodeUsing); Assert.Equal("github.com/microsoft/kiota-abstractions-go/serialization", nodeUsing[0].Declaration.Name); } + [Theory] + [InlineData("ISODuration", false)] + [InlineData("DateOnly", false)] + [InlineData("TimeOnly", false)] + [InlineData("Time", false)] + [InlineData("DateTimeOffset", false)] + [InlineData("Guid", false)] + [InlineData("string", false)] + [InlineData("boolean", true)] + [InlineData("int64", true)] + [InlineData("integer", true)] + [InlineData("long", true)] + [InlineData("float", true)] + public async Task ImportsStrConvForRelevantTypesOnly(string pathParameterType, bool isImported) + { + var model = root.AddClass(new CodeClass + { + Name = "RequestBuilder", + Kind = CodeClassKind.RequestBuilder + }).First(); + var constructor = model.AddMethod(new CodeMethod + { + Name = "NewRequestBuilder", + Kind = CodeMethodKind.Constructor, + ReturnType = new CodeType + { + Name = "void" + } + }).First(); + constructor.AddParameter(new CodeParameter + { + Name = "daysInPast", + Kind = CodeParameterKind.Path, + Type = new CodeType + { + Name = pathParameterType + } + }); + await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Go }, root); + Assert.Equal(isImported, model.StartBlock.Usings.Any(static x => x.Declaration.Name.Equals("strconv", StringComparison.OrdinalIgnoreCase))); + } #endregion }