Skip to content

Commit

Permalink
Prevent type casting nil values in Go collections (microsoft#2766)
Browse files Browse the repository at this point in the history
* Add nil check before typecasting in go collections

* Update changelog
  • Loading branch information
rkodev authored Jun 14, 2023
1 parent 87d42bf commit c5a3f60
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Deprecated Visual Studio OpenAPI reference packages.
- Fixes a bug where a stackoverflow exception occurs when inlined schemas have self-references [2656](https://github.com/microsoft/kiota/issues/2656)
- Fixes nil safety while type casting values in collections in Go

## [1.3.0] - 2023-06-09

Expand Down
4 changes: 4 additions & 0 deletions src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,12 @@ private static void WriteCollectionCast(string propertyTypeImportName, string so
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();
writer.CloseBlock();
}

private static string getSendMethodName(string returnType, CodeMethod codeElement, bool isPrimitive, bool isBinary, bool isEnum)
Expand Down Expand Up @@ -919,12 +921,14 @@ private void WriteSerializationMethodCall(CodeTypeBase propType, CodeElement par
writer.WriteLines($"cast := make([]{parsableSymbol}, len({valueGet}))",
$"for i, v := range {valueGet} {{");
writer.IncreaseIndent();
writer.StartBlock("if v != nil {");
if (isInterface)
writer.WriteLine($"cast[i] = {GetTypeAssertion("v", parsableSymbol)}");
else
writer.WriteLines("temp := v", // temporary creating a new reference to avoid pointers to the same object
$"cast[i] = {parsableSymbol}(&temp)");
writer.CloseBlock();
writer.CloseBlock();
}
var collectionPrefix = propType.IsCollection ? "CollectionOf" : string.Empty;
var collectionSuffix = propType.IsCollection ? "s" : string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ public void WritesModelFactoryBodyForUnionModels()
Assert.Contains("result.SetStringValue(val)", result);
Assert.Contains("else if val, err := parseNode.GetCollectionOfObjectValues(CreateComplexType2FromDiscriminatorValue); val != nil {", result);
Assert.Contains("cast := make([]ComplexType2, len(val))", result);
Assert.Contains("if v != nil ", result);
Assert.Contains("for i, v := range val", result);
Assert.Contains("result.SetComplexType2Value(cast)", result);
Assert.Contains("return result, nil", result);
Expand Down

0 comments on commit c5a3f60

Please sign in to comment.