From baf4ecf78fae634fafef956d3b85420162c074bb Mon Sep 17 00:00:00 2001 From: Libby Date: Thu, 19 Nov 2026 20:19:22 +0000 Subject: [PATCH] CultNet: refuse a binding serializer that returns null (#177) --- .../CultNetDocumentRegistry.cs | 14 ++++++++++--- .../BytesAfterPathTests.cs | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/GameCult.Networking/CultNetDocumentRegistry.cs b/src/GameCult.Networking/CultNetDocumentRegistry.cs index bf17a91f..5b21c9de 100644 --- a/src/GameCult.Networking/CultNetDocumentRegistry.cs +++ b/src/GameCult.Networking/CultNetDocumentRegistry.cs @@ -253,7 +253,7 @@ namespace GameCult.Networking RecordKey = handle.Key.Value, StoredAt = storedAt, PayloadEncoding = "messagepack", - Payload = binding.PayloadSerializer(document), + Payload = SerializeWithBinding(binding, document), SourceRuntimeId = options?.SourceRuntimeId, SourceAgentId = options?.SourceAgentId, SourceRole = options?.SourceRole, @@ -652,8 +652,9 @@ namespace GameCult.Networking RecordKey = key.Value, StoredAt = storedAt, PayloadEncoding = "messagepack", - Payload = binding?.PayloadSerializer(document) ?? - CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), _documents), + Payload = binding == null + ? CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), _documents) + : SerializeWithBinding(binding, document), SourceRuntimeId = options?.SourceRuntimeId, SourceAgentId = options?.SourceAgentId, SourceRole = options?.SourceRole, @@ -661,6 +662,13 @@ namespace GameCult.Networking }; } + // A binding owns its payload bytes: a serializer that returns null is a broken owner, refused here rather than + // handed to the default serializer or onto the wire as a null payload. + private static byte[] SerializeWithBinding(CultNetDocumentBinding binding, object document) => + binding.PayloadSerializer(document) + ?? throw new InvalidOperationException( + $"The payload serializer bound to {binding.DocumentType.FullName} (schema '{binding.SchemaId}') returned null; a binding must return the payload bytes."); + private CultNetEdge ToEdge(CultNetSelectionEvaluator.EdgeMatch edge, bool wantDocument) { // R-E: ToRawRecord and ToEdge emit the same id for the same row - the wire schema id a diff --git a/tests/GameCult.Networking.Tests/BytesAfterPathTests.cs b/tests/GameCult.Networking.Tests/BytesAfterPathTests.cs index 0b125075..c6d48a42 100644 --- a/tests/GameCult.Networking.Tests/BytesAfterPathTests.cs +++ b/tests/GameCult.Networking.Tests/BytesAfterPathTests.cs @@ -89,5 +89,26 @@ namespace GameCult.Networking.Tests Assert.That(refused.Message, Does.Contain("refused on write")); Assert.That(refused.Message, Does.Contain(typeof(DeepDoc).FullName)); } + + // A binding owns its payload bytes: one whose serializer returns null is refused, not silently given the default + // serializer's bytes. A non-null result still wins over the default. + [Test] + public void ABindingWhoseSerializerReturnsNullIsRefusedNotDefaulted() + { + var descriptor = Documents.GetRequired(typeof(TailDoc)); + var key = new CultRecordKey("a"); + const string storedAt = "1970-01-01T00:00:00.000Z"; + + var broken = new CultNetDocumentRegistry(Documents).Register( + CultNetDocumentBinding.ForDocument(Documents, payloadSerializer: _ => null!)); + var refused = Assert.Throws(() => broken.ToRawRecord(descriptor, key, new TailDoc(), storedAt))!; + Assert.That(refused.Message, Does.Contain(typeof(TailDoc).FullName)); + Assert.Throws(() => broken.CreateRawDocumentPutMessage("m", new CultRecordHandle(key), new TailDoc())); + + var custom = new byte[] { 0x91, 0x07 }; + var honest = new CultNetDocumentRegistry(Documents).Register( + CultNetDocumentBinding.ForDocument(Documents, payloadSerializer: _ => custom)); + Assert.That(honest.ToRawRecord(descriptor, key, new TailDoc(), storedAt).Payload, Is.EqualTo(custom)); + } } }