From d74f4065693970306938b16a94388c54f9fa330e Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 25 Jan 2022 10:53:47 +0300 Subject: [PATCH] Date,Time and Timepan handling --- .../Microsoft.Graph.Core.csproj | 4 +- src/Microsoft.Graph.Core/Models/Date.cs | 82 ------------------- src/Microsoft.Graph.Core/Models/Duration.cs | 46 ----------- src/Microsoft.Graph.Core/Models/TimeOfDay.cs | 75 ----------------- .../Serialization/SerializerTests.cs | 23 +++--- .../TestModels/DateTestClass.cs | 13 +-- 6 files changed, 21 insertions(+), 222 deletions(-) delete mode 100644 src/Microsoft.Graph.Core/Models/Date.cs delete mode 100644 src/Microsoft.Graph.Core/Models/Duration.cs delete mode 100644 src/Microsoft.Graph.Core/Models/TimeOfDay.cs diff --git a/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj b/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj index 7e9a18890..4bd9bc0e2 100644 --- a/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj +++ b/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj @@ -100,9 +100,9 @@ - + - + diff --git a/src/Microsoft.Graph.Core/Models/Date.cs b/src/Microsoft.Graph.Core/Models/Date.cs deleted file mode 100644 index d2dc3c58d..000000000 --- a/src/Microsoft.Graph.Core/Models/Date.cs +++ /dev/null @@ -1,82 +0,0 @@ -// ------------------------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. -// ------------------------------------------------------------------------------ - -namespace Microsoft.Graph -{ - using System; - using System.Text.Json.Serialization; - - /// - /// Custom Date model for serialization - /// - public class Date - { - /// - /// Internal Date constructor - /// - /// - internal Date(DateTime dateTime) - { - this.DateTime = dateTime; - } - - /// - /// Create a new Date object from a year, month, and day. - /// - /// The year. - /// The month. - /// The day of the month. - public Date(int year, int month, int day) - : this(new DateTime(year, month, day)) - { - } - - /// - /// The DateTime object. - /// - internal DateTime DateTime { get; set; } - - /// - /// The date's year. - /// - public int Year - { - get - { - return this.DateTime.Year; - } - } - - /// - /// The date's month. - /// - public int Month - { - get - { - return this.DateTime.Month; - } - } - - /// - /// The date's day. - /// - public int Day - { - get - { - return this.DateTime.Day; - } - } - - /// - /// Convert the date to a string. - /// - /// The string value of the date in the format "yyyy-MM-dd". - public override string ToString() - { - return this.DateTime.ToString("yyyy-MM-dd"); - } - } -} diff --git a/src/Microsoft.Graph.Core/Models/Duration.cs b/src/Microsoft.Graph.Core/Models/Duration.cs deleted file mode 100644 index 0b580bb11..000000000 --- a/src/Microsoft.Graph.Core/Models/Duration.cs +++ /dev/null @@ -1,46 +0,0 @@ -// ------------------------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. -// ------------------------------------------------------------------------------ - -namespace Microsoft.Graph -{ - using System; - using System.Xml; - using System.Text.Json.Serialization; - - /// - /// Represents an edm.duration value. - /// - public class Duration - { - internal TimeSpan TimeSpan { get; set; } - - /// - /// Create a Duration object from a TimeSpan. - /// - /// - public Duration(TimeSpan timeSpan) - { - this.TimeSpan = timeSpan; - } - - /// - /// Create a Duration object from an ISO8601 duration. - /// - /// An ISO8601 duration. http://en.wikipedia.org/wiki/ISO_8601#Durations - public Duration(string duration) - { - // Convert an ISO8601 duration to a TimeSpan. - this.TimeSpan = XmlConvert.ToTimeSpan(duration); - } - - /// - /// Convert the stored TimeSpan into an ISO8601 duration. - /// - /// An ISO8601 duration. For example, PT1M is "period time of 1 minute" - public override string ToString() - { - return XmlConvert.ToString(this.TimeSpan); - } - } -} diff --git a/src/Microsoft.Graph.Core/Models/TimeOfDay.cs b/src/Microsoft.Graph.Core/Models/TimeOfDay.cs deleted file mode 100644 index 77ecd1414..000000000 --- a/src/Microsoft.Graph.Core/Models/TimeOfDay.cs +++ /dev/null @@ -1,75 +0,0 @@ -// ------------------------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. -// ------------------------------------------------------------------------------ - -namespace Microsoft.Graph -{ - using System; - using System.Text.Json.Serialization; - - /// - /// Time of day model. - /// - public class TimeOfDay - { - internal DateTime DateTime { get; set; } - - internal TimeOfDay(DateTime dateTime) - { - this.DateTime = dateTime; - } - - /// - /// Create a new TimeOfDay from hours, minutes, and seconds. - /// - /// The hour. - /// The minute. - /// The second. - public TimeOfDay(int hour, int minute, int second) - : this(new DateTime(1, 1, 1, hour, minute, second)) - { - } - - /// - /// The hour. - /// - public int Hour - { - get - { - return this.DateTime.Hour; - } - } - - /// - /// The minute. - /// - public int Minute - { - get - { - return this.DateTime.Minute; - } - } - - /// - /// The second. - /// - public int Second - { - get - { - return this.DateTime.Second; - } - } - - /// - /// The time of day, formatted as "HH:mm:ss". - /// - /// The string time of day. - public override string ToString() - { - return this.DateTime.ToString("HH:mm:ss"); - } - } -} diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Serialization/SerializerTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Serialization/SerializerTests.cs index 1298a02e8..fbefa929e 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/Serialization/SerializerTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Serialization/SerializerTests.cs @@ -7,6 +7,7 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Serialization using Microsoft.Graph.Core.Models; using Microsoft.Graph.DotnetCore.Core.Test.TestModels; using Microsoft.Graph.DotnetCore.Core.Test.TestModels.ServiceModels; + using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Serialization.Json; using System; using System.Collections.Generic; @@ -146,15 +147,15 @@ public void DeserializeDateEnumerableValue() Assert.Equal(2, deserializedObject.DateCollection.Count()); Assert.True(deserializedObject.DateCollection.Any( date => - date.Year == now.Year && - date.Month == now.Month && - date.Day == now.Day), + date.Value.Year == now.Year && + date.Value.Month == now.Month && + date.Value.Day == now.Day), "Now date not found."); Assert.Contains(deserializedObject.DateCollection, date => - date.Year == tomorrow.Year && - date.Month == tomorrow.Month && - date.Day == tomorrow.Day); + date.Value.Year == tomorrow.Year && + date.Value.Month == tomorrow.Month && + date.Value.Day == tomorrow.Day); } @@ -165,7 +166,7 @@ public void DeserializeMemorableDatesValue() var tomorrow = now.AddDays(1); var recurrence = new DateTestClass { - DateCollection = new List { new Date(now.Year, now.Month, now.Day), new Date(tomorrow.Year, tomorrow.Month, tomorrow.Day) }, + DateCollection = new List { new Date(now.Year, now.Month, now.Day), new Date(tomorrow.Year, tomorrow.Month, tomorrow.Day) }, }; var derivedTypeInstance = new DerivedTypeClass @@ -199,9 +200,9 @@ public void DeserializeDateValue() var parseNode = this.parseNodeFactory.GetRootParseNode(CoreConstants.MimeTypeNames.Application.Json, memoryStream); var dateClass = parseNode.GetObjectValue(); - Assert.Equal(now.Year, dateClass.NullableDate.Year); - Assert.Equal(now.Month, dateClass.NullableDate.Month); - Assert.Equal(now.Day, dateClass.NullableDate.Day); + Assert.Equal(now.Year, dateClass.NullableDate.Value.Year); + Assert.Equal(now.Month, dateClass.NullableDate.Value.Month); + Assert.Equal(now.Day, dateClass.NullableDate.Value.Day); } [Fact] @@ -316,7 +317,7 @@ public void SerializeDateEnumerableValue() var recurrence = new DateTestClass { - DateCollection = new List { new Date(now.Year, now.Month, now.Day), new Date(tomorrow.Year, tomorrow.Month, tomorrow.Day) }, + DateCollection = new List { new Date(now.Year, now.Month, now.Day), new Date(tomorrow.Year, tomorrow.Month, tomorrow.Day) }, }; using var jsonSerializerWriter = new JsonSerializationWriter(); diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/TestModels/DateTestClass.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/TestModels/DateTestClass.cs index 523903218..0fc9459e4 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/TestModels/DateTestClass.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/TestModels/DateTestClass.cs @@ -4,6 +4,7 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.TestModels { + using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Abstractions.Serialization; using System; using System.Collections.Generic; @@ -17,12 +18,12 @@ public class DateTestClass: IParsable /// /// Gets or sets nullableDate. /// - public Date NullableDate { get; set; } + public Date? NullableDate { get; set; } /// /// Gets or sets dateCollection. /// - public IEnumerable DateCollection { get; set; } + public IEnumerable DateCollection { get; set; } /// /// Gets or sets InvalidType. @@ -48,8 +49,8 @@ public IDictionary> GetFieldDeserializers() { return new Dictionary> { - {"nullableDate", (o,n) => { (o as DateTestClass).NullableDate = new Date(n.GetDateTimeOffsetValue().Value.DateTime); } }, - {"dateCollection", (o,n) => { (o as DateTestClass).DateCollection = n.GetCollectionOfPrimitiveValues().Select(dateTimeOffset => new Date(dateTimeOffset.Value.DateTime) ); } }, + {"nullableDate", (o,n) => { (o as DateTestClass).NullableDate = n.GetDateValue(); } }, + {"dateCollection", (o,n) => { (o as DateTestClass).DateCollection = n.GetCollectionOfPrimitiveValues(); } }, {"invalidType", (o,n) => { (o as DateTestClass).InvalidType = n.GetIntValue(); } }, }; } @@ -62,8 +63,8 @@ public IDictionary> GetFieldDeserializers() public void Serialize(ISerializationWriter writer) { _ = writer ?? throw new ArgumentNullException(nameof(writer)); - writer.WriteStringValue("nullableDate", NullableDate?.ToString()); - writer.WriteCollectionOfPrimitiveValues("dateCollection", DateCollection?.Select( date => date.ToString())); + writer.WriteDateValue("nullableDate", NullableDate); + writer.WriteCollectionOfPrimitiveValues("dateCollection", DateCollection); writer.WriteIntValue("invalidType", InvalidType); writer.WriteAdditionalData(AdditionalData); }