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
Changes from 1 commit
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 @@ -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)
Expand All @@ -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)))
{
Expand All @@ -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;");
}
}
Expand Down
Loading