diff --git a/src/GameCult.Caching/CultCache.cs b/src/GameCult.Caching/CultCache.cs index a347fd58..f639389c 100644 --- a/src/GameCult.Caching/CultCache.cs +++ b/src/GameCult.Caching/CultCache.cs @@ -3256,7 +3256,7 @@ namespace GameCult.Caching // The codec knows what it refused but not which record; the store does. Say both, and never null the record. var causes = new List(); for (var cause = exception; cause != null; cause = cause.InnerException) causes.Add(cause.Message); - throw new InvalidDataException($"Record '{record.Key}' (schema '{record.SchemaId}') cannot be decoded: {string.Join(" <- ", causes)}", exception); + throw new InvalidDataException($"Record '{record.Key}' (schema '{record.SchemaId}') cannot be decoded: {string.Join(" <- ", causes.Distinct())}", exception); } return new CultStoredDocument( new CultRecordKey(record.Key), diff --git a/tests/GameCult.Caching.Tests/UnionArmGuardTests.cs b/tests/GameCult.Caching.Tests/UnionArmGuardTests.cs index 741b5840..817dc9db 100644 --- a/tests/GameCult.Caching.Tests/UnionArmGuardTests.cs +++ b/tests/GameCult.Caching.Tests/UnionArmGuardTests.cs @@ -262,6 +262,22 @@ namespace GameCult.Caching.Tests Assert.That(MessagePackSerializer.Serialize(new CultRecordRef(new CultRecordKey("k")), options), Is.EqualTo(Raw((ref MessagePackWriter w) => w.Write("consumer-ref")))); } + // A consumer resolver may format the union type itself. The guard wraps whatever the rest of the composite hands out, + // so that formatter is used and the key check still runs ahead of it. + [Test] + public void ConsumerFormatterForTheUnionItselfIsWrappedNotReplaced() + { + var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(GuardDoc).Assembly); + Assert.That(MessagePackSerializer.Serialize(new CustomArm(), options), Is.EqualTo(Raw((ref MessagePackWriter w) => w.Write("consumer-union")))); + Assert.Throws(() => MessagePackSerializer.Deserialize( + Raw((ref MessagePackWriter w) => { w.WriteArrayHeader(2); w.Write(5); w.WriteArrayHeader(0); }), options)); + Assert.Throws(() => MessagePackSerializer.Serialize(new CustomRogue(), options)); + } + + [Union(0, typeof(CustomArm))] public abstract class Custom { } + [MessagePackObject] public sealed class CustomArm : Custom { } + [MessagePackObject] public sealed class CustomRogue : Custom { } + // The guard asks for a union's own attributes only. MessagePack declares UnionAttribute not inherited, so an inner union // can never see its outer union's keys through the base class; this pins that fact, on which the explicit false rests. [Test] @@ -277,6 +293,7 @@ namespace GameCult.Caching.Tests public IMessagePackFormatter? GetFormatter() => typeof(T) == typeof(Overridden) ? (IMessagePackFormatter)(object)new Fixed(new Overridden(), "consumer") + : typeof(T) == typeof(Custom) ? (IMessagePackFormatter)(object)new Fixed(new CustomArm(), "consumer-union") : typeof(T) == typeof(CultRecordRef) ? (IMessagePackFormatter)(object)new Fixed>(default, "consumer-ref") : null; @@ -350,14 +367,15 @@ namespace GameCult.Caching.Tests { using var cache = await CultCacheMessagePack.OpenAsync(path, new CultCacheOpenOptions { Registry = Registry, UseDirectoryStore = directory, ReadOnly = true }); }); - var message = Chain(thrown!); + // The outermost message alone says it: a caller that prints only .Message must see the record and the union. + var message = thrown!.Message; Assert.Multiple(() => { Assert.That(message, Does.Contain("Record 'd1'")); Assert.That(message, Does.Contain($"(schema '{Registry.GetRequired(typeof(GuardDoc)).SchemaId}')")); Assert.That(message, Does.Contain(NodeName)); Assert.That(message, Does.Contain($"key {key} ")); - Assert.That(message, Does.Not.Contain(Canary)); + Assert.That(Chain(thrown), Does.Not.Contain(Canary)); }); } finally