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

Add support for enum member attribute #77

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

### Added

## [1.0.4] - 2023-04-03

### Changed

- Fixes a bug where EnumMember attribute was not taken into account during serialization/deserialization

## [1.0.3] - 2023-03-15

### Changed
Expand Down Expand Up @@ -94,3 +98,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Initial Nuget release

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class JsonParseNodeTests
" ],\r\n" +
" \"displayName\": \"Megan Bowen\",\r\n" +
" \"numbers\":\"one,two,thirtytwo\"," +
" \"testNamingEnum\":\"Item2:SubItem1\"," +
" \"givenName\": \"Megan\",\r\n" +
" \"accountEnabled\": true,\r\n" +
" \"createdDateTime\": \"2017 -07-29T03:07:25Z\",\r\n" +
Expand Down Expand Up @@ -53,6 +54,7 @@ public void GetsEntityValueFromJson()
Assert.Equal("Auditor", testEntity.AdditionalData["jobTitle"]);
Assert.Equal("48d31887-5fad-4d73-a9f5-3c356e68a038", testEntity.Id);
Assert.Equal(TestEnum.One | TestEnum.Two, testEntity.Numbers ); // Unknown enum value is not included
Assert.Equal(TestNamingEnum.Item2SubItem1, testEntity.TestNamingEnum ); // correct value is chosen
Assert.Equal(TimeSpan.FromHours(1), testEntity.WorkDuration); // Parses timespan values
Assert.Equal(new Time(8,0,0).ToString(),testEntity.StartWorkTime.ToString());// Parses time values
Assert.Equal(new Time(17, 0, 0).ToString(), testEntity.EndWorkTime.ToString());// Parses time values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void WritesSampleCollectionOfObjectValues()
{
Id = "48d31887-5fad-4d73-a9f5-3c356e68a038",
Numbers = TestEnum.One | TestEnum.Two,
TestNamingEnum = TestNamingEnum.Item2SubItem1,
AdditionalData = new Dictionary<string, object>
{
{"mobilePhone",null}, // write null value
Expand All @@ -136,6 +137,7 @@ public void WritesSampleCollectionOfObjectValues()
var expectedString = "[{" +
"\"id\":\"48d31887-5fad-4d73-a9f5-3c356e68a038\"," +
"\"numbers\":\"one,two\"," +
"\"testNamingEnum\":\"item2:SubItem1\"," +
"\"mobilePhone\":null," +
"\"accountEnabled\":false," +
"\"jobTitle\":\"Author\"," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TestEntity : IParsable, IAdditionalDataHolder
/// <summary>Read-only.</summary>
public TestEnum? Numbers { get; set; }
/// <summary>Read-only.</summary>
public TestNamingEnum? TestNamingEnum { get; set; }
/// <summary>Read-only.</summary>
public TimeSpan? WorkDuration { get; set; }
/// <summary>Read-only.</summary>
public Date? BirthDay { get; set; }
Expand All @@ -40,6 +42,7 @@ public IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
return new Dictionary<string, Action<IParseNode>> {
{"id", n => { Id = n.GetStringValue(); } },
{"numbers", n => { Numbers = n.GetEnumValue<TestEnum>(); } },
{"testNamingEnum", n => { TestNamingEnum = n.GetEnumValue<TestNamingEnum>(); } },
{"createdDateTime", n => { CreatedDateTime = n.GetDateTimeOffsetValue(); } },
{"officeLocation", n => { OfficeLocation = n.GetStringValue(); } },
{"workDuration", n => { WorkDuration = n.GetTimeSpanValue(); } },
Expand All @@ -57,6 +60,7 @@ public void Serialize(ISerializationWriter writer)
_ = writer ?? throw new ArgumentNullException(nameof(writer));
writer.WriteStringValue("id", Id);
writer.WriteEnumValue<TestEnum>("numbers",Numbers);
writer.WriteEnumValue<TestNamingEnum>("testNamingEnum",TestNamingEnum);
writer.WriteDateTimeOffsetValue("createdDateTime", CreatedDateTime);
writer.WriteStringValue("officeLocation", OfficeLocation);
writer.WriteTimeSpanValue("workDuration", WorkDuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.Serialization;

namespace Microsoft.Kiota.Serialization.Json.Tests.Mocks
{
public enum TestNamingEnum
{
Item1,
[EnumMember(Value = "Item2:SubItem1")]
Item2SubItem1,
[EnumMember(Value = "Item3:SubItem1")]
Item3SubItem1
}
}
16 changes: 15 additions & 1 deletion src/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using Microsoft.Kiota.Abstractions.Serialization;
using Microsoft.Kiota.Abstractions;
Expand Down Expand Up @@ -152,7 +153,10 @@ public JsonParseNode(JsonElement node)
{
var rawValue = _jsonNode.GetString();
if(string.IsNullOrEmpty(rawValue)) return null;
if(typeof(T).GetCustomAttributes<FlagsAttribute>().Any())

var type = typeof(T);
rawValue = ToEnumRawName<T>(type, rawValue!);
if(type.GetCustomAttributes<FlagsAttribute>().Any())
{
return (T)(object)rawValue!
.Split(',')
Expand Down Expand Up @@ -384,5 +388,15 @@ private void AssignFieldValues<T>(T item) where T : IParsable

return default;
}

private string ToEnumRawName<T>(Type type, string value) where T : struct, Enum
{
if (type.GetMembers().FirstOrDefault(member =>
member.GetCustomAttribute<EnumMemberAttribute>() is { } attr &&
value.Equals(attr.Value, StringComparison.Ordinal))?.Name is { } strValue)
return strValue;

return value;
}
}
}
18 changes: 16 additions & 2 deletions src/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions;
using System.Xml;
Expand Down Expand Up @@ -241,10 +242,10 @@ public void WriteEnumValue<T>(string? key, T? value) where T : struct, Enum
writer.WriteStringValue(Enum.GetValues(typeof(T))
.Cast<T>()
.Where(x => value.Value.HasFlag(x))
.Select(x => Enum.GetName(typeof(T),x))
.Select(GetEnumName)
.Select(x => x.ToFirstCharacterLowerCase())
.Aggregate((x, y) => $"{x},{y}"));
else writer.WriteStringValue(value.Value.ToString().ToFirstCharacterLowerCase());
else writer.WriteStringValue(GetEnumName(value.Value).ToFirstCharacterLowerCase());
}
}

Expand Down Expand Up @@ -433,5 +434,18 @@ public void Dispose()
writer.Dispose();
GC.SuppressFinalize(this);
}

private string GetEnumName<T>(T value) where T : struct, Enum
{
var type = typeof(T);

if (Enum.GetName(type, value) is not { } name)
throw new ArgumentException($"Invalid Enum value {value} for enum of type {type}");

if (type.GetMember(name).FirstOrDefault()?.GetCustomAttribute<EnumMemberAttribute>() is { } attribute)
return attribute.Value;

return name;
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Serialization.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://microsoft.github.io/kiota/</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionPrefix>1.0.4</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down