Fix tests
CI / backend (push) Failing after 8m50s
CI / frontend (push) Failing after 1m45s

This commit is contained in:
voltsrage
2026-08-05 20:21:23 +08:00
parent b44954545b
commit a6a491a2da
16 changed files with 104 additions and 15 deletions
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertSeverityJsonConverter : JsonConverter<AlertSeverity>
{
public override AlertSeverity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertSeverityExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, AlertSeverityExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertSeverity value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertStatusJsonConverter : JsonConverter<AlertStatus>
{
public override AlertStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertStatusExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, AlertStatusExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertStatus value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertTypeJsonConverter : JsonConverter<AlertType>
{
public override AlertType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertTypeExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, AlertTypeExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertType value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AuditActionJsonConverter : JsonConverter<AuditAction>
{
public override AuditAction Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AuditActionExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, AuditActionExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AuditAction value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -17,7 +17,7 @@ public sealed class BloodTypeJsonConverter : JsonConverterFactory
private sealed class BloodTypeConverter : JsonConverter<BloodType>
{
public override BloodType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> BloodTypeExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, BloodTypeExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, BloodType value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -30,7 +30,7 @@ public sealed class BloodTypeJsonConverter : JsonConverterFactory
if (reader.TokenType == JsonTokenType.Null)
return null;
return BloodTypeExtensions.FromDbString(reader.GetString()!);
return DbStringEnumJson.Read(ref reader, BloodTypeExtensions.FromDbString);
}
public override void Write(Utf8JsonWriter writer, BloodType? value, JsonSerializerOptions options)
@@ -0,0 +1,39 @@
using System.Text.Json;
/// <summary>
/// Shared read helper for converters that write DB-wire strings (e.g. "ICU")
/// but must also accept default System.Text.Json client payloads (numeric enums)
/// and PascalCase enum names.
/// </summary>
internal static class DbStringEnumJson
{
public static TEnum Read<TEnum>(ref Utf8JsonReader reader, Func<string, TEnum> fromDbString)
where TEnum : struct, Enum
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
if (reader.TryGetInt32(out var numeric)
&& Enum.IsDefined(typeof(TEnum), numeric))
return (TEnum)Enum.ToObject(typeof(TEnum), numeric);
throw new JsonException($"Invalid numeric value for {typeof(TEnum).Name}.");
case JsonTokenType.String:
var raw = reader.GetString()
?? throw new JsonException($"Null string for {typeof(TEnum).Name}.");
try
{
return fromDbString(raw);
}
catch (ArgumentOutOfRangeException) when (
Enum.TryParse(raw, ignoreCase: true, out TEnum byName))
{
return byName;
}
default:
throw new JsonException(
$"Unexpected token {reader.TokenType} when parsing {typeof(TEnum).Name}.");
}
}
}
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class DepartmentJsonConverter : JsonConverter<Department>
{
public override Department Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> DepartmentExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, DepartmentExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, Department value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class ObservationSourceJsonConverter : JsonConverter<ObservationSource>
{
public override ObservationSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> ObservationSourceExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, ObservationSourceExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, ObservationSource value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class SepsisBundleComplianceStatusJsonConverter : JsonConverter<SepsisBundleComplianceStatus>
{
public override SepsisBundleComplianceStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> SepsisBundleComplianceStatusExtensions.FromDbString(reader.GetString()!);
=> DbStringEnumJson.Read(ref reader, SepsisBundleComplianceStatusExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, SepsisBundleComplianceStatus value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString());