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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
namespace SVSim.BattleNode.Protocol;
|
||||
|
||||
/// <summary>The "resultCode" field on synchronize pushes. 1=Success, else error.</summary>
|
||||
/// <summary>
|
||||
/// The "resultCode" field on synchronize pushes. 1 = Success, else error.
|
||||
/// Mirrors the full catalog from docs/api-spec/in-battle/enums.md, including
|
||||
/// source typos in the original spec (RoomBattleReadeError, RoomTornament*).
|
||||
/// </summary>
|
||||
public enum ReceiveNodeResultCode
|
||||
{
|
||||
None = 0,
|
||||
@@ -8,9 +12,30 @@ public enum ReceiveNodeResultCode
|
||||
Different_UUID = 30001,
|
||||
RedisReplyError = 30002,
|
||||
UnexistUserinfoError = 30003,
|
||||
RoomStatusInfoError = 30101,
|
||||
RoomCreateError = 30102,
|
||||
RoomEntryError = 30103,
|
||||
RoomKickError = 30104,
|
||||
RoomLeaveError = 30105,
|
||||
RoomReleaseError = 30106,
|
||||
RoomForceReleaseError = 30107,
|
||||
RoomReenterError = 30108,
|
||||
RoomBattleReadeError = 30109, // source typo per spec, preserved
|
||||
RoomTournamentDeckError = 30110,
|
||||
RoomTournamentError = 30111,
|
||||
RoomSetupLock = 30112,
|
||||
MatchingTimeOut = 30201,
|
||||
UnmatchedError = 30211,
|
||||
CurrentBattleError = 30212,
|
||||
UnexpectedPhaseError = 30213,
|
||||
// Other codes from spec/enums.md added as needed; v1 only triggers the four above.
|
||||
WatchError = 30302,
|
||||
SwapTimeoutError = 31001,
|
||||
FoundRemovedUserErrorSelf = 32101,
|
||||
FoundRemovedUserErrorOppo = 32102,
|
||||
FoundRemovedUserErrorWatcher = 32103,
|
||||
RoomTimeEndError = 32104,
|
||||
WatcherInRemovedOwnerRoomError = 32105,
|
||||
RoomTornamentOwnTimeEndError = 32106, // source typo per spec, preserved
|
||||
RoomTornamentOppoTimeEndError = 32107, // source typo per spec, preserved
|
||||
BattleFinishTimeEnd = 32108,
|
||||
}
|
||||
|
||||
@@ -63,4 +63,42 @@ public class MsgEnvelopeTests
|
||||
Assert.That(env.Uri, Is.EqualTo(NetworkBattleUri.PlayActions));
|
||||
Assert.That(env.Cat, Is.EqualTo(EmitCategory.Battle));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToJson_BodyContainingReservedKey_Throws()
|
||||
{
|
||||
var env = new MsgEnvelope(
|
||||
Uri: NetworkBattleUri.Loaded,
|
||||
ViewerId: 1,
|
||||
Uuid: "u",
|
||||
Bid: null,
|
||||
Try: 0,
|
||||
Cat: EmitCategory.Battle,
|
||||
PubSeq: null,
|
||||
PlaySeq: null,
|
||||
Body: new Dictionary<string, object?> { ["uri"] = "Injected" });
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => MsgEnvelope.ToJson(env));
|
||||
Assert.That(ex!.Message, Does.Contain("uri"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToJson_UriField_SerializesAsExactPascalCaseMemberName()
|
||||
{
|
||||
var env = new MsgEnvelope(
|
||||
Uri: NetworkBattleUri.PlayActions,
|
||||
ViewerId: 1,
|
||||
Uuid: "u",
|
||||
Bid: null,
|
||||
Try: 0,
|
||||
Cat: EmitCategory.Battle,
|
||||
PubSeq: null,
|
||||
PlaySeq: null,
|
||||
Body: new Dictionary<string, object?>());
|
||||
|
||||
var json = MsgEnvelope.ToJson(env);
|
||||
|
||||
// Wire form must be PascalCase exactly — not "playActions", not "play_actions".
|
||||
Assert.That(json, Does.Contain("\"uri\":\"PlayActions\""));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user