Skip to content

Commit b32cd8b

Browse files
authored
Merge pull request #3261 from microsoft/andrueastman/fixTypescriptEnums
Fixes bitwise enum serialization in typescript.
2 parents ed37777 + b2e9348 commit b32cd8b

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- Work around a System.ComandLine bug where adjacent matching command names would crash the CLI parser (CLI). [microsoftgraph/msgraph-cli#316](https://github.com/microsoftgraph/msgraph-cli/issues/316) [microsoftgraph/msgraph-cli#320](https://github.com/microsoftgraph/msgraph-cli/issues/320), [dotnet/command-line-api#2260](https://github.com/dotnet/command-line-api/issues/2260)
3333
- Aggregate typescript import statements by source [#3232](https://github.com/microsoft/kiota/issues/3232)
3434
- Fixes a bug in dotnet where enums types would not be fully disambiguated when initialized in constructor with default values and existing conflicting property exists [#3233]
35+
- Fixes a bug in generation of flagged enum properties and their serializer/deserializer functions in typescript [https://github.com/microsoft/kiota/issues/3260]
3536

3637
## [1.5.1] - 2023-08-08
3738

src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private string GetSerializationMethodName(CodeTypeBase propType)
163163
private string? GetSerializationMethodNameForCodeType(CodeType propType, string propertyType)
164164
{
165165
if (propType.TypeDefinition is CodeEnum currentEnum)
166-
return $"writeEnumValue<{currentEnum.Name.ToFirstCharacterUpperCase()}>";
166+
return $"writeEnumValue<{currentEnum.Name.ToFirstCharacterUpperCase()}{(currentEnum.Flags && !propType.IsCollection ? "[]" : string.Empty)}>";
167167
else if (conventions.StreamTypeName.Equals(propertyType, StringComparison.OrdinalIgnoreCase))
168168
return "writeByteArrayValue";
169169
else if (propType.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None)

src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w
1313
if (codeElement.ExistsInExternalBaseType)
1414
return;
1515
var returnType = conventions.GetTypeString(codeElement.Type, codeElement);
16-
var isFlagEnum = codeElement.Type is CodeType currentType && currentType.TypeDefinition is CodeEnum currentEnum && currentEnum.Flags;
16+
var isFlagEnum = codeElement.Type is CodeType { TypeDefinition: CodeEnum { Flags: true } }
17+
&& !codeElement.Type.IsCollection;//collection of flagged enums are not supported/don't make sense
1718

1819
conventions.WriteLongDescription(codeElement, writer);
1920
switch (codeElement.Parent)

tests/Kiota.Builder.Tests/Writers/TypeScript/CodePropertyWriterTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,23 @@ public void WritesFlagEnums()
9494
var result = tw.ToString();
9595
Assert.Contains("[]", result);
9696
}
97+
[Fact]
98+
public void WritesCollectionFlagEnumsAsOneDimensionalArray()
99+
{
100+
property.Kind = CodePropertyKind.Custom;
101+
property.Type = new CodeType
102+
{
103+
Name = "customEnum",
104+
CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array
105+
};
106+
(property.Type as CodeType).TypeDefinition = new CodeEnum
107+
{
108+
Name = "customEnumType",
109+
Flags = true,//this is not expected for a collection. So treat as enum collection
110+
};
111+
writer.Write(property);
112+
var result = tw.ToString();
113+
Assert.Contains("[]", result);
114+
Assert.DoesNotContain("[] []", result);
115+
}
97116
}

0 commit comments

Comments
 (0)