Skip to content

Commit 027566d

Browse files
authored
Merge pull request #523 from ngboardway/master
Fix/#522
2 parents 8b630c4 + a2b527e commit 027566d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/JsonApiDotNetCore/Internal/TypeHelper.cs

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

43+
if(type == typeof(TimeSpan))
44+
return TimeSpan.Parse(stringValue);
45+
4346
if (type.GetTypeInfo().IsEnum)
4447
return Enum.Parse(type, stringValue);
4548

test/UnitTests/Internal/TypeHelper_Tests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ public void ConvertType_Returns_Default_Value_For_Empty_Strings()
106106
Assert.Equal(t.Value, result);
107107
}
108108
}
109+
110+
[Fact]
111+
public void Can_Convert_TimeSpans()
112+
{
113+
//arrange
114+
TimeSpan timeSpan = TimeSpan.FromMinutes(45);
115+
string stringSpan = timeSpan.ToString();
116+
117+
//act
118+
var result = TypeHelper.ConvertType(stringSpan, typeof(TimeSpan));
119+
120+
//assert
121+
Assert.Equal(timeSpan, result);
122+
}
123+
124+
[Fact]
125+
public void Bad_TimeSpanString_Throws()
126+
{
127+
// arrange
128+
var formattedString = "this_is_not_a_valid_timespan";
129+
130+
// act/assert
131+
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(TimeSpan)));
132+
}
109133

110134
private enum TestEnum
111135
{

0 commit comments

Comments
 (0)