Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws an exception if the datetime cannot be converted. #16288

Merged
merged 15 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
throw new ArgumentException("Unexpected type to convert.", nameof(typeToConvert));
}

if (!reader.TryGetDateTime(out var value) && DateTime.TryParse(reader.GetString()!, out value))
if (!reader.TryGetDateTime(out var value))
{
return value;
var stringValue = reader.GetString();
if (DateTime.TryParse(stringValue, out value))
gvkries marked this conversation as resolved.
Show resolved Hide resolved
{
return value;
}

throw new JsonException($"Unable to convert \"{stringValue}\" to DateTime.");
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved
gvkries marked this conversation as resolved.
Show resolved Hide resolved
}

return value;
Expand Down
32 changes: 32 additions & 0 deletions test/OrchardCore.Tests/Data/ContentItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ namespace OrchardCore.Tests.Data;

public class ContentItemTests
{
[Fact]
public void NullValueDateTimeFieldSerialisationTest()
{
// Arrange
var jsonStr = """
{
"NullValueDateTimeFieldTest": {
"Value": null
}
}
""";

var jobject = JsonNode.Parse(jsonStr);

var nullValueDateTimeField = jobject.SelectNode("NullValueDateTimeFieldTest").ToObject<DateTimeField>();
Assert.Null(nullValueDateTimeField.Value);
Assert.Null(JObject.FromObject(nullValueDateTimeField).SelectNode("Value"));
}
/// <summary>
/// To validate <see cref="DateTimeJsonConverter"/>
/// and <seealso cref="TimeSpanJsonConverter"/>
Expand All @@ -25,6 +43,12 @@ public void JsonNode_WhenParseCalled_ConvertShortTimeFormatToTimeField()
"DateTimeFieldTest": {
"Value": "2024-5-31 13:05"
},
"EmptyValueDateTimeFieldTest": {
"Value": ""
},
"ErrorFormatDateTimeFieldTest": {
"Value": "ErrorFormatValue"
},
"TimezoneDateTimeFieldTest": {
"Value": "2022-12-13T21:02:18.399-05:00"
},
Expand All @@ -42,13 +66,21 @@ public void JsonNode_WhenParseCalled_ConvertShortTimeFormatToTimeField()
var timezoneDateTimeFieldTest = jobject.SelectNode("TimezoneDateTimeFieldTest").ToObject<DateTimeField>();

// Assert

var emptyValueTestexcepion = Assert.Throws<JsonException>(() => jobject.SelectNode("EmptyValueDateTimeFieldTest").ToObject<DateTimeField>());
Assert.Equal("Unable to convert \"\" to DateTime.", emptyValueTestexcepion.Message);

var errorFormatValueTestexcepion = Assert.Throws<JsonException>(() => jobject.SelectNode("ErrorFormatDateTimeFieldTest").ToObject<DateTimeField>());
Assert.Equal("Unable to convert \"ErrorFormatValue\" to DateTime.", errorFormatValueTestexcepion.Message);

Assert.Equal("13:05:00", timeField.Value.Value.ToString());
Assert.Equal("2024-05-31", dateField.Value.Value.ToString("yyyy-MM-dd"));
Assert.Equal("2024-05-31 13:05", dateTimeField.Value.Value.ToString("yyyy-MM-dd HH:mm"));
Assert.Equal("13:05:00", JObject.FromObject(timeField).SelectNode("Value").ToString());
Assert.Equal("2024-05-31T00:00:00Z", JObject.FromObject(dateField).SelectNode("Value").ToString());
Assert.Equal("2024-05-31T13:05:00Z", JObject.FromObject(dateTimeField).SelectNode("Value").ToString());


var utcTime = TimeZoneInfo.ConvertTimeToUtc(timezoneDateTimeFieldTest.Value.Value);
Assert.Equal("2022-12-14 02:02:18", utcTime.ToString("yyyy-MM-dd HH:mm:ss"));
}
Expand Down