From 1522ea6c023bbfd1e6db1667c56374966f60397e Mon Sep 17 00:00:00 2001 From: Ruslan N Date: Tue, 3 Sep 2024 16:03:58 +0200 Subject: [PATCH 1/2] fix enum serialization --- .../Writers/TypeScript/CodeFunctionWriter.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index 5a80b74ad8..67a8cc9581 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -401,16 +401,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) @@ -425,8 +424,6 @@ private void WriteSerializationStatementForComposedTypeProperty(CodeComposedType private void WriteComposedTypeSwitchClause(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; foreach (var type in composedType.Types.Where(x => IsPrimitiveType(x, composedType))) { @@ -438,7 +435,7 @@ private void WriteComposedTypeSwitchClause(CodeComposedTypeBase composedType, Co ? $"Array.isArray({modelParamName}.{codePropertyName}) && ({modelParamName}.{codePropertyName}).every(item => typeof item === '{nodeType}') :" : $"case 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("break;"); } } From 36da20c4499ce3bb2260a02990c41a2d7d3f8baf Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 4 Sep 2024 15:23:10 -0400 Subject: [PATCH 2/2] fix: adds demo unit test for enum collections Signed-off-by: Vincent Biret --- .../TypeScript/CodeFunctionWriterTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index b2894a186a..646cac3734 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 };