From 99d2702c13e78725dd2f5279bf6c2c40a4ac8e0e Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 10 Apr 2026 09:33:00 +0200 Subject: [PATCH] refactor(core): harden PluginManifest error model Addresses code quality review of c082ecf: - Require takes a literal JSON field name, no more fragile PascalCase->camelCase transform - Parse_MissingRequiredField_Throws asserts exact message, not substring - Remove unused using System.Text.Json.Serialization --- src/AcDream.Core/Plugins/PluginManifest.cs | 14 ++++++-------- .../Plugins/PluginManifestTests.cs | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/AcDream.Core/Plugins/PluginManifest.cs b/src/AcDream.Core/Plugins/PluginManifest.cs index 446178c..da16dcf 100644 --- a/src/AcDream.Core/Plugins/PluginManifest.cs +++ b/src/AcDream.Core/Plugins/PluginManifest.cs @@ -1,5 +1,4 @@ using System.Text.Json; -using System.Text.Json.Serialization; namespace AcDream.Core.Plugins; @@ -26,10 +25,10 @@ public sealed record PluginManifest( if (dto is null) throw new PluginManifestException("manifest is empty"); - Require(dto.Id, nameof(dto.Id)); - Require(dto.DisplayName, nameof(dto.DisplayName)); - Require(dto.Version, nameof(dto.Version)); - Require(dto.EntryDll, nameof(dto.EntryDll)); + Require(dto.Id, "id"); + Require(dto.DisplayName, "displayName"); + Require(dto.Version, "version"); + Require(dto.EntryDll, "entryDll"); if (dto.ApiVersion <= 0) throw new PluginManifestException("apiVersion must be >= 1"); @@ -42,11 +41,10 @@ public sealed record PluginManifest( dto.Dependencies ?? Array.Empty()); } - private static void Require(string? value, string fieldName) + private static void Require(string? value, string jsonFieldName) { if (string.IsNullOrWhiteSpace(value)) - throw new PluginManifestException( - $"missing required field: {char.ToLowerInvariant(fieldName[0])}{fieldName[1..]}"); + throw new PluginManifestException($"missing required field: {jsonFieldName}"); } private static readonly JsonSerializerOptions JsonOptions = new() diff --git a/tests/AcDream.Core.Tests/Plugins/PluginManifestTests.cs b/tests/AcDream.Core.Tests/Plugins/PluginManifestTests.cs index 180eeee..cfe08bc 100644 --- a/tests/AcDream.Core.Tests/Plugins/PluginManifestTests.cs +++ b/tests/AcDream.Core.Tests/Plugins/PluginManifestTests.cs @@ -34,7 +34,7 @@ public class PluginManifestTests """; var ex = Assert.Throws(() => PluginManifest.Parse(json)); - Assert.Contains("displayName", ex.Message); + Assert.Equal("missing required field: displayName", ex.Message); } [Fact]