fix(battle-node): MsgEnvelope rejects reserved Body keys + complete ReceiveNodeResultCode

ToJson now throws ArgumentException when a Body key collides with a reserved
envelope field (uri/viewerId/uuid/bid/try/cat/pubSeq/playSeq); FromJson reuses
the same shared ReservedEnvelopeKeys HashSet. ReceiveNodeResultCode expanded
from 9 to 31 codes to mirror the full enums.md catalog. Two regression tests
added for the collision guard and PascalCase uri serialization.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-31 21:55:11 -04:00
parent 383044dd8f
commit 4cc8b3c01c
3 changed files with 79 additions and 5 deletions

View File

@@ -21,6 +21,11 @@ public sealed record MsgEnvelope(
{
private static readonly JsonSerializerOptions Options = CreateOptions();
private static readonly HashSet<string> ReservedEnvelopeKeys = new()
{
"uri", "viewerId", "uuid", "bid", "try", "cat", "pubSeq", "playSeq",
};
private static JsonSerializerOptions CreateOptions()
{
var opt = new JsonSerializerOptions
@@ -46,7 +51,14 @@ public sealed record MsgEnvelope(
if (env.Bid is not null) doc["bid"] = env.Bid;
if (env.PubSeq.HasValue) doc["pubSeq"] = env.PubSeq.Value;
if (env.PlaySeq.HasValue) doc["playSeq"] = env.PlaySeq.Value;
foreach (var (k, v) in env.Body) doc[k] = v;
foreach (var (k, v) in env.Body)
{
if (ReservedEnvelopeKeys.Contains(k))
throw new ArgumentException(
$"Body key '{k}' collides with a reserved envelope field. Move it to a typed field on MsgEnvelope.",
nameof(env));
doc[k] = v;
}
return JsonSerializer.Serialize(doc, Options);
}
@@ -65,10 +77,9 @@ public sealed record MsgEnvelope(
var playSeq = root.TryGetProperty("playSeq", out var plsEl) ? plsEl.GetInt64() : (long?)null;
var body = new Dictionary<string, object?>();
var reserved = new HashSet<string> { "uri", "viewerId", "uuid", "bid", "try", "cat", "pubSeq", "playSeq" };
foreach (var prop in root.EnumerateObject())
{
if (reserved.Contains(prop.Name)) continue;
if (ReservedEnvelopeKeys.Contains(prop.Name)) continue;
body[prop.Name] = ToObject(prop.Value);
}