The API's JSON is generated at build time, and no answer is an anonymous object
Build / Build (push) Successful in 36s
Build / Build (push) Successful in 36s
Every answer of the Mastodon API and the client API now has its JSON shape generated when the server is built, instead of worked out by reflection while it runs; the bytes sent are unchanged. The small answers that were anonymous objects (the instance documents, notification policies, reports, lists, tags, trends, errors and the empty answers) are named types. A type without generated metadata fails at once in development and on the pasture, and in production is logged and still answered. Three lists that were sent as lazy sequences (moderation reports, the published domain blocks and the languages) are now built first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
8a1e4e419b
commit
f3fdeaf038
41 files changed
+1333
-266
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
using PrivaPub.Api.Mastodon.Entities;
|
||||
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||
using PrivaPub.ClientModels;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Json
|
||||
{
|
||||
public static class PrivaPubJson
|
||||
{
|
||||
public static JsonSerializerOptions Web { get; } = Chained(new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||
|
||||
public static JsonSerializerOptions Plain { get; } = Chained(new JsonSerializerOptions());
|
||||
|
||||
public static JsonSerializerOptions Chained(JsonSerializerOptions options)
|
||||
{
|
||||
Chain(options.TypeInfoResolverChain);
|
||||
return options;
|
||||
}
|
||||
|
||||
public static void Chain(IList<IJsonTypeInfoResolver> chain)
|
||||
{
|
||||
var kept = chain.Where(r => r is not DefaultJsonTypeInfoResolver and not UnregisteredJson and not ClientModelsJsonContext and not ServerJsonContext).ToList();
|
||||
chain.Clear();
|
||||
chain.Add(ClientModelsJsonContext.Default);
|
||||
chain.Add(ServerJsonContext.Default);
|
||||
foreach (var resolver in kept)
|
||||
chain.Add(resolver);
|
||||
chain.Add(UnregisteredJson.Instance);
|
||||
}
|
||||
|
||||
public static void Check(IHostEnvironment environment)
|
||||
{
|
||||
UnregisteredJson.Strict = !environment.IsProduction();
|
||||
if (environment.IsProduction())
|
||||
return;
|
||||
var missing = Missing(ClientModelsJsonContext.Default, typeof(WebResult).Assembly, "PrivaPub.ClientModels")
|
||||
.Concat(Missing(MastodonJsonContext.Default, typeof(Account).Assembly, "PrivaPub.Api.Mastodon.Entities"))
|
||||
.ToList();
|
||||
if (missing.Count > 0)
|
||||
throw new InvalidOperationException("Not registered in a JsonSerializerContext (HR0306): " + string.Join(", ", missing));
|
||||
}
|
||||
|
||||
static IEnumerable<string> Missing(IJsonTypeInfoResolver context, Assembly assembly, string space) =>
|
||||
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)
|
||||
&& t.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
|
||||
.Where(t => context.GetTypeInfo(t, new JsonSerializerOptions()) == null)
|
||||
.Select(t => t.FullName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Json
|
||||
{
|
||||
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(ProblemDetails))]
|
||||
[JsonSerializable(typeof(ValidationProblemDetails))]
|
||||
[JsonSerializable(typeof(ViewBuild))]
|
||||
[JsonSerializable(typeof(Dictionary<string, object>))]
|
||||
[JsonSerializable(typeof(Dictionary<string, string[]>))]
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(bool))]
|
||||
[JsonSerializable(typeof(int))]
|
||||
[JsonSerializable(typeof(long))]
|
||||
[JsonSerializable(typeof(double))]
|
||||
public partial class ServerJsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
using Serilog;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Json
|
||||
{
|
||||
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))
|
||||
Log.ForContext<UnregisteredJson>().Error("{Type} is serialized without a JsonSerializerContext registration (HR0306)", type.FullName);
|
||||
return Reflection.GetTypeInfo(type, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace PrivaPub.Infrastructure.Json
|
||||
{
|
||||
public class ViewBuild
|
||||
{
|
||||
public string Commit { get; set; }
|
||||
public string BuildRef { get; set; }
|
||||
public string BuiltAt { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Json;
|
||||
using PrivaPub.Models;
|
||||
|
||||
using System.Globalization;
|
||||
@@ -53,7 +54,7 @@ namespace PrivaPub.Infrastructure.Push
|
||||
["aud"] = audience,
|
||||
["exp"] = new DateTimeOffset(now + TokenLifetime).ToUnixTimeSeconds(),
|
||||
["sub"] = subject
|
||||
}));
|
||||
}, PrivaPubJson.Plain));
|
||||
var signed = header + "." + claims;
|
||||
using var key = ECDsa.Create(_parameters);
|
||||
var signature = key.SignData(Encoding.ASCII.GetBytes(signed), HashAlgorithmName.SHA256, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
|
||||
|
||||
@@ -13,6 +13,7 @@ using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Infrastructure.Json;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.User;
|
||||
@@ -114,7 +115,7 @@ namespace PrivaPub.Infrastructure.Push
|
||||
["icon"] = mapped.Account?.AvatarStatic ?? mapped.Account?.Avatar,
|
||||
["title"] = Title(notification, mapped),
|
||||
["body"] = Body(mapped)
|
||||
});
|
||||
}, PrivaPubJson.Plain);
|
||||
}
|
||||
|
||||
var key = await vapid.Key(token);
|
||||
|
||||
Reference in new issue
Block a user