Skip to content

Commit 773073e

Browse files
committed
fix(type-helper): do not silently return default
something unexpected happened, we should return an error
1 parent 2401baf commit 773073e

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/JsonApiDotNetCore/Internal/TypeHelper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ public static object ConvertType(object value, Type type)
2424

2525
return Convert.ChangeType(stringValue, type);
2626
}
27-
catch (Exception)
27+
catch (Exception e)
2828
{
29-
if (type.GetTypeInfo().IsValueType)
30-
return Activator.CreateInstance(type);
31-
32-
return null;
29+
throw new FormatException($"{ value } cannot be converted to { type.GetTypeInfo().Name }", e);
3330
}
3431
}
3532
}

test/UnitTests/Internal/TypeHelper_Tests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,16 @@ public void Can_Convert_DateTimeOffsets()
1919
// assert
2020
Assert.Equal(dto, result);
2121
}
22+
23+
[Fact]
24+
public void Bad_DateTimeOffset_String_Throws()
25+
{
26+
// arrange
27+
var formattedString = "this_is_not_a_valid_dto";
28+
29+
// act
30+
// assert
31+
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset)));
32+
}
2233
}
2334
}

0 commit comments

Comments
 (0)