Generate derived documents' members and require [Key] on every one
The generator walked only members declared on the document class and emitted
a codec only when those were dense, so a derived document's generated
descriptor hashed a different schema id and catalog than the reflective one.
It now discovers members along the base-type chain the way
CultDocumentRegistry reflects them (public instance fields and get/set
properties, inherited included, [IgnoreMember] excluded), and density is
over that full set: RoutingWeapon : RoutingGear gets a codec and the same
SchemaId and catalog (Name@0, Damage@1) from both builds.
Unkeyed members made the two orderings disagree (generator by name, registry
by metadata token). Both implicit-slot paths are deleted: an unkeyed
persisted member of a [CultDocument] type is generator error GCC001, and the
registry throws the same message. The consumer audit found no unkeyed
document with a persisted store.
Tests: GeneratedAndReflectiveDescriptorsAgreeForDerivedDocuments failed on
ea5a07b ("no generated codec for the derived document") and passes through
UpsertWeaponThroughGearHandleReloadsAsWeapon; UnkeyedDocumentMemberIsRejected
drives the generator through Roslyn and compares its diagnostic with the
registry's exception. The registry's reflective BuildDescriptor is internal
and visible to the tests for that comparison. The registry-collision fixture's
emitted field gains its [Key]; schema-compatibility fixture ids are unchanged.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
ea5a07b37a
commit
9009aa79bc
8 changed files with 152 additions and 96 deletions
|
|
@ -1,3 +1,4 @@
|
|||
#nullable enable
|
||||
using GameCult.Caching.MessagePack;
|
||||
using GameCult.Caching.Tests;
|
||||
using MessagePack;
|
||||
|
|
|
|||
|
|
@ -142,7 +142,9 @@ public sealed class CultDocumentRegistryTests
|
|||
new object[] { schemaName, schemaVersion }));
|
||||
if (addStringField)
|
||||
{
|
||||
type.DefineField("Value", typeof(string), FieldAttributes.Public);
|
||||
type.DefineField("Value", typeof(string), FieldAttributes.Public).SetCustomAttribute(new CustomAttributeBuilder(
|
||||
typeof(global::MessagePack.KeyAttribute).GetConstructor(new[] { typeof(int) })!,
|
||||
new object[] { 0 }));
|
||||
}
|
||||
|
||||
return type.CreateType()!;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
<TargetFramework>net10.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>GameCult.Caching.Tests</RootNamespace>
|
||||
<!-- The generator source is compiled in so UnkeyedDocumentMemberIsRejected can drive it. -->
|
||||
<NoWarn>$(NoWarn);RS1036;RS1041</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
@ -12,6 +14,8 @@
|
|||
<ProjectReference Include="..\..\src\GameCult.Caching.MessagePack.Generator\GameCult.Caching.MessagePack.Generator.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false" />
|
||||
<Compile Include="..\..\src\GameCult.Caching.MessagePack.Generator\CultDocumentMessagePackGenerator.cs" Link="Generator\CultDocumentMessagePackGenerator.cs" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -71,6 +71,21 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(((RoutingWeapon)loaded!).Name, Is.EqualTo("lance"), "the inherited member did not round-trip");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GeneratedAndReflectiveDescriptorsAgreeForDerivedDocuments()
|
||||
{
|
||||
var generated = CultDocumentRegistry.ForTypes(new[] { typeof(RoutingWeapon) }).GetRequired<RoutingWeapon>();
|
||||
var reflective = CultDocumentRegistry.BuildDescriptor(typeof(RoutingWeapon));
|
||||
|
||||
Assert.That(generated.GeneratedPayloadSerializer, Is.Not.Null, "no generated codec for the derived document");
|
||||
Assert.That(generated.GeneratedPayloadDeserializer, Is.Not.Null);
|
||||
Assert.That(generated.SchemaId, Is.EqualTo(reflective.SchemaId));
|
||||
Assert.That(generated.CanonicalSchemaJson, Is.EqualTo(reflective.CanonicalSchemaJson));
|
||||
Assert.That(generated.Members.Select(member => $"{member.MemberName}@{member.Slot}"), Is.EqualTo(new[] { "Name@0", "Damage@1" }));
|
||||
Assert.That(reflective.Members.Select(member => $"{member.MemberName}@{member.Slot}"), Is.EqualTo(new[] { "Name@0", "Damage@1" }));
|
||||
await UpsertWeaponThroughGearHandleReloadsAsWeapon();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByNameMatchesAssignableTypes()
|
||||
{
|
||||
|
|
@ -728,10 +743,7 @@ namespace GameCult.Caching.Tests
|
|||
public string Text = string.Empty;
|
||||
}
|
||||
|
||||
// RoutingWeapon's own slots are not dense, so it gets no generated codec and serializes through
|
||||
// MessagePack's dynamic resolver, which needs a public [MessagePackObject].
|
||||
[CultDocument("tests.routing_gear", "tests.routing_gear.v1")]
|
||||
[MessagePackObject]
|
||||
public class RoutingGear
|
||||
{
|
||||
[Key(0)]
|
||||
|
|
@ -739,7 +751,6 @@ namespace GameCult.Caching.Tests
|
|||
}
|
||||
|
||||
[CultDocument("tests.routing_weapon", "tests.routing_weapon.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class RoutingWeapon : RoutingGear
|
||||
{
|
||||
[Key(1)]
|
||||
|
|
|
|||
66
tests/GameCult.Caching.Tests/UnkeyedDocumentMemberTests.cs
Normal file
66
tests/GameCult.Caching.Tests/UnkeyedDocumentMemberTests.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack.Generator;
|
||||
using MessagePack;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Caching.Tests
|
||||
{
|
||||
public class UnkeyedDocumentMemberTests
|
||||
{
|
||||
[Test]
|
||||
public void UnkeyedDocumentMemberIsRejected()
|
||||
{
|
||||
var reflective = Assert.Throws<InvalidOperationException>(() => CultDocumentRegistry.ForTypes(new[] { EmitUnkeyedDocument() }))!;
|
||||
|
||||
const string source = @"
|
||||
using GameCult.Caching;
|
||||
using MessagePack;
|
||||
[CultDocument(""tests.unkeyed"", ""tests.unkeyed.v1"")]
|
||||
public class UnkeyedDocument
|
||||
{
|
||||
[Key(0)] public string Name = string.Empty;
|
||||
public int Count;
|
||||
}";
|
||||
var references = ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")!)
|
||||
.Split(Path.PathSeparator)
|
||||
.Append(typeof(CultDocumentAttribute).Assembly.Location)
|
||||
.Append(typeof(KeyAttribute).Assembly.Location)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Select(path => MetadataReference.CreateFromFile(path));
|
||||
var compilation = CSharpCompilation.Create(
|
||||
"UnkeyedConsumer",
|
||||
new[] { CSharpSyntaxTree.ParseText(source) },
|
||||
references,
|
||||
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
|
||||
CSharpGeneratorDriver.Create(new CultDocumentMessagePackGenerator())
|
||||
.RunGeneratorsAndUpdateCompilation(compilation, out _, out var diagnostics);
|
||||
|
||||
var generated = diagnostics.Single(diagnostic => diagnostic.Id == "GCC001");
|
||||
Assert.That(generated.Severity, Is.EqualTo(DiagnosticSeverity.Error));
|
||||
Assert.That(generated.GetMessage(), Is.EqualTo(reflective.Message));
|
||||
Assert.That(reflective.Message, Does.Contain("UnkeyedDocument member Count has no [Key]"));
|
||||
}
|
||||
|
||||
private static Type EmitUnkeyedDocument()
|
||||
{
|
||||
var module = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("UnkeyedDocuments"), AssemblyBuilderAccess.Run)
|
||||
.DefineDynamicModule("UnkeyedDocuments");
|
||||
var type = module.DefineType("UnkeyedDocument", TypeAttributes.Public | TypeAttributes.Class);
|
||||
type.SetCustomAttribute(new CustomAttributeBuilder(
|
||||
typeof(CultDocumentAttribute).GetConstructor(new[] { typeof(string), typeof(string) })!,
|
||||
new object[] { "tests.unkeyed", "tests.unkeyed.v1" }));
|
||||
type.DefineField("Name", typeof(string), FieldAttributes.Public)
|
||||
.SetCustomAttribute(new CustomAttributeBuilder(typeof(KeyAttribute).GetConstructor(new[] { typeof(int) })!, new object[] { 0 }));
|
||||
type.DefineField("Count", typeof(int), FieldAttributes.Public);
|
||||
return type.CreateType()!;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue