Skip to content

Commit

Permalink
fix(Deserialize): fail on enums marked with Flags (#142)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergii Kurganskiy <[email protected]>
  • Loading branch information
Paulskit and Sergii Kurganskiy authored Dec 7, 2023
1 parent df18960 commit 9d6b4b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/AvroConvert/AvroObjectServices/Read/Resolvers/Int.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ private object ConvertValue(Type readType, object value)
{
switch (readType)
{
case not null when readType.IsEnum:
return Enum.ToObject(readType, value);

case not null when readType == typeof(int):
case not null when readType == typeof(int?):
return Convert.ToInt32(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public void Enum_object(Func<object, Type, dynamic> engine)
//Assert
Assert.Equal(toSerialize, deserialized);
}

[Theory]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
public void Enum_with_flags_object(Func<object, Type, dynamic> engine)
{
foreach (var toSerialize in Enum.GetValues<TestEnumWithFlags>())
{
//Act
var deserialized = engine.Invoke(toSerialize, typeof(TestEnumWithFlags));

//Assert
Assert.Equal(toSerialize, deserialized);
}
}

[Theory]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
Expand Down
11 changes: 11 additions & 0 deletions tests/AvroConvertTests/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ public enum TestEnum
ca,
dlo
}

[Flags]
public enum TestEnumWithFlags
{
None = 0,
Option1 = 1,
Option2 = 2,
Option3 = 4,
Option4 = 8,
CombinedOption = Option1 | Option2
}

[Equals(DoNotAddEqualityOperators = true)]
public class ClassWithEnum
Expand Down

0 comments on commit 9d6b4b7

Please sign in to comment.