Skip to content

Commit

Permalink
fix: null check on composed types
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Aug 19, 2024
1 parent d6953fb commit ed4888d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void WriteSerializationFunctionForCodeUnionTypes(CodeComposedTypeBase co
}

var paramName = composedParam.Name.ToFirstCharacterLowerCase();
writer.WriteLine($"if ({paramName} === undefined) return;");
writer.WriteLine($"if ({paramName} === undefined || {paramName} === null) return;");
WriteDiscriminatorSwitchBlock(discriminatorInfo, paramName, codeElement, writer);
}

Expand Down Expand Up @@ -190,7 +190,7 @@ private void WriteDiscriminatorSwitchBlock(DiscriminatorInformation discriminato
private void WriteSerializationFunctionForTypeComposedOfPrimitives(CodeComposedTypeBase composedType, CodeParameter composedParam, CodeFunction method, LanguageWriter writer)
{
var paramName = composedParam.Name.ToFirstCharacterLowerCase();
writer.WriteLine($"if ({paramName} === undefined) return;");
writer.WriteLine($"if ({paramName} === undefined || {paramName} === null) return;");
writer.StartBlock($"switch (typeof {paramName}) {{");

foreach (var type in composedType.GetPrimitiveTypes(IsComposedPrimitive))
Expand Down Expand Up @@ -220,7 +220,7 @@ private static void WriteApiConstructorBody(CodeFile parentFile, CodeMethod meth
if (method.Parameters.OfKind(CodeParameterKind.RequestAdapter)?.Name.ToFirstCharacterLowerCase() is not string requestAdapterArgumentName) return;
if (!string.IsNullOrEmpty(method.BaseUrl))
{
writer.StartBlock($"if ({requestAdapterArgumentName}.baseUrl === undefined || {requestAdapterArgumentName}.baseUrl === \"\") {{");
writer.StartBlock($"if ({requestAdapterArgumentName}.baseUrl === undefined || {requestAdapterArgumentName}.baseUrl === null || {requestAdapterArgumentName}.baseUrl === \"\") {{");
writer.WriteLine($"{requestAdapterArgumentName}.baseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TranslateType_ReturnsCorrectTranslation_WhenComposedTypeIsNotNull()
var result = TypeScriptConventionService.TranslateTypescriptType(composedType);
Assert.Equal("Test", result);
}

public CodeType CurrentType()
{
CodeType currentType = new CodeType { Name = "SomeType" };
Expand Down

0 comments on commit ed4888d

Please sign in to comment.