This DTO was correctly deserialized when I was on version 4.0.34. However, recently I updated the entire system to 4.0.40 and I’ve noticed that the Date property ends up being ‘null’ in my POST request.
Any idea why this might be the case? Is there a possible issue with deserialization when the name of the property is Date?
The problem appears to be in the date format. It doesn’t like 06/28/2015 anymore. This used to work, but after the update it stopped. If I change the date to 6/28/2015, it works fine. Thanks!
Please see the correct way to unambiguously specify dates: https://xkcd.com/1179/
To minimize issues you should always use the above format when specifying dates across the wire.
You can workaround this issue atm by adding a parse error fallback:
DateTimeSerializer.OnParseErrorFn = (s, ex) => {
var parts = s.Split('/');
return new DateTime(int.Parse(parts[2]), int.Parse(parts[0]), int.Parse(parts[1]));
};
The reason why it works for D/MM/YYYY is because it avoids manual date parsing and falls back to DateTime.Parse(). I’ve also added a fix where it will fallback to DateTime.Parse() if an invalid dateformat was provided, so it should now support all different formats in this commit. Although the recommendation is still to use the unambiguous YYYY-MM-DD where possible.
Thanks for the pointers. While prototyping, I had simply passed the value to the backend that bootstrap datepicker had provided. It makes sense to change to the YYYY-MM-DD format.
I love the fact that you have a pre-release nuget!! And, thanks for a quick fix as well as the workaround for catching parsing errors!