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) 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..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); @@ -1205,6 +1206,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); @@ -1305,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);