Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Remove LINQ usage from Enum conversion from/to string #227

Merged
merged 3 commits into from
May 20, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.1] - 2024-05-20

### Changed

- Updated serialization and deserialization of enums to remove LINQ to resolve NativeAOT compatibility issue

## [1.3.0] - 2024-05-13

### Added
Expand Down
21 changes: 15 additions & 6 deletions src/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,21 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
rawValue = ToEnumRawName<T>(rawValue!);
if(type.GetCustomAttributes<FlagsAttribute>().Any())
{
return (T)(object)rawValue!
.Split(',')
.Select(x => Enum.TryParse<T>(x, true, out var result) ? result : (T?)null)
.Where(x => !x.Equals(null))
.Select(x => (int)(object)x!)
.Sum();
ReadOnlySpan<char> valueSpan = rawValue.AsSpan();
int value = 0;
while(valueSpan.Length > 0)
{
int commaIndex = valueSpan.IndexOf(',');
ReadOnlySpan<char> valueNameSpan = commaIndex < 0 ? valueSpan : valueSpan.Slice(0, commaIndex);
#if NET6_0_OR_GREATER
if(Enum.TryParse<T>(valueNameSpan, true, out var result))
#else
if(Enum.TryParse<T>(valueNameSpan.ToString(), true, out var result))
#endif
value |= (int)(object)result;
valueSpan = commaIndex < 0 ? ReadOnlySpan<char>.Empty : valueSpan.Slice(commaIndex + 1);
}
return (T)(object)value;
}
else
return Enum.TryParse<T>(rawValue, true,out var result) ? result : null;
Expand Down
25 changes: 18 additions & 7 deletions src/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions;
using System.Xml;
using System.Text;

#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
Expand Down Expand Up @@ -278,16 +280,25 @@ public void WriteEnumValue<T>(string? key, T? value) where T : struct, Enum
if(value.HasValue)
{
if(typeof(T).GetCustomAttributes<FlagsAttribute>().Any())
WriteStringValue(null,
{
var values =
#if NET5_0_OR_GREATER
Enum.GetValues<T>()
Enum.GetValues<T>();
#else
Enum.GetValues(typeof(T))
.Cast<T>()
Enum.GetValues(typeof(T)).Cast<T>();
#endif
.Where(x => value.Value.HasFlag(x))
.Select(GetEnumName)
.Aggregate((x, y) => $"{x},{y}"));
StringBuilder valueNames = new StringBuilder();
foreach (var x in values)
{
if(value.Value.HasFlag(x) && GetEnumName(x) is string valueName)
{
if (valueNames.Length > 0)
valueNames.Append(",");
valueNames.Append(valueName);
}
}
WriteStringValue(null, valueNames.ToString());
}
else WriteStringValue(null, GetEnumName(value.Value));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Serialization.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.3.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Loading