From c894a601c2c5993f54bae2242de8b2b714bf62ae Mon Sep 17 00:00:00 2001 From: Libby Date: Thu, 12 Nov 2026 01:14:20 +0000 Subject: [PATCH] CultUnionGuard: measure a type only when the chain hands out MessagePack's own [Key]-layout formatter Guarded decided from attributes, then measured whatever formatter the composite handed out, so a consumer resolver's [count, items...] type and a [MessagePackObject] collection were falsely marked (Soul s2 guard-asks-attribute-not-formatter). KeyLayout decides from the formatter itself, by identity with StandardResolver's (or StandardResolverAllowPrivate's), excluding the attribute and generic-collection resolvers' instances, as ResolverClaim does. The [MessagePackFormatter] attribute check is deleted: that door is now the AttributeFormatterResolver identity. Co-Authored-By: Claude Opus 5.5 --- .../CultUnionGuard.cs | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/GameCult.Caching.MessagePack/CultUnionGuard.cs b/src/GameCult.Caching.MessagePack/CultUnionGuard.cs index 1a2a535e..b8e38f3c 100644 --- a/src/GameCult.Caching.MessagePack/CultUnionGuard.cs +++ b/src/GameCult.Caching.MessagePack/CultUnionGuard.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Runtime.CompilerServices; using MessagePack; using MessagePack.Formatters; +using MessagePack.Resolvers; namespace GameCult.Caching.MessagePack; @@ -21,16 +22,35 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol { var arms = typeof(T).GetCustomAttributes(inherit: false).ToArray(); if (arms.Length == 0) - return Guarded(typeof(T)) && inner.GetFormatter() is { } objectFormatter ? new SlotFormatter(objectFormatter) : null; + return inner.GetFormatter() is { } objectFormatter && KeyLayout(objectFormatter) ? new SlotFormatter(objectFormatter) : null; return inner.GetFormatter() is not { } formatter ? null : new Formatter(formatter, arms); } - // A concrete int-keyed [MessagePackObject] type whose layout MessagePack derives from its [Key] members. A type that names its own - // [MessagePackFormatter] owns its layout and its tolerance (it may read a legacy shape or skip fields on purpose), so it is not guarded. - private static bool Guarded(Type type) => - !type.IsAbstract && !type.IsInterface && !type.IsGenericTypeDefinition && - type.GetCustomAttribute(inherit: true) is { KeyAsPropertyName: false } && - type.GetCustomAttribute(inherit: true) == null; + // The one decision of whether a type is measured: the formatter the chain hands out for an int-keyed [MessagePackObject] type is + // MessagePack's own [Key]-layout formatter, the very instance StandardResolver (or its AllowPrivate twin) builds for it. A formatter a + // consumer resolver or a [MessagePackFormatter] attribute supplies owns its layout and its tolerance, and a [MessagePackObject] + // collection is written as its items by the generic resolver: none of those is measured. Identity, as ResolverClaim asks it. + private static bool KeyLayout(IMessagePackFormatter formatter) + { + if (typeof(T).IsAbstract || typeof(T).IsInterface || + typeof(T).GetCustomAttribute(inherit: true) is not { KeyAsPropertyName: false }) + return false; + if (ReferenceEquals(formatter, Of(AttributeFormatterResolver.Instance)) || ReferenceEquals(formatter, Of(DynamicGenericResolver.Instance))) + return false; + return ReferenceEquals(formatter, Of(StandardResolver.Instance)) || ReferenceEquals(formatter, Of(StandardResolverAllowPrivate.Instance)); + } + + private static IMessagePackFormatter? Of(IFormatterResolver resolver) + { + try + { + return resolver.GetFormatter(); + } + catch (Exception) + { + return null; + } + } // Counts a concrete object's array header against its declared slots and notes the excess into the open CultSlotScope, then lets the // inner formatter read it unchanged: no byte scan, no second decode. With no scope open it is a depth-free pass-through.