runtime: Deserialize of DateOnly in System.Text.Json 9.0.0 returns incorrect date and year

Description

JsonSerializer.Deserializeof a DateOnly field returns the incorrect date and year.

Reproduction Steps

public class TestDateOnly
{
    public DateOnly? TestDate { get; set; } // same problem if not nullable
}

[TestMethod]
public void DateOnlyConverter()
{
    var testDate = new DateOnly(2001, 01, 25);

    var response = new TestDateOnly { TestDate = testDate };
    var json = JsonSerializer.Serialize(response); // serialization is correct

    var test = JsonSerializer.Deserialize<TestDateOnly>(json);
    Assert.IsNotNull(test);
    Assert.AreEqual(testDate, test.TestDate);
}

Expected behavior

Test should pass.

Actual behavior

Assert.AreEqual failed. Expected:<1/25/2001>. Actual:<1/1/0001>

Regression?

Test passes with System.Text.Json 8.0.4.

Known Workarounds

No response

Configuration

Windows 10, .NET 9, x64

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 28 (18 by maintainers)

Most upvoted comments

I dug into this a little deeper. It fails if I target .NET 6.0. If I target .NET 8.0 or .NET 9.0, it works correctly.