CultNet: refuse a binding serializer that returns null instead of defaulting
Some checks failed
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 16m9s
Some checks failed
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 16m9s
ToRawRecord's 'binding?.PayloadSerializer(document) ?? default' let the default serializer silently override a bound owner whose serializer returned null. The binding path now refuses (InvalidOperationException, naming type and schema); CreateRawDocumentPutMessage shares the same check instead of putting a null payload on the wire. Non-null results are unchanged. Closes cultlib-gaps:finding:cut-bytes-after-cs.s3.null-serializer-silent-fallback Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
75188fdefa
commit
f4e3359eaf
2 changed files with 32 additions and 3 deletions
|
|
@ -253,7 +253,7 @@ namespace GameCult.Networking
|
||||||
RecordKey = handle.Key.Value,
|
RecordKey = handle.Key.Value,
|
||||||
StoredAt = storedAt,
|
StoredAt = storedAt,
|
||||||
PayloadEncoding = "messagepack",
|
PayloadEncoding = "messagepack",
|
||||||
Payload = binding.PayloadSerializer(document),
|
Payload = SerializeWithBinding(binding, document),
|
||||||
SourceRuntimeId = options?.SourceRuntimeId,
|
SourceRuntimeId = options?.SourceRuntimeId,
|
||||||
SourceAgentId = options?.SourceAgentId,
|
SourceAgentId = options?.SourceAgentId,
|
||||||
SourceRole = options?.SourceRole,
|
SourceRole = options?.SourceRole,
|
||||||
|
|
@ -652,8 +652,9 @@ namespace GameCult.Networking
|
||||||
RecordKey = key.Value,
|
RecordKey = key.Value,
|
||||||
StoredAt = storedAt,
|
StoredAt = storedAt,
|
||||||
PayloadEncoding = "messagepack",
|
PayloadEncoding = "messagepack",
|
||||||
Payload = binding?.PayloadSerializer(document) ??
|
Payload = binding == null
|
||||||
CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), _documents),
|
? CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), _documents)
|
||||||
|
: SerializeWithBinding(binding, document),
|
||||||
SourceRuntimeId = options?.SourceRuntimeId,
|
SourceRuntimeId = options?.SourceRuntimeId,
|
||||||
SourceAgentId = options?.SourceAgentId,
|
SourceAgentId = options?.SourceAgentId,
|
||||||
SourceRole = options?.SourceRole,
|
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)
|
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
|
// R-E: ToRawRecord and ToEdge emit the same id for the same row - the wire schema id a
|
||||||
|
|
|
||||||
|
|
@ -89,5 +89,26 @@ namespace GameCult.Networking.Tests
|
||||||
Assert.That(refused.Message, Does.Contain("refused on write"));
|
Assert.That(refused.Message, Does.Contain("refused on write"));
|
||||||
Assert.That(refused.Message, Does.Contain(typeof(DeepDoc).FullName));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue