Skip to content

Commit

Permalink
fix: when using SET_INITIAL_DATE_IF_NULL in STJ only set MinValue i…
Browse files Browse the repository at this point in the history
…f json value is not nullable

i think, this was the initial intention behind the converters? and there's no older/existing test that fails, so I think it's fine that way

note that this does _NOT_ change the behaviour of the newtonsoft converters!
  • Loading branch information
Konstantin committed Nov 20, 2024
1 parent e3f8a10 commit 39bdfa7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public override DateTime Read(
JsonSerializerOptions options
)
{
bool targetTypeIsNullable = Nullable.GetUnderlyingType(typeToConvert) != null;
if (reader.TokenType == JsonTokenType.Null && !targetTypeIsNullable)
{
return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
}
if (reader.TryGetDateTime(out var dt))
{
return DateTime.SpecifyKind(dt, DateTimeKind.Utc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public override DateTimeOffset Read(
JsonSerializerOptions options
)
{
bool targetTypeIsNullable = Nullable.GetUnderlyingType(typeToConvert) != null;
if (reader.TokenType == JsonTokenType.Null && !targetTypeIsNullable)
{
return DateTimeOffset.MinValue;
}
if (reader.TryGetDateTimeOffset(out var dto))
{
return dto;
Expand Down
78 changes: 78 additions & 0 deletions BO4ETestProject/TestNullableDateTimeOffsetConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using BO4E.meta.LenientConverters;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestBO4E;

[TestClass]
public class TestNullableDateTimeOffsetConverter
{
public class ClassWithNullableDateTimeOffset
{
public DateTimeOffset? Foo { get; set; }
}

[TestMethod]
public void Test_Nullable_DateTimeOffset_is_not_set_to_min_value()
{
const string jsonString = "{\"Foo\":null}";
var lenientParsing = LenientParsing.SET_INITIAL_DATE_IF_NULL | LenientParsing.DATE_TIME;
var obj = System.Text.Json.JsonSerializer.Deserialize<ClassWithNullableDateTimeOffset>(
jsonString,
lenientParsing.GetJsonSerializerOptions()
);
obj.Foo.Should().BeNull();
}

public class ClassWithNonNullableDateTimeOffset
{
public DateTimeOffset Foo { get; set; }
}

[TestMethod]
public void Test_NonNullable_DateTimeOffset_is_set_to_min_value()
{
const string jsonString = "{\"Foo\":null}";
var lenientParsing = LenientParsing.SET_INITIAL_DATE_IF_NULL | LenientParsing.DATE_TIME;
var obj = System.Text.Json.JsonSerializer.Deserialize<ClassWithNonNullableDateTimeOffset>(
jsonString,
lenientParsing.GetJsonSerializerOptions()
);
obj.Foo.Should().Be(DateTimeOffset.MinValue);
}

public class ClassWithNullableDateTime
{
public DateTime? Foo { get; set; }
}

[TestMethod]
public void Test_Nullable_DateTime_is_not_set_to_min_value()
{
const string jsonString = "{\"Foo\":null}";
var lenientParsing = LenientParsing.SET_INITIAL_DATE_IF_NULL | LenientParsing.DATE_TIME;
var obj = System.Text.Json.JsonSerializer.Deserialize<ClassWithNullableDateTime>(
jsonString,
lenientParsing.GetJsonSerializerOptions()
);
obj.Foo.Should().BeNull();
}

public class ClassWithNonNullableDateTime
{
public DateTime Foo { get; set; }
}

[TestMethod]
public void Test_NonNullable_DateTime_is_set_to_min_value()
{
const string jsonString = "{\"Foo\":null}";
var lenientParsing = LenientParsing.SET_INITIAL_DATE_IF_NULL | LenientParsing.DATE_TIME;
var obj = System.Text.Json.JsonSerializer.Deserialize<ClassWithNonNullableDateTime>(
jsonString,
lenientParsing.GetJsonSerializerOptions()
);
obj.Foo.Should().Be(DateTime.MinValue);
}
}

0 comments on commit 39bdfa7

Please sign in to comment.