diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index 1710944648..d6b49b28b8 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -409,16 +409,15 @@ private void WritePropertySerializer(string modelParamName, CodeProperty codePro private void WritePropertySerializationStatement(CodeProperty codeProperty, string modelParamName, string? serializationName, string? defaultValueSuffix, CodeFunction codeFunction, LanguageWriter writer) { var isCollectionOfEnum = IsCollectionOfEnum(codeProperty); - var spreadOperator = isCollectionOfEnum ? "..." : string.Empty; var codePropertyName = codeProperty.Name.ToFirstCharacterLowerCase(); var composedType = GetOriginalComposedType(codeProperty.Type); - if (!string.IsNullOrWhiteSpace(spreadOperator)) + if (isCollectionOfEnum) writer.WriteLine($"if({modelParamName}.{codePropertyName})"); if (composedType is not null && (composedType.IsComposedOfPrimitives(IsPrimitiveType) || composedType.IsComposedOfObjectsAndPrimitives(IsPrimitiveType))) WriteSerializationStatementForComposedTypeProperty(composedType, modelParamName, codeFunction, writer, codeProperty, string.Empty); else - writer.WriteLine($"writer.{serializationName}(\"{codeProperty.WireName}\", {spreadOperator}{modelParamName}.{codePropertyName}{defaultValueSuffix});"); + writer.WriteLine($"writer.{serializationName}(\"{codeProperty.WireName}\", {modelParamName}.{codePropertyName}{defaultValueSuffix});"); } private void WriteSerializationStatementForComposedTypeProperty(CodeComposedTypeBase composedType, string modelParamName, CodeFunction method, LanguageWriter writer, CodeProperty codeProperty, string? serializeName) @@ -431,8 +430,6 @@ private void WriteSerializationStatementForComposedTypeProperty(CodeComposedType private void WriteComposedTypeIfClause(CodeComposedTypeBase composedType, CodeFunction method, LanguageWriter writer, CodeProperty codeProperty, string modelParamName, string defaultValueSuffix) { var codePropertyName = codeProperty.Name.ToFirstCharacterLowerCase(); - var isCollectionOfEnum = IsCollectionOfEnum(codeProperty); - var spreadOperator = isCollectionOfEnum ? "..." : string.Empty; bool isFirst = true; foreach (var type in composedType.Types.Where(x => IsPrimitiveType(x, composedType))) @@ -446,7 +443,7 @@ private void WriteComposedTypeIfClause(CodeComposedTypeBase composedType, CodeFu ? $"{isElse}if (Array.isArray({modelParamName}.{codePropertyName}) && ({modelParamName}.{codePropertyName}).every(item => typeof item === '{nodeType}')) {{" : $"{isElse}if ( typeof {modelParamName}.{codePropertyName} === \"{nodeType}\") {{"); - writer.WriteLine($"writer.{serializationName}(\"{codeProperty.WireName}\", {spreadOperator}{modelParamName}.{codePropertyName}{defaultValueSuffix} as {nodeType});"); + writer.WriteLine($"writer.{serializationName}(\"{codeProperty.WireName}\", {modelParamName}.{codePropertyName}{defaultValueSuffix} as {nodeType});"); writer.CloseBlock(); isFirst = false; } diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index 6eee533f97..e18bea8fd0 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs @@ -377,6 +377,54 @@ public async Task WritesDeSerializerBodyWithDefaultValueAsync() Assert.Contains("?? EnumTypeWithOptionObject.SomeOption", result); } [Fact] + public async Task WritesSerializerBodyEnumCollectionAsync() + { + var parentClass = TestHelper.CreateModelClass(root, "parentClass"); + TestHelper.AddSerializationPropertiesToModelClass(parentClass); + var propName = "propWithDefaultValue"; + parentClass.AddProperty(new CodeProperty + { + Name = propName, + Kind = CodePropertyKind.Custom, + Type = new CodeType + { + Name = "string", + }, + }); + var propertyEnum = new CodeEnum + { + Name = "EnumTypeWithOption", + Parent = parentClass, + }; + var enumOption = new CodeEnumOption() { Name = "SomeOption" }; + propertyEnum.AddOption(enumOption); + var codeNamespace = parentClass.Parent as CodeNamespace; + codeNamespace.AddEnum(propertyEnum); + parentClass.AddProperty(new CodeProperty + { + Name = "propWithDefaultEnum", + DefaultValue = enumOption.Name, + Type = new CodeType + { + TypeDefinition = propertyEnum, + CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array, + } + }); + + await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root); + var serializerFunction = root.FindChildByName($"serialize{parentClass.Name.ToFirstCharacterUpperCase()}"); + Assert.NotNull(serializerFunction); + var parentNS = serializerFunction.GetImmediateParentOfType(); + Assert.NotNull(parentNS); + var complexTypeDefinition = root.FindChildByName("SomeComplexType"); + Assert.NotNull(complexTypeDefinition); + parentNS.TryAddCodeFile("foo", serializerFunction, parentClass, complexTypeDefinition); + writer.Write(serializerFunction); + var result = tw.ToString(); + Assert.Contains("writeEnumValue(\"propWithDefaultEnum\"", result); + Assert.Contains("?? [EnumTypeWithOptionObject.SomeOption]", result); + } + [Fact] public async Task WritesInheritedSerializerBodyAsync() { var generationConfiguration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript };