From 30b41033d8663a3ff99214af0353fa3d347c08a6 Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Fri, 22 Mar 2024 15:49:38 +0100 Subject: [PATCH 1/3] Fix go scalar collection cast --- .../Writers/Go/CodeMethodWriter.cs | 26 ++++++++++++++----- .../Writers/Go/CodeMethodWriterTests.cs | 2 ++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs index 989c9df1d7..ebda6de8f3 100644 --- a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs @@ -723,16 +723,23 @@ private void WriteFieldDeserializer(CodeProperty property, LanguageWriter writer private static string GetTypeAssertion(string originalReference, string typeImportName, string? assignVarName = default, string? statusVarName = default) => $"{assignVarName}{(!string.IsNullOrEmpty(statusVarName) && !string.IsNullOrEmpty(assignVarName) ? ", " : string.Empty)}{statusVarName}{(string.IsNullOrEmpty(statusVarName) && string.IsNullOrEmpty(assignVarName) ? string.Empty : " := ")}{originalReference}.({typeImportName})"; - private static void WriteCollectionCast(string propertyTypeImportName, string sourceVarName, string targetVarName, LanguageWriter writer, string pointerSymbol = "*", bool dereference = true) + private static void WriteCollectionCast(string propertyTypeImportName, string sourceVarName, string targetVarName, LanguageWriter writer, string pointerSymbol = "*", bool dereference = true, bool scalar = false) { writer.WriteLines($"{targetVarName} := make([]{propertyTypeImportName}, len({sourceVarName}))", $"for i, v := range {sourceVarName} {{"); writer.IncreaseIndent(); - writer.StartBlock("if v != nil {"); - var derefPrefix = dereference ? "*(" : string.Empty; - var derefSuffix = dereference ? ")" : string.Empty; - writer.WriteLine($"{targetVarName}[i] = {GetTypeAssertion(derefPrefix + "v", pointerSymbol + propertyTypeImportName)}{derefSuffix}"); - writer.CloseBlock(); + if (!scalar) + { + writer.StartBlock("if v != nil {"); + var derefPrefix = dereference ? "*(" : string.Empty; + var derefSuffix = dereference ? ")" : string.Empty; + writer.WriteLine($"{targetVarName}[i] = {GetTypeAssertion(derefPrefix + "v", pointerSymbol + propertyTypeImportName)}{derefSuffix}"); + writer.CloseBlock(); + } + else + { + writer.WriteLine($"{targetVarName}[i] = v"); + } writer.CloseBlock(); } @@ -909,7 +916,14 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req writer.WriteBlock("if err != nil {", "}", "return nil, err"); } else + { + if (requestParams.requestBody.Type.IsCollection) + { + WriteCollectionCast("interface{}", bodyParamReference, "cast", writer, string.Empty, false, true); + bodyParamReference = "cast"; + } writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{collectionSuffix}({contextParameterName}, m.{requestAdapterPropertyName}, \"{sanitizedRequestBodyContentType}\", {bodyParamReference})"); + } } writer.WriteLine($"return {RequestInfoVarName}, nil"); diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs index 54ded91a6e..59561012b5 100644 --- a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs @@ -1205,6 +1205,8 @@ public async Task WritesRequestGeneratorBodyForScalarCollection() Assert.Contains("if c.Q != nil", result); Assert.Contains("requestInfo.AddQueryParameters(", result); Assert.Contains("requestInfo.AddRequestOptions(", result); + Assert.Contains("cast := make([]interface{}, ", result); + Assert.Contains("cast[i] = v, ", result); Assert.Contains("requestInfo.SetContentFromScalarCollection(ctx, m.BaseRequestBuilder.RequestAdapter", result); Assert.Contains("return requestInfo, nil", result); AssertExtensions.CurlyBracesAreClosed(result); From 688c0d64c0cc23fe4646eae2bde33788d3769921 Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Fri, 22 Mar 2024 17:05:57 +0100 Subject: [PATCH 2/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90a7c1d6d1..e109a7f170 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed Go generation bug when dealing with scalar collection body. [#4380](https://github.com/microsoft/kiota/issues/4380) - Fixed a bug where multiple Visual Studio Code instances would make the extension install/update fail. [#3686](https://github.com/microsoft/kiota/issues/3686) - Fixed a bug where models properties named "additionalData" or "backingstore" would be ignored. [#4224](https://github.com/microsoft/kiota/issues/4224) - PREVIEW: Renamed the config commands to workspace. [#4310](https://github.com/microsoft/kiota/issues/4310) From b0337cfed64cf2dbcf30c879c669a52f3785fcab Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Sat, 23 Mar 2024 01:28:20 +0100 Subject: [PATCH 3/3] Fix test --- tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs index 59561012b5..46fc85c386 100644 --- a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs @@ -1168,6 +1168,7 @@ public async Task WritesRequestGeneratorBodyForScalar() Assert.Contains("if c.Q != nil", result); Assert.Contains("requestInfo.AddQueryParameters(", result); Assert.Contains("requestInfo.AddRequestOptions(", result); + Assert.DoesNotContain("cast[i] = v", result); Assert.Contains("requestInfo.SetContentFromScalar(ctx, m.BaseRequestBuilder.RequestAdapter", result); Assert.Contains("return requestInfo, nil", result); AssertExtensions.CurlyBracesAreClosed(result); @@ -1206,7 +1207,7 @@ public async Task WritesRequestGeneratorBodyForScalarCollection() Assert.Contains("requestInfo.AddQueryParameters(", result); Assert.Contains("requestInfo.AddRequestOptions(", result); Assert.Contains("cast := make([]interface{}, ", result); - Assert.Contains("cast[i] = v, ", result); + Assert.Contains("cast[i] = v", result); Assert.Contains("requestInfo.SetContentFromScalarCollection(ctx, m.BaseRequestBuilder.RequestAdapter", result); Assert.Contains("return requestInfo, nil", result); AssertExtensions.CurlyBracesAreClosed(result); @@ -1307,6 +1308,7 @@ public async Task WritesRequestGeneratorBodyForParsableCollection() Assert.Contains("if c.Q != nil", result); Assert.Contains("requestInfo.AddQueryParameters(", result); Assert.Contains("requestInfo.AddRequestOptions(", result); + Assert.Contains("cast[i] =", result); Assert.Contains("requestInfo.SetContentFromParsableCollection(ctx, m.BaseRequestBuilder.RequestAdapter", result); Assert.Contains("return requestInfo, nil", result); AssertExtensions.CurlyBracesAreClosed(result);