Union guard: name an out-of-range or non-integer arm key, pin interface unions, name the schema on a variant refusal, changelog the exception type

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-10 23:48:49 +02:00
commit 3042757701
4 changed files with 204 additions and 6 deletions

View file

@ -37,11 +37,36 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol
{
// Peek only the arm's first two tokens; any other shape is the inner formatter's to refuse.
var peek = reader.CreatePeekReader();
if (!peek.TryReadNil() && peek.NextMessagePackType == MessagePackType.Array && peek.ReadArrayHeader() == 2 &&
peek.NextMessagePackType == MessagePackType.Integer && peek.ReadInt32() is var key && !keys.Contains(key))
throw new MessagePackSerializationException(
$"Union {typeof(T).FullName} has no arm for key {key} (slot 0 of the arm); declared keys: {string.Join(", ", keys.OrderBy(k => k))}.");
if (!peek.TryReadNil() && peek.NextMessagePackType == MessagePackType.Array && peek.ReadArrayHeader() == 2)
{
var declared = string.Join(", ", keys.OrderBy(k => k));
if (peek.NextMessagePackType != MessagePackType.Integer)
throw new MessagePackSerializationException($"Union {typeof(T).FullName} has a non-integer arm key (slot 0 of the arm); declared keys: {declared}.");
if (!TryReadKey(ref peek, out var key))
throw new MessagePackSerializationException($"Union {typeof(T).FullName} has an arm key outside Int32 (slot 0 of the arm); declared keys: {declared}.");
if (!keys.Contains(key))
throw new MessagePackSerializationException($"Union {typeof(T).FullName} has no arm for key {key} (slot 0 of the arm); declared keys: {declared}.");
}
return inner.Deserialize(ref reader, options);
}
// An arm key is an Int32; a wider or unsigned-64 integer is out of range, never an OverflowException.
private static bool TryReadKey(ref MessagePackReader reader, out int key)
{
key = 0;
if (reader.NextCode == MessagePackCode.UInt64)
{
var wide = reader.ReadUInt64();
if (wide > int.MaxValue) return false;
key = (int)wide;
return true;
}
var value = reader.ReadInt64();
if (value < int.MinValue || value > int.MaxValue) return false;
key = (int)value;
return true;
}
}
}

View file

@ -2819,7 +2819,7 @@ namespace GameCult.Caching
{
// A value that no longer decodes refuses exactly as a plain record holding it does.
throw new InvalidOperationException(
$"Variant {key} overrides '{member.Member.Name}' (slot {slot}) with a value that does not decode as {member.MemberType.Name}: " +
$"Variant {key} (schema '{descriptor.SchemaId}') overrides '{member.Member.Name}' (slot {slot}) with a value that does not decode as {member.MemberType.Name}: " +
exception.GetBaseException().Message, exception);
}

View file

@ -20,7 +20,7 @@ namespace GameCult.Caching.Tests
{
private const string Canary = "CANARY-9d1e47";
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(GuardDoc) });
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(GuardDoc), typeof(ShapeDoc) });
private delegate void Emit(ref MessagePackWriter writer);
@ -306,6 +306,154 @@ namespace GameCult.Caching.Tests
[MessagePackObject] public sealed class Overridden { [Key(0)] public int N; }
// An arm key outside Int32 or not an integer is the guard's own refusal, naming the union and the arm position, not an
// OverflowException from reading it and not an echo of the key or the payload.
private static Emit KeyedArm(Emit key) => (ref MessagePackWriter w) =>
{
w.WriteArrayHeader(2); key(ref w);
w.WriteArrayHeader(1); w.Write(Canary);
};
[TestCase(2147483648L, "2147483648")]
[TestCase(-2147483649L, "2147483649")]
[TestCase(4294967295L, "4294967295")]
public void ArmKeyOutsideInt32IsRefusedNamingTheUnion(long key, string digits) =>
AssertKeyRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.Write(key))), "outside Int32", digits);
[Test]
public void ArmKeyOfUint64MaxIsRefusedNamingTheUnion() =>
AssertKeyRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.Write(ulong.MaxValue))), "outside Int32", "18446744073709551615");
[Test]
public void NegativeArmKeyInInt32IsAnUnknownKey() =>
AssertRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.Write(-5))), NodeName, -5, "3, 5, 8, 9");
[Test]
public void NonIntegerArmKeyIsRefusedNamingTheUnion()
{
AssertKeyRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.Write("three"))), "non-integer", "three");
AssertKeyRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.Write(3.5))), "non-integer", "3.5");
AssertKeyRefused(Doc(KeyedArm((ref MessagePackWriter w) => w.WriteNil())), "non-integer", "nil");
}
private static void AssertKeyRefused(byte[] payload, string shape, string echoed)
{
var exception = Decode(payload);
var message = Chain(exception);
Assert.Multiple(() =>
{
Assert.That(message, Does.Contain($"Union {NodeName} has"));
Assert.That(message, Does.Contain(shape));
Assert.That(message, Does.Contain("(slot 0 of the arm)"));
Assert.That(message, Does.Contain("declared keys: 3, 5, 8, 9."));
Assert.That(message, Does.Not.Contain(Canary));
Assert.That(message, Does.Not.Contain(echoed), "the offending key is not echoed");
for (var cause = exception; cause != null; cause = cause.InnerException)
Assert.That(cause, Is.Not.InstanceOf<OverflowException>());
});
}
// An interface is a union like an abstract class: the guard reads the interface's own [Union] attributes.
private static readonly string ShapeName = typeof(IShape).FullName!;
// ShapeDoc: slots [id, shape, shapes].
private static byte[] Shapes(Emit shape, Emit? shapes = null) => Raw((ref MessagePackWriter w) =>
{
w.WriteArrayHeader(3);
w.Write("s1");
shape(ref w);
(shapes ?? List())(ref w);
});
private static Emit CircleArm(int key, int radius) => InnerArm(key, radius);
[TestCase(1)]
[TestCase(3)]
[TestCase(9)]
public void UnknownArmOfAnInterfaceUnionRefused(int key)
{
foreach (var payload in new[] { Shapes(CircleArm(key, 1)), Shapes(Nil, List(CircleArm(0, 1), CircleArm(key, 1))) })
{
Exception? thrown = null;
try { CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(ShapeDoc), payload, Registry); }
catch (Exception exception) { thrown = exception; }
Assert.That(thrown, Is.Not.Null, "an unknown arm of an interface union was accepted");
var message = Chain(thrown!);
Assert.That(message, Does.Contain($"Union {ShapeName} has no arm for key {key} "));
Assert.That(message, Does.Contain("declared keys: 0, 2."));
}
}
[Test]
public void UnnamedImplementationOfAnInterfaceUnionWriteRefused()
{
foreach (var doc in new[] { new ShapeDoc { Shape = new Triangle() }, new ShapeDoc { Shapes = { new Circle(), new Triangle() } }, new ShapeDoc { Shape = new BigCircle() } })
{
var message = Chain(Assert.Catch<MessagePackSerializationException>(
() => CultDocumentMessagePackSerialization.SerializeUntyped(doc, typeof(ShapeDoc), Registry))!);
Assert.That(message, Does.Contain("is not an arm of union").And.Contain(ShapeName));
}
}
[Test]
public void KnownArmsOfAnInterfaceUnionByteIdentical()
{
var doc = new ShapeDoc { Id = "s1", Shape = new Square { N = 4 }, Shapes = { new Circle { N = 2 }, null! } };
var expected = Shapes(CircleArm(2, 4), List(CircleArm(0, 2), Nil));
Assert.That(Convert.ToHexString(CultDocumentMessagePackSerialization.SerializeUntyped(doc, typeof(ShapeDoc), Registry)), Is.EqualTo(Convert.ToHexString(expected)));
Assert.That(Convert.ToHexString(MessagePackSerializer.Serialize(doc, MessagePackSerializerOptions.Standard)), Is.EqualTo(Convert.ToHexString(expected)));
var back = (ShapeDoc)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(ShapeDoc), expected, Registry);
Assert.That(back.Shape, Is.TypeOf<Square>());
Assert.That(back.Shapes[0], Is.TypeOf<Circle>());
Assert.That(back.Shapes[1], Is.Null);
}
// A variant record whose override holds an unknown arm is refused naming the record, the schema id, the union and the key.
[Test]
public void VariantOverrideHoldingAnUnknownArmIsRefusedNamingTheSchema()
{
var folder = Path.Combine(Path.GetTempPath(), $"cultlib-unionguard-{Guid.NewGuid():N}");
Directory.CreateDirectory(folder);
try
{
var descriptor = Registry.GetRequired(typeof(GuardDoc));
var path = Path.Combine(folder, "variant.cc");
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(new CultPersistedStoreSnapshot
{
FormatVersion = CultPersistedStoreSnapshot.FormatV2,
SchemaCatalog = new[] { descriptor.ToCatalogEntry() },
Records = new[]
{
new CultPersistedRecord
{
Key = "base", SchemaId = descriptor.SchemaId, StoredAt = "2026-01-01T00:00:00.0000000Z",
Payload = Doc(LeafArm(3, "r", "ok", 1))
},
new CultPersistedRecord
{
Key = "variant", SchemaId = descriptor.SchemaId, StoredAt = "2026-01-01T00:00:00.0000000Z",
Payload = Array.Empty<byte>(),
Variant = new CultVariantDelta("base", new[] { CultVariantOverride.Set(1, Raw(LeafArm(4, "r", Canary, 7))) })
}
}
}));
var thrown = Assert.Catch(() => CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = Registry, ReadOnly = true }).Dispose())!;
var message = Chain(thrown);
Assert.Multiple(() =>
{
Assert.That(message, Does.Contain("Variant variant"));
Assert.That(message, Does.Contain($"(schema '{descriptor.SchemaId}')"));
Assert.That(message, Does.Contain(NodeName));
Assert.That(message, Does.Contain("key 4 "));
Assert.That(message, Does.Not.Contain(Canary));
});
}
finally
{
Directory.Delete(folder, recursive: true);
}
}
// A store holding an unknown arm fails to load and names the record, its schema, the union and the key; the genuine
// payload is replaced in the file by one that differs only in the arm key.
[TestCase(false, 4)]
@ -457,5 +605,23 @@ namespace GameCult.Caching.Tests
[Key(3)] public Inner? Crafted;
[Key(4)] public Outer? Any;
}
// An interface union: arm keys 0 and 2, so 1 is a retired gap.
[Union(0, typeof(Circle)), Union(2, typeof(Square))]
public interface IShape { }
[MessagePackObject] public class Circle : IShape { [Key(0)] public int N; }
[MessagePackObject] public sealed class Square : IShape { [Key(0)] public int N; }
[MessagePackObject] public sealed class Triangle : IShape { }
[MessagePackObject] public sealed class BigCircle : Circle { }
[CultDocument("tests.shape_doc", "tests.shape_doc.v1")]
[MessagePackObject]
public sealed class ShapeDoc
{
[Key(0)] [CultName] public string Id = string.Empty;
[Key(1)] public IShape? Shape;
[Key(2)] public List<IShape> Shapes = new();
}
}
}

View file

@ -12,6 +12,13 @@ All notable changes to this package are documented in this file.
does not name used to be written as `nil`. Both now throw, naming the union and
the key or type, and a store pull names the record key and schema id. Known arms
encode to the same bytes as before.
- A payload that fails to decode during a store pull (single-file or directory)
now throws `InvalidDataException` naming the record key and schema id, with the
original exception (for example `MessagePackSerializationException`) as its
`InnerException`. Callers that caught the codec's exception type around a store
open or pull must catch `InvalidDataException`. An arm key outside Int32 or not
an integer is refused with the same union-naming refusal, and a variant
override holding an unknown arm names the schema id.
## [1.0.60]