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
15 changes: 11 additions & 4 deletions src/KubernetesClient/KubernetesJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
{
var iso8601TimeSpanString = XmlConvert.ToString(value); // XmlConvert for TimeSpan uses ISO8601, so delegate serialization to it
writer.WriteStringValue(iso8601TimeSpanString);

Check warning on line 27 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Dotnet build (ubuntu-latest)

In externally visible method 'void Iso8601TimeSpanConverter.Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 27 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

In externally visible method 'void Iso8601TimeSpanConverter.Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
}
}

Expand All @@ -34,27 +34,34 @@
private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffZ";
private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";

private const string RFC3339MicroWithOffsetFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK";
private const string RFC3339NanoWithOffsetFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK";
private const string RFC3339WithOffsetFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK";

private static readonly string[] StandardFormats = { RFC3339Format, RFC3339MicroFormat, RFC3339WithOffsetFormat, RFC3339MicroWithOffsetFormat };
private static readonly string[] NanoFormats = { RFC3339NanoFormat, RFC3339NanoWithOffsetFormat };

public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var str = reader.GetString();

if (DateTimeOffset.TryParseExact(str, new[] { RFC3339Format, RFC3339MicroFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
// Try standard formats first
if (DateTimeOffset.TryParseExact(str, StandardFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
{
return result;
}

// try RFC3339NanoLenient by trimming 1-9 digits to 7 digits
// Try RFC3339NanoLenient by trimming 1-9 digits to 7 digits
var originalstr = str;
str = Regex.Replace(str, @"\.\d+", m => (m.Value + "000000000").Substring(0, 7 + 1)); // 7 digits + 1 for the dot
if (DateTimeOffset.TryParseExact(str, new[] { RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
if (DateTimeOffset.TryParseExact(str, NanoFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}

throw new FormatException($"Unable to parse {originalstr} as RFC3339 RFC3339Micro or RFC3339Nano");
}


public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
// Output as RFC3339Micro
Expand All @@ -66,7 +73,7 @@
{
// No fractional seconds - use format without fractional part
var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture);
writer.WriteStringValue(basePart + "Z");

Check warning on line 76 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Dotnet build (ubuntu-latest)

In externally visible method 'void KubernetesDateTimeOffsetConverter.Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 76 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

In externally visible method 'void KubernetesDateTimeOffsetConverter.Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
}
else
{
Expand Down Expand Up @@ -180,7 +187,7 @@
public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null)
{
#if NET8_0_OR_GREATER
var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType());

Check warning on line 190 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Dotnet build (ubuntu-latest)

In externally visible method 'string KubernetesJson.Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 190 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

In externally visible method 'string KubernetesJson.Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
return JsonSerializer.Serialize(value, info);
#else
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
Expand All @@ -190,7 +197,7 @@
public static string Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null)
{
#if NET8_0_OR_GREATER
var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType());

Check warning on line 200 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Dotnet build (ubuntu-latest)

In externally visible method 'string KubernetesJson.Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 200 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

In externally visible method 'string KubernetesJson.Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
return JsonSerializer.Serialize(value, info);
#else
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
Expand All @@ -210,7 +217,7 @@
public static string Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null)
{
#if NET8_0_OR_GREATER
var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType());

Check warning on line 220 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Dotnet build (ubuntu-latest)

In externally visible method 'string KubernetesJson.Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 220 in src/KubernetesClient/KubernetesJson.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

In externally visible method 'string KubernetesJson.Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null)', validate parameter 'value' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
return JsonSerializer.Serialize(value, info);
#else
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
Expand Down
32 changes: 32 additions & 0 deletions tests/KubernetesClient.Tests/KubernetesJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,36 @@ public void DateTimeWithFractionalSecondsAlwaysHasSixDigits()
// Also verify it doesn't have 5 digits (which would fail in Kubernetes)
Assert.DoesNotContain("2025-11-17T22:52:34.96217Z", json);
}

[Fact]
public void DateTimeWithTimezoneOffsetParsesCorrectly()
{
// Test that datetime with explicit timezone offset (e.g., +00:00) is parsed correctly
// This uses the "last resort" general DateTimeOffset parsing fallback
var json = "{\"metadata\":{\"creationTimestamp\":\"2025-12-12T16:16:55.079293+00:00\"}}";

var secret = KubernetesJson.Deserialize<V1Secret>(json);

Assert.NotNull(secret.Metadata?.CreationTimestamp);
var dt = secret.Metadata.CreationTimestamp.Value;

Assert.Equal(2025, dt.Year);
Assert.Equal(12, dt.Month);
Assert.Equal(12, dt.Day);
Assert.Equal(16, dt.Hour);
Assert.Equal(16, dt.Minute);
Assert.Equal(55, dt.Second);
Assert.Equal(79, dt.Millisecond);
#if NET7_0_OR_GREATER
Assert.Equal(293, dt.Microsecond);
#endif
}

[Fact]
public void DateTimeNonRfc3339FormatIsRejected()
{
var json = "{\"metadata\":{\"creationTimestamp\":\"12/31/2023\"}}";

Assert.Throws<FormatException>(() => KubernetesJson.Deserialize<V1Secret>(json));
}
}
Loading