Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix go scalar collection cast #4388

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 20 additions & 6 deletions src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading