CultNet: refuse a binding serializer that returns null #177

Merged
eureka merged 3 commits from hands/cultnet-cs-null-serializer into main 2026-11-19 20:19:22 +00:00
2 changed files with 32 additions and 3 deletions

View file

@ -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

View file

@ -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<TailDoc>(Documents, payloadSerializer: _ => null!));
var refused = Assert.Throws<InvalidOperationException>(() => broken.ToRawRecord(descriptor, key, new TailDoc(), storedAt))!;
Assert.That(refused.Message, Does.Contain(typeof(TailDoc).FullName));
Assert.Throws<InvalidOperationException>(() => broken.CreateRawDocumentPutMessage("m", new CultRecordHandle<TailDoc>(key), new TailDoc()));
var custom = new byte[] { 0x91, 0x07 };
var honest = new CultNetDocumentRegistry(Documents).Register(
CultNetDocumentBinding.ForDocument<TailDoc>(Documents, payloadSerializer: _ => custom));
Assert.That(honest.ToRawRecord(descriptor, key, new TailDoc(), storedAt).Payload, Is.EqualTo(custom));
}
}
}