Skip to content
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
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Internal/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static object ConvertType(object value, Type type)
if (type == typeof(DateTimeOffset))
return DateTimeOffset.Parse(stringValue);

if(type == typeof(TimeSpan))
return TimeSpan.Parse(stringValue);

if (type.GetTypeInfo().IsEnum)
return Enum.Parse(type, stringValue);

Expand Down
24 changes: 24 additions & 0 deletions test/UnitTests/Internal/TypeHelper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ public void ConvertType_Returns_Default_Value_For_Empty_Strings()
Assert.Equal(t.Value, result);
}
}

[Fact]
public void Can_Convert_TimeSpans()
{
//arrange
TimeSpan timeSpan = TimeSpan.FromMinutes(45);
string stringSpan = timeSpan.ToString();

//act
var result = TypeHelper.ConvertType(stringSpan, typeof(TimeSpan));

//assert
Assert.Equal(timeSpan, result);
}

[Fact]
public void Bad_TimeSpanString_Throws()
{
// arrange
var formattedString = "this_is_not_a_valid_timespan";

// act/assert
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(TimeSpan)));
}

private enum TestEnum
{
Expand Down