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: enum serialization #5309

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 3 additions & 6 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeFunction>($"serialize{parentClass.Name.ToFirstCharacterUpperCase()}");
Assert.NotNull(serializerFunction);
var parentNS = serializerFunction.GetImmediateParentOfType<CodeNamespace>();
Assert.NotNull(parentNS);
var complexTypeDefinition = root.FindChildByName<CodeInterface>("SomeComplexType");
Assert.NotNull(complexTypeDefinition);
parentNS.TryAddCodeFile("foo", serializerFunction, parentClass, complexTypeDefinition);
writer.Write(serializerFunction);
var result = tw.ToString();
Assert.Contains("writeEnumValue<EnumTypeWithOption>(\"propWithDefaultEnum\"", result);
Assert.Contains("?? [EnumTypeWithOptionObject.SomeOption]", result);
}
[Fact]
public async Task WritesInheritedSerializerBodyAsync()
{
var generationConfiguration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript };
Expand Down
Loading