Skip to content

Commit

Permalink
fix: Add strconv import only for types that strconv can parse to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Dec 18, 2024
1 parent 8971314 commit 44f735b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -744,6 +744,9 @@ private static void AddErrorAndStringsImportForEnums(CodeElement currentElement)
"Duration",
"TimeOnly",
"DateOnly",
"TimeSpan",
"Time",
"ISODuration",
"string",
"UUID",
"Guid"
Expand Down
41 changes: 41 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 44f735b

Please sign in to comment.