Skip to content

Commit 1632509

Browse files
committed
fix(deserialization): deserialize enums
fix exception thrown when attributes are of an enum type during deserialization
1 parent 111773f commit 1632509

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/JsonApiDotNetCore/Internal/TypeHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static object ConvertType(object value, Type type)
2222
if (type == typeof(DateTimeOffset))
2323
return DateTimeOffset.Parse(stringValue);
2424

25+
if (type.GetTypeInfo().IsEnum)
26+
return Enum.Parse(type, stringValue);
27+
2528
return Convert.ChangeType(stringValue, type);
2629
}
2730
catch (Exception e)

test/UnitTests/Internal/TypeHelper_Tests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,23 @@ public void Bad_DateTimeOffset_String_Throws()
3030
// assert
3131
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset)));
3232
}
33+
34+
[Fact]
35+
public void Can_Convert_Enums()
36+
{
37+
// arrange
38+
var formattedString = "1";
39+
40+
// act
41+
var result = TypeHelper.ConvertType(formattedString, typeof(TestEnum));
42+
43+
// assert
44+
Assert.Equal(TestEnum.Test, result);
45+
}
46+
47+
public enum TestEnum
48+
{
49+
Test = 1
50+
}
3351
}
3452
}

0 commit comments

Comments
 (0)