Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/abstractions/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ------------------------------------------------------------------------------

using System;
using System.Text.Json.Serialization;

namespace Microsoft.Kiota.Abstractions
{
Expand Down Expand Up @@ -42,8 +43,9 @@
/// <param name="year">The year.</param>
/// <param name="month">The month.</param>
/// <param name="day">The day of the month.</param>
[JsonConstructor]
public Date(int year, int month, int day)
: this(new DateTime(year, month, day))

Check warning on line 48 in src/abstractions/Date.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)

Check warning on line 48 in src/abstractions/Date.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)

Check warning on line 48 in src/abstractions/Date.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)
{
}

Expand Down
1 change: 1 addition & 0 deletions src/abstractions/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
<!-- NET 5 target to be removed on next major version-->
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0' or '$(TargetFramework)'== 'netStandard2.0' or '$(TargetFramework)' == 'netStandard2.1'">
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="[6.0,)" />
<PackageReference Include="System.Text.Json" Version="[6.0.10,)" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/abstractions/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ------------------------------------------------------------------------------

using System;
using System.Text.Json.Serialization;

namespace Microsoft.Kiota.Abstractions
{
Expand All @@ -17,7 +18,7 @@
/// </summary>
/// <param name="time">The <see cref="TimeOnly"/> to be converted.</param>
/// <returns>A new <see cref="Time"/> structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time.</returns>
public static implicit operator Time(TimeOnly time) => new(new DateTime(1, 1, 1, time.Hour, time.Minute, time.Second, time.Millisecond));

Check warning on line 21 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)

/// <summary>
/// Converts the supplied <see cref="Time"/> parameter to <see cref="TimeOnly"/>.
Expand All @@ -34,7 +35,7 @@
public bool Equals(Time other) => Hour == other.Hour && Minute == other.Minute && Second == other.Second;

/// <inheritdoc />
public override bool Equals(object? o) => (o is Time other) && Equals(other);

Check warning on line 38 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Rename parameter 'o' to 'obj' to match the base class declaration. (https://rules.sonarsource.com/csharp/RSPEC-927)

Check warning on line 38 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Rename parameter 'o' to 'obj' to match the base class declaration. (https://rules.sonarsource.com/csharp/RSPEC-927)

Check warning on line 38 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Rename parameter 'o' to 'obj' to match the base class declaration. (https://rules.sonarsource.com/csharp/RSPEC-927)

/// <inheritdoc />
public override int GetHashCode()
Expand All @@ -55,8 +56,9 @@
/// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param>
/// <param name="second">The second.</param>
[JsonConstructor]
public Time(int hour, int minute, int second)
: this(new DateTime(1, 1, 1, hour, minute, second))

Check warning on line 61 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)

Check warning on line 61 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)

Check warning on line 61 in src/abstractions/Time.cs

View workflow job for this annotation

GitHub Actions / Build

Provide the "DateTimeKind" when creating this object. (https://rules.sonarsource.com/csharp/RSPEC-6562)
{
}

Expand Down
55 changes: 55 additions & 0 deletions tests/abstractions/DateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Text.Json;
using Xunit;

namespace Microsoft.Kiota.Abstractions.Tests
{
public class DateTests
{
[Fact]
public void TestDateSerialization()
{
var date = new Date(2025, 10, 24);
var serialized = JsonSerializer.Serialize(date);

// System.Text.Json should serialize the Date object with its properties
Assert.Contains("\"Year\":2025", serialized);
Assert.Contains("\"Month\":10", serialized);
Assert.Contains("\"Day\":24", serialized);
}

[Fact]
public void TestDateDeserialization()
{
// This is the issue scenario - deserializing a Date object from JSON
var json = "{\"DateTime\":\"2025-10-24T10:18:54.5003283-05:00\",\"Year\":2025,\"Month\":10,\"Day\":24}";
var date = JsonSerializer.Deserialize<Date>(json);

Assert.Equal(2025, date.Year);
Assert.Equal(10, date.Month);
Assert.Equal(24, date.Day);
}

[Fact]
public void TestDateRoundTrip()
{
// Test that we can serialize and deserialize a Date object
var original = new Date(2025, 10, 24);
var serialized = JsonSerializer.Serialize(original);
var deserialized = JsonSerializer.Deserialize<Date>(serialized);

Assert.Equal(original.Year, deserialized.Year);
Assert.Equal(original.Month, deserialized.Month);
Assert.Equal(original.Day, deserialized.Day);
}

[Fact]
public void TestDateToString()
{
var date = new Date(2025, 10, 24);
var expectedString = "2025-10-24";

Assert.Equal(expectedString, date.ToString());
}
}
}
38 changes: 38 additions & 0 deletions tests/abstractions/TimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json;
using Xunit;

namespace Microsoft.Kiota.Abstractions.Tests
Expand All @@ -24,5 +25,42 @@ public void TestTimeToString()

Assert.Equal(expectedString, time.ToString());
}

[Fact]
public void TestTimeSerialization()
{
var time = new Time(10, 18, 54);
var serialized = JsonSerializer.Serialize(time);

// System.Text.Json should serialize the Time object with its properties
Assert.Contains("\"Hour\":10", serialized);
Assert.Contains("\"Minute\":18", serialized);
Assert.Contains("\"Second\":54", serialized);
}

[Fact]
public void TestTimeDeserialization()
{
// This is the issue scenario - deserializing a Time object from JSON
var json = "{\"DateTime\":\"2025-10-24T10:18:54.5090402-05:00\",\"Hour\":10,\"Minute\":18,\"Second\":54}";
var time = JsonSerializer.Deserialize<Time>(json);

Assert.Equal(10, time.Hour);
Assert.Equal(18, time.Minute);
Assert.Equal(54, time.Second);
}

[Fact]
public void TestTimeRoundTrip()
{
// Test that we can serialize and deserialize a Time object
var original = new Time(10, 18, 54);
var serialized = JsonSerializer.Serialize(original);
var deserialized = JsonSerializer.Deserialize<Time>(serialized);

Assert.Equal(original.Hour, deserialized.Hour);
Assert.Equal(original.Minute, deserialized.Minute);
Assert.Equal(original.Second, deserialized.Second);
}
}
}
Loading