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
This commit is contained in:
Erik 2026-04-10 09:33:00 +02:00
parent c082ecf36a
commit 99d2702c13
2 changed files with 7 additions and 9 deletions

View file

@ -1,5 +1,4 @@
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
namespace AcDream.Core.Plugins; namespace AcDream.Core.Plugins;
@ -26,10 +25,10 @@ public sealed record PluginManifest(
if (dto is null) if (dto is null)
throw new PluginManifestException("manifest is empty"); throw new PluginManifestException("manifest is empty");
Require(dto.Id, nameof(dto.Id)); Require(dto.Id, "id");
Require(dto.DisplayName, nameof(dto.DisplayName)); Require(dto.DisplayName, "displayName");
Require(dto.Version, nameof(dto.Version)); Require(dto.Version, "version");
Require(dto.EntryDll, nameof(dto.EntryDll)); Require(dto.EntryDll, "entryDll");
if (dto.ApiVersion <= 0) if (dto.ApiVersion <= 0)
throw new PluginManifestException("apiVersion must be >= 1"); throw new PluginManifestException("apiVersion must be >= 1");
@ -42,11 +41,10 @@ public sealed record PluginManifest(
dto.Dependencies ?? Array.Empty<string>()); dto.Dependencies ?? Array.Empty<string>());
} }
private static void Require(string? value, string fieldName) private static void Require(string? value, string jsonFieldName)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
throw new PluginManifestException( throw new PluginManifestException($"missing required field: {jsonFieldName}");
$"missing required field: {char.ToLowerInvariant(fieldName[0])}{fieldName[1..]}");
} }
private static readonly JsonSerializerOptions JsonOptions = new() private static readonly JsonSerializerOptions JsonOptions = new()

View file

@ -34,7 +34,7 @@ public class PluginManifestTests
"""; """;
var ex = Assert.Throws<PluginManifestException>(() => PluginManifest.Parse(json)); var ex = Assert.Throws<PluginManifestException>(() => PluginManifest.Parse(json));
Assert.Contains("displayName", ex.Message); Assert.Equal("missing required field: displayName", ex.Message);
} }
[Fact] [Fact]