fix: add input validation and error-path tests for CompactNordPoolDayPrice

Made-with: Cursor
This commit is contained in:
Marius
2026-03-19 14:38:41 +02:00
parent 5a4a4ed001
commit b4e465573f
2 changed files with 48 additions and 7 deletions
+12 -5
View File
@@ -6,20 +6,24 @@ namespace NordPool.Models;
[JsonConverter(typeof(CompactNordPoolDayPriceConverter))]
public readonly struct CompactNordPoolDayPrice
{
private const long NullTimestampSentinel = 0L;
public long Start { get; init; }
public long End { get; init; }
public double? Price { get; init; }
public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice)
{
ArgumentNullException.ThrowIfNull(dayPrice);
return new CompactNordPoolDayPrice
{
Start = dayPrice.Start.HasValue
? new DateTimeOffset(dayPrice.Start.Value, TimeSpan.Zero).ToUnixTimeSeconds()
: 0L,
: NullTimestampSentinel,
End = dayPrice.End.HasValue
? new DateTimeOffset(dayPrice.End.Value, TimeSpan.Zero).ToUnixTimeSeconds()
: 0L,
: NullTimestampSentinel,
Price = dayPrice.Price
};
}
@@ -32,13 +36,16 @@ internal sealed class CompactNordPoolDayPriceConverter : JsonConverter<CompactNo
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException("Expected start of array.");
reader.Read();
if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
throw new JsonException("Expected number for Start at position 0.");
var start = reader.GetInt64();
reader.Read();
if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
throw new JsonException("Expected number for End at position 1.");
var end = reader.GetInt64();
reader.Read();
if (!reader.Read() || (reader.TokenType != JsonTokenType.Number && reader.TokenType != JsonTokenType.Null))
throw new JsonException("Expected number or null for Price at position 2.");
double? price = reader.TokenType == JsonTokenType.Null ? null : reader.GetDouble();
reader.Read();