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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
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
Expand Up @@ -91,7 +91,7 @@ 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);
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
break;

default:
Expand Down