JsonApiDotNetCore icon indicating copy to clipboard operation
JsonApiDotNetCore copied to clipboard

Add support for DateOnly/TimeOnly

Open bkoelman opened this issue 2 years ago • 2 comments

This PR adds support for using DateOnly and TimeOnly in resource models, which were introduced in .NET 6. PostgreSQL fully supports these types. But the .NET 6 version of System.Text.Json does not, so you currently need to reference a recent .NET 7 pre-release version of STJ to make it work.

While implementing this, I found that query string parsing uses the OS-level culture settings. For example, if you're running your API on an English language version of Windows, but have customized regional settings to display dates in Dutch format, then you'll need to use Dutch notation for date values in query strings (for example 31-12-2022 instead of 12/31/2022). This doesn't apply to JSON request/response bodies, so the behavior is quite inconsistent. This PR fixes that by always using the Invariant culture. A backward-compatibility switch has been added, so we don't need to wait for the next major release. To revert to the old behavior, add the following at startup:

AppContext.SetSwitch("JsonApiDotNetCore.ParseQueryStringsUsingCurrentCulture", true);

Fixes #1168.

QUALITY CHECKLIST

  • [x] Changes implemented in code
  • [x] Complies with our contributing guidelines
  • [x] Adapted tests
  • [ ] N/A: Documentation updated

bkoelman avatar Jul 10 '22 21:07 bkoelman

Codecov Report

Merging #1169 (9d17ce1) into master (8e8427e) will increase coverage by 0.05%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1169      +/-   ##
==========================================
+ Coverage   92.63%   92.69%   +0.05%     
==========================================
  Files         243      243              
  Lines        7806     7815       +9     
==========================================
+ Hits         7231     7244      +13     
+ Misses        575      571       -4     
Impacted Files Coverage Δ
...tations/Resources/Internal/RuntimeTypeConverter.cs 100.00% <100.00%> (+10.81%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Jul 17 '22 20:07 codecov[bot]

Converted back to Draft because this hasn't been tested with ModelState validation, so may not work.

bkoelman avatar Sep 03 '22 16:09 bkoelman

Turns out this isn't going to work well with ModelState validation on .NET 6, because RangeAttribute internally depends on System.ComponentModel.TypeConverter, which doesn't support DateOnly and TimeOnly. The missing support was added in .NET 7. But there does not seem to be a separate NuGet package we can reference for .NET 6 to make this work out of the box.

~Therefore I think we should postpone this PR until JsonApiDotNetCore targets .NET 7.~

Update: There's a polyfill NuGet package now, to fill the gap for .NET 6.

bkoelman avatar Oct 02 '22 15:10 bkoelman