Skip to content

Commit 5f0df10

Browse files
committed
perf: lazily generate MessageId default
Per review: generating the default in the auto-property initializer ran on every AgentMessage construction, including deserialization where the JSON already carries a messageId, discarding the generated id immediately. Uses a backing field and a lazy getter instead, so the default is only generated if the property is actually read without having been set.
1 parent ab2b6a2 commit 5f0df10

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/A2A.V0_3/Models/AgentMessage.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,24 @@ public sealed class AgentMessage() : A2AResponse(A2AEventKind.Message)
5050
[JsonPropertyName("referenceTaskIds")]
5151
public List<string>? ReferenceTaskIds { get; set; }
5252

53+
private string? _messageId;
54+
5355
/// <summary>
5456
/// Identifier created by the message creator.
5557
/// </summary>
5658
/// <remarks>
5759
/// Not <c>[JsonRequired]</c>: some v0.3 producers omit it (a discrepancy between the
58-
/// <c>.proto</c> and JSON Schema definitions of v0.3). Defaults to a freshly generated id
60+
/// <c>.proto</c> and JSON Schema definitions of v0.3). Falls back to a lazily generated id
5961
/// rather than an empty string, so a message deserialized without one still has a usable,
60-
/// unique identifier.
62+
/// unique identifier. Lazy so a message whose JSON already carries a messageId never
63+
/// allocates a discarded id on construction.
6164
/// </remarks>
6265
[JsonPropertyName("messageId")]
63-
public string MessageId { get; set; } = Guid.NewGuid().ToString();
66+
public string MessageId
67+
{
68+
get => _messageId ??= Guid.NewGuid().ToString();
69+
set => _messageId = value;
70+
}
6471

6572
/// <summary>
6673
/// Identifier of task the message is related to.

0 commit comments

Comments
 (0)