Persist get-only and readonly members the way MessagePack does

The reflective registry now decides persistence from the same two facts
MessagePack 3.1.7's DynamicObjectResolver uses: whether it can assign a keyed
member after construction, and which constructor it picks.

- Every keyed public member is discovered, get-only properties included.
  MessagePack writes them all; the registry used to leave a get-only property
  out of the catalog and schema id even when a constructor filled it.
- MessagePack picks the [SerializationConstructor] if marked, otherwise the
  longest visible constructor whose parameters take the keyed members by
  position (probed: a longer non-matching constructor falls back to a shorter
  one). A member it cannot assign is read back only when its slot is below
  that constructor's parameter count.
- A readonly field is assignable under AllowPrivate (probed: round-trips with
  only a parameterless constructor), and otherwise only through the picked
  constructor. Keyed readonly fields that constructor fills are accepted.
- A keyed member MessagePack writes but never reads back is refused with a
  message naming the constructor position: a get-only property, a readonly
  field without AllowPrivate, or a non-public setter without AllowPrivate,
  when the picked constructor does not fill it (probed: loaded as default).
- An unkeyed public get-only property is refused; MessagePack throws "all
  public members must mark KeyAttribute or IgnoreMemberAttribute". The unkeyed
  message now names both fixes.

Schema ids change for five GameCult.Eve.Surface documents, whose keyed members
are all get-only properties filled by a [SerializationConstructor]:
gamecult.eve.surface, gamecult.eve.asset_catalog,
gamecult.eve.command_invocation, gamecult.eve.command_receipt and
gamecult.eve.provider_advertisement (EveProviderAdvertisementDocument). Their
catalogs gain those members. No .cc store under F:\Projects holds records of
these schemas: the gamecult.eve.provider_advertisement catalog in Gjallar's
scratch smoke stores belongs to GjallarProviderAdvertisementRecord, which has
no get-only members, and every other hit is prose or a git tree listing.

Consumer registration scan (CultLib src/tests/tools/samples, Ymir.Core,
Gjallar, AquaSynth Core/Faust, Mimir): 148 types register; the two rejections
(CounterState string key, BenchItem without [MessagePackObject]) are rejected
on 9139bf3 too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 00:21:21 +02:00
commit 5bd4e4ed6e
3 changed files with 195 additions and 45 deletions

View file

@ -16,6 +16,8 @@ namespace GameCult.Caching.Tests
public class DocumentShapeTests
{
private const string NonPublicSetter = "has a non-public set accessor; MessagePack writes it but never reads it back without AllowPrivate, so make the setter public or add [MessagePackObject(AllowPrivate = true)].";
private const string Unkeyed = "has no [Key]; MessagePack requires [Key(n)] or [IgnoreMember] on every public member, so mark it one or the other.";
private const string ReadonlyUnfilled = "is a readonly field that no constructor fills; MessagePack writes it but never reads it back, so give the constructor MessagePack calls a parameter at position {0}, drop readonly, or add [MessagePackObject(AllowPrivate = true)].";
private const string DivergentOverride ="overrides {0} with a different [Key] or [IgnoreMember]; MessagePack reads the base declaration's, so an override must repeat or omit them.";
private static readonly ModuleBuilder Rejected = AssemblyBuilder
@ -104,7 +106,7 @@ namespace GameCult.Caching.Tests
var type = Document("Unkeyed");
Field(type, "Name", typeof(string), Key(0));
Field(type, "Count", typeof(int));
Reject(type.CreateType()!, "Cult document Unkeyed member Count has no [Key]; every persisted member of a [CultDocument] type needs an explicit [Key(n)].");
Reject(type.CreateType()!, "Cult document Unkeyed member Count " + Unkeyed);
}
// MessagePack's DynamicObjectResolver reads the base declaration: it throws "key is duplicated" for this shape.
@ -218,6 +220,81 @@ namespace GameCult.Caching.Tests
Assert.That(Convert.ToHexString(Accept(new InternalSetterNote { Name = "n" })), Is.EqualTo("91A16E"));
}
// MessagePack fills a get-only property through the longest constructor whose parameters take the keyed members by position.
[Test]
public void GetOnlyPropertyFilledByConstructor()
{
Assert.That(Convert.ToHexString(Accept(new GetOnlyNote("n", 2))), Is.EqualTo("92A16E02"));
AssertSlots<GetOnlyNote>("0:Name", "1:Count");
}
// The three-parameter constructor has no member at position 2, so MessagePack falls back to the one-parameter constructor.
[Test]
public void ReadonlyFieldFilledByConstructor()
{
Assert.That(Convert.ToHexString(Accept(new ReadonlyNote("n") { Count = 3 })), Is.EqualTo("92A16E03"));
AssertSlots<ReadonlyNote>("0:Name", "1:Count");
}
// Under AllowPrivate MessagePack writes a readonly field directly; no constructor has to fill it.
[Test]
public void ReadonlyFieldUnderAllowPrivate()
{
var sample = new ReadonlyPrivateNote();
typeof(ReadonlyPrivateNote).GetField(nameof(ReadonlyPrivateNote.Name))!.SetValue(sample, "n");
Assert.That(Convert.ToHexString(Accept(sample)), Is.EqualTo("91A16E"));
Assert.That(((ReadonlyPrivateNote)CultDocumentMessagePackSerialization.DeserializeUntyped(
typeof(ReadonlyPrivateNote), Accept(sample), CultDocumentRegistry.ForTypes(new[] { typeof(ReadonlyPrivateNote) }))).Name, Is.EqualTo("n"));
AssertSlots<ReadonlyPrivateNote>("0:Name");
}
// MessagePack throws "all public members must mark KeyAttribute or IgnoreMemberAttribute" for this shape.
[Test]
public void UnkeyedGetOnlyProperty()
{
var type = Document("UnkeyedGetter");
Field(type, "Name", typeof(string), Key(0));
GetOnlyProperty(type, "Upper");
Reject(type.CreateType()!, "Cult document UnkeyedGetter member Upper " + Unkeyed);
}
// MessagePack writes Label and loads it back as the default: only the parameterless constructor exists.
[Test]
public void GetOnlyPropertyNoConstructorFills()
{
var type = Document("GetterUnfilled");
Field(type, "Name", typeof(string), Key(0));
GetOnlyProperty(type, "Label", Key(1));
Reject(type.CreateType()!, "Cult document GetterUnfilled member Label is a get-only property that no constructor fills; MessagePack writes it but never reads it back, so give the constructor MessagePack calls a parameter at position 1, add a setter, or mark it [IgnoreMember].");
}
// Without AllowPrivate MessagePack writes Name and loads it back as the field initializer's value.
[Test]
public void ReadonlyFieldNoConstructorFills()
{
var type = Document("ReadonlyUnfilled");
type.DefineField("Name", typeof(string), FieldAttributes.Public | FieldAttributes.InitOnly).SetCustomAttribute(Key(0));
Reject(type.CreateType()!, "Cult document ReadonlyUnfilled member Name " + string.Format(ReadonlyUnfilled, 0));
}
// MessagePack picks the one-parameter constructor, which fills Name but not Other; Other loads back as its default.
[Test]
public void ReadonlyFieldBeyondPickedConstructor()
{
var type = Document("ReadonlyBeyond");
var name = type.DefineField("Name", typeof(string), FieldAttributes.Public | FieldAttributes.InitOnly);
name.SetCustomAttribute(Key(0));
type.DefineField("Other", typeof(string), FieldAttributes.Public | FieldAttributes.InitOnly).SetCustomAttribute(Key(1));
var il = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] { typeof(string) }).GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes)!);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld, name);
il.Emit(OpCodes.Ret);
Reject(type.CreateType()!, "Cult document ReadonlyBeyond member Other " + string.Format(ReadonlyUnfilled, 1));
}
[Test]
public void StringKey()
{
@ -238,6 +315,23 @@ namespace GameCult.Caching.Tests
return payload;
}
private static void AssertSlots<T>(params string[] expected) where T : class =>
Assert.That(CultDocumentRegistry.ForTypes(new[] { typeof(T) }).GetRequired<T>().ToCatalogEntry().Members.Select(member => $"{member.Slot}:{member.MemberName}"), Is.EqualTo(expected));
private static void GetOnlyProperty(TypeBuilder type, string name, params CustomAttributeBuilder[] attributes)
{
var backing = type.DefineField("_" + name, typeof(string), FieldAttributes.Private);
backing.SetCustomAttribute(new CustomAttributeBuilder(typeof(IgnoreMemberAttribute).GetConstructor(Type.EmptyTypes)!, Array.Empty<object>()));
var getter = type.DefineMethod("get_" + name, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, typeof(string), Type.EmptyTypes);
var il = getter.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, backing);
il.Emit(OpCodes.Ret);
var property = type.DefineProperty(name, PropertyAttributes.None, typeof(string), null);
property.SetGetMethod(getter);
foreach (var attribute in attributes) property.SetCustomAttribute(attribute);
}
private static void Reject(Type type, string expected) =>
Assert.That(Assert.Throws<InvalidOperationException>(() => CultDocumentRegistry.ForTypes(new[] { type }))!.Message, Is.EqualTo(expected));
@ -341,6 +435,39 @@ namespace GameCult.Caching.Tests
[Key(0)] public string Name { get; internal set; } = "";
}
[CultDocument("shape.get_only", "shape.get_only.v1")]
[MessagePackObject]
public sealed class GetOnlyNote
{
public GetOnlyNote(string name, int count)
{
Name = name;
Count = count;
}
[Key(0)] public string Name { get; }
[Key(1)] public int Count { get; set; }
}
[CultDocument("shape.readonly", "shape.readonly.v1")]
[MessagePackObject]
public sealed class ReadonlyNote
{
public ReadonlyNote() => Name = "";
public ReadonlyNote(string name) => Name = name;
public ReadonlyNote(string name, int count, string extra) => Name = name;
[Key(0)] public readonly string Name;
[Key(1)] public int Count { get; set; }
}
[CultDocument("shape.readonly_private", "shape.readonly_private.v1")]
[MessagePackObject(AllowPrivate = true)]
internal sealed class ReadonlyPrivateNote
{
[Key(0)] public readonly string Name = "init";
}
[MessagePackObject]
public class OvrSameBase
{