The client's JSON comes from generated metadata only
Build / Build (push) Successful in 32s

PrivaPub's own types are read through the shared ClientModels context, and a type without generated metadata no longer
falls back to reflection silently: it fails at once in development and on the pasture, and is reported on the console
elsewhere. Startup outside production checks that every PrivaPub and Mastodon type the client knows has its metadata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-11-10 07:28:09 +01:00
1 parent 3bc511421e
commit dca046e27d
6 files changed
+70 -105

No files matched your search

+3 -4
View File
@@ -2,7 +2,6 @@
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using decePubClient.Models;
using decePubClient.Services;
@@ -22,13 +21,13 @@ namespace decePubClient.Helpers
/// <summary>
/// The JSON options for /clientapi payloads and localStorage: web defaults, enums by name as PrivaPub writes them
/// (numbers still read), ClientJsonContext first, reflection after.
/// (numbers still read), ClientJsonContext first, then PrivaPub.ClientModels' own context.
/// </summary>
public static JsonSerializerOptions DefaultSerializer { get; } = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter() },
TypeInfoResolver = JsonTypeInfoResolver.Combine(ClientJsonContext.Default, new DefaultJsonTypeInfoResolver())
TypeInfoResolverChain = { ClientJsonContext.Default, ClientModelsJsonContext.Default, UnregisteredJson.Instance }
};
/// <summary>The JSON options for PrivaPub's Mastodon API and /oauth: snake_case, MastodonJsonContext first.</summary>
@@ -37,7 +36,7 @@ namespace decePubClient.Helpers
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IgnoreReadOnlyProperties = true,
TypeInfoResolver = JsonTypeInfoResolver.Combine(MastodonJsonContext.Default, new DefaultJsonTypeInfoResolver())
TypeInfoResolverChain = { MastodonJsonContext.Default, UnregisteredJson.Instance }
};
/// <summary>What a cancelled request turns into: HTTP 410, which AddResponseError ignores.</summary>
+48
View File
@@ -0,0 +1,48 @@
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using decePubClient.Models.Mastodon;
using PrivaPub.ClientModels;
namespace decePubClient.Helpers
{
public sealed class UnregisteredJson : IJsonTypeInfoResolver
{
public static readonly UnregisteredJson Instance = new();
static readonly DefaultJsonTypeInfoResolver Reflection = new();
static readonly ConcurrentDictionary<Type, byte> Reported = new();
public static bool Strict { get; set; }
public JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
if (Strict)
throw new NotSupportedException($"{type.FullName} is not registered in a JsonSerializerContext (HR0306).");
if (Reported.TryAdd(type, 0))
Console.Error.WriteLine($"{type.FullName} is serialized without a JsonSerializerContext registration (HR0306)");
return Reflection.GetTypeInfo(type, options);
}
public static void Check(bool strict)
{
Strict = strict;
if (!strict)
return;
var missing = Missing(ClientModelsJsonContext.Default, typeof(WebResult), "PrivaPub.ClientModels")
.Concat(Missing(MastodonJsonContext.Default, typeof(Status), "decePubClient.Models.Mastodon"))
.ToList();
if (missing.Count > 0)
throw new InvalidOperationException("Not registered in a JsonSerializerContext (HR0306): " + string.Join(", ", missing));
}
static IEnumerable<string> Missing(IJsonTypeInfoResolver context, Type anchor, string space) =>
anchor.Assembly.GetTypes()
.Where(t => t.IsClass && t.IsPublic && !t.IsAbstract && !t.IsGenericTypeDefinition && t.Namespace is { } ns && ns.StartsWith(space, StringComparison.Ordinal)
&& !ns.EndsWith(".Resources", StringComparison.Ordinal) && !typeof(Attribute).IsAssignableFrom(t) && !typeof(JsonSerializerContext).IsAssignableFrom(t))
.Where(t => context.GetTypeInfo(t, new JsonSerializerOptions()) == null)
.Select(t => t.FullName);
}
}