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

Added the deserialize fix and test for the UTC DateTime #613

Merged
merged 3 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;
Expand All @@ -34,6 +35,7 @@ public void Deserialize_YamlWithInterfaceTypeAndMapping_ReturnsModel()
{
var yaml = @"
name: Jack
momentOfBirth: 1983-04-21T20:21:03.0041599Z
cars:
- name: Mercedes
year: 2018
Expand All @@ -48,6 +50,13 @@ public void Deserialize_YamlWithInterfaceTypeAndMapping_ReturnsModel()

var person = sut.Deserialize<Person>(yaml);
person.Name.Should().Be("Jack");
person.MomentOfBirth.Kind.Should().Be(DateTimeKind.Utc);
person.MomentOfBirth.ToUniversalTime().Year.Should().Be(1983);
person.MomentOfBirth.ToUniversalTime().Month.Should().Be(4);
person.MomentOfBirth.ToUniversalTime().Day.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Hour.Should().Be(20);
person.MomentOfBirth.ToUniversalTime().Minute.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Second.Should().Be(3);
person.Cars.Should().HaveCount(2);
person.Cars[0].Name.Should().Be("Mercedes");
person.Cars[0].Spec.Should().BeNull();
Expand All @@ -60,6 +69,7 @@ public void Deserialize_YamlWithTwoInterfaceTypesAndMappings_ReturnsModel()
{
var yaml = @"
name: Jack
momentOfBirth: 1983-04-21T20:21:03.0041599Z
cars:
- name: Mercedes
year: 2018
Expand All @@ -81,6 +91,13 @@ public void Deserialize_YamlWithTwoInterfaceTypesAndMappings_ReturnsModel()

var person = sut.Deserialize<Person>(yaml);
person.Name.Should().Be("Jack");
person.MomentOfBirth.Kind.Should().Be(DateTimeKind.Utc);
person.MomentOfBirth.ToUniversalTime().Year.Should().Be(1983);
person.MomentOfBirth.ToUniversalTime().Month.Should().Be(4);
person.MomentOfBirth.ToUniversalTime().Day.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Hour.Should().Be(20);
person.MomentOfBirth.ToUniversalTime().Minute.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Second.Should().Be(3);
person.Cars.Should().HaveCount(2);
person.Cars[0].Name.Should().Be("Mercedes");
person.Cars[0].Spec.EngineType.Should().Be("V6");
Expand All @@ -94,6 +111,8 @@ public class Person
{
public string Name { get; private set; }

public DateTime MomentOfBirth { get; private set; }

public IList<ICar> Cars { get; private set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IPars

case TypeCode.DateTime:
// TODO: This is probably incorrect. Use the correct regular expression.
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
var style = scalar.Value.EndsWith("Z")
? DateTimeStyles.RoundtripKind
: scalar.Value.Length > 27 ? DateTimeStyles.AssumeLocal : DateTimeStyles.None;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the length seems a bit fragile. Maybe a regex would be more robust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the skills to create a good regex for this, so I don't know if it would be more robust. The previous implementation is still the same, DateTimeStyles.None is the default. And the 'o' roundtrip is compliant with the ISO 8601, so I thought this won't break other implementations but would only add the correct TimeZone if this is known.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you are a busy man at the moment but is there a chance we can resolve this? What kind of regex are you thinking of?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I haven't had much free time. I made some experiments and it seems that instead of forcing the adjustment, you can simply pass the DateTimeStyles.RoundtripKind value to the style and you will get the correct kind. Have you tried that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.. I've overlooked that one, that is definitely a better solution.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can always use the DateTimeStyles.RoundtripKind, without any test:

value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, style);
break;

default:
Expand Down