diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e48571c..7f6de877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.9.5] - 2024-06-03 + +### Added + +- Kiota's `Date` and `Time` types are interchangeable with the system `DateOnly` and `TimeOnly` types through implicit conversion operators. + ## [1.9.4] - 2024-05-31 ### Changed diff --git a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs index df3e4812..56604a6f 100644 --- a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs @@ -260,6 +260,52 @@ public void SetsPathParametersOfTimeType() // Assert Assert.Contains($"%24time=06%3A00%3A00", requestInfo.URI.OriginalString); } +#if NET6_0_OR_GREATER + [Fact] + public void SetsPathParametersOfDateOnlyType() + { + // Arrange as the request builders would + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + UrlTemplate = "http://localhost/users{?%24date}" + }; + + // Act + var date = new DateOnly(2023, 10, 26); + var pathParameters = new Dictionary + { + { "%24date", date } + }; + + requestInfo.PathParameters = pathParameters; + + // Assert + Assert.Contains($"%24date=2023-10-26", requestInfo.URI.OriginalString); + } + [Fact] + public void SetsPathParametersOfTimeOnlyType() + { + // Arrange as the request builders would + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + UrlTemplate = "http://localhost/users{?%24time}" + }; + + // Act + var time = new TimeOnly(6, 0, 0); + var pathParameters = new Dictionary + { + { "%24time", time } + }; + + requestInfo.PathParameters = pathParameters; + + // Assert + Assert.Contains($"%24time=06%3A00%3A00", requestInfo.URI.OriginalString); + } +#endif [Fact] public void CurrentCultureDoesNotAffectTimeSerialization() { @@ -663,14 +709,54 @@ public void SetsTimeValuesInQueryParameters() }; // Act - var date1 = new Time(10, 0, 0); - var date2 = new Time(11, 1, 1); + var time1 = new Time(10, 0, 0); + var time2 = new Time(11, 1, 1); + + requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { time1, time2 } }); + + // Assert + Assert.Equal("http://localhost/me?items=10%3A00%3A00,11%3A01%3A01", requestInfo.URI.OriginalString); + } + +#if NET6_0_OR_GREATER + [Fact] + public void SetsDateOnlyValuesInQueryParameters() + { + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + UrlTemplate = "http://localhost/me{?items}" + }; + + // Act + Date date1 = new DateOnly(2022, 8, 1); + DateOnly date2 = new Date(2022, 8, 2); requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { date1, date2 } }); + // Assert + Assert.Equal("http://localhost/me?items=2022-08-01,2022-08-02", requestInfo.URI.OriginalString); + } + + [Fact] + public void SetsTimeOnlyValuesInQueryParameters() + { + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + UrlTemplate = "http://localhost/me{?items}" + }; + + // Act + Time time1 = new TimeOnly(10, 0, 0); + TimeOnly time2 = new Time(11, 1, 1); + + requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { time1, time2 } }); + // Assert Assert.Equal("http://localhost/me?items=10%3A00%3A00,11%3A01%3A01", requestInfo.URI.OriginalString); } +#endif [Fact] public void SetsGuidValuesInQueryParameters() diff --git a/src/Date.cs b/src/Date.cs index ebd09784..d78a20fd 100644 --- a/src/Date.cs +++ b/src/Date.cs @@ -11,6 +11,22 @@ namespace Microsoft.Kiota.Abstractions /// public struct Date { +#if NET6_0_OR_GREATER + /// + /// Converts the supplied parameter to . + /// + /// The to be converted. + /// A new structure whose years, months and days are equal to those of the supplied date. + public static implicit operator Date(DateOnly date) => new(date.Year, date.Month, date.Day); + + /// + /// Converts the supplied parameter to . + /// + /// The to be converted. + /// A new structure whose years, months and days are equal to those of the supplied date. + public static implicit operator DateOnly(Date date) => new(date.Year, date.Month, date.Day); +#endif + /// /// Create a new Date object from a object /// diff --git a/src/Microsoft.Kiota.Abstractions.csproj b/src/Microsoft.Kiota.Abstractions.csproj index 6d4a9f69..ebd5f893 100644 --- a/src/Microsoft.Kiota.Abstractions.csproj +++ b/src/Microsoft.Kiota.Abstractions.csproj @@ -15,7 +15,7 @@ https://aka.ms/kiota/docs true true - 1.9.4 + 1.9.5 true false diff --git a/src/RequestInformation.cs b/src/RequestInformation.cs index 478c9800..493420f4 100644 --- a/src/RequestInformation.cs +++ b/src/RequestInformation.cs @@ -124,6 +124,10 @@ public Uri URI bool boolean => boolean.ToString().ToLower(),// pass in a lowercase string as the final url will be uppercase due to the way ToString() works for booleans DateTimeOffset dateTimeOffset => dateTimeOffset.ToString("o"),// Default to ISO 8601 for datetimeoffsets in the url. DateTime dateTime => dateTime.ToString("o"),// Default to ISO 8601 for datetimes in the url. +#if NET6_0_OR_GREATER + DateOnly dateOnly => dateOnly.ToString("yyyy-MM-dd"),// Default to ISO 8601 for dateonlys in the url. + TimeOnly timeOnly => timeOnly.ToString(@"HH\:mm\:ss"),// Default to ISO 8601 for timeonlys in the url. +#endif Guid guid => guid.ToString("D"),// Default of 32 digits separated by hyphens Date date => date.ToString(), //Default to string format of the custom date object Time time => time.ToString(), //Default to string format of the custom time object diff --git a/src/Time.cs b/src/Time.cs index ee368731..fc534f5d 100644 --- a/src/Time.cs +++ b/src/Time.cs @@ -11,6 +11,21 @@ namespace Microsoft.Kiota.Abstractions /// public struct Time { +#if NET6_0_OR_GREATER + /// + /// Converts the supplied parameter to . + /// + /// The to be converted. + /// A new structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time. + public static implicit operator Time(TimeOnly time) => new(new DateTime(1, 1, 1, time.Hour, time.Minute, time.Second, time.Millisecond)); + + /// + /// Converts the supplied parameter to . + /// + /// The to be converted. + /// A new structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time. + public static implicit operator TimeOnly(Time time) => new(time.DateTime.Hour, time.DateTime.Minute, time.DateTime.Second, time.DateTime.Millisecond); +#endif /// /// Create a new Time from hours, minutes, and seconds. ///