Skip to content

Commit 0f48857

Browse files
Copilotwestey-m
andcommitted
Add parameter validation to CreateSessionAsync methods
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
1 parent 87b8298 commit 0f48857

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ protected sealed override ValueTask<AgentSession> CreateSessionCoreAsync(Cancell
6363
/// <param name="contextId">The context id to continue.</param>
6464
/// <returns>A value task representing the asynchronous operation. The task result contains a new <see cref="AgentSession"/> instance.</returns>
6565
public ValueTask<AgentSession> CreateSessionAsync(string contextId)
66-
=> new(new A2AAgentSession() { ContextId = contextId });
66+
{
67+
_ = Throw.IfNullOrWhitespace(contextId);
68+
return new(new A2AAgentSession() { ContextId = contextId });
69+
}
6770

6871
/// <summary>
6972
/// Get a new <see cref="AgentSession"/> instance using an existing context id and task id, to resume that conversation from a specific task.
@@ -72,7 +75,11 @@ public ValueTask<AgentSession> CreateSessionAsync(string contextId)
7275
/// <param name="taskId">The task id to resume from.</param>
7376
/// <returns>A value task representing the asynchronous operation. The task result contains a new <see cref="AgentSession"/> instance.</returns>
7477
public ValueTask<AgentSession> CreateSessionAsync(string contextId, string taskId)
75-
=> new(new A2AAgentSession() { ContextId = contextId, TaskId = taskId });
78+
{
79+
_ = Throw.IfNullOrWhitespace(contextId);
80+
_ = Throw.IfNullOrWhitespace(taskId);
81+
return new(new A2AAgentSession() { ContextId = contextId, TaskId = taskId });
82+
}
7683

7784
/// <inheritdoc/>
7885
protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)

dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ public async Task CreateSessionAsync_WithContextIdAndTaskId_CreatesSessionWithBo
11991199
public async Task CreateSessionAsync_WithInvalidContextId_ThrowsArgumentExceptionAsync(string? contextId)
12001200
{
12011201
// Act & Assert
1202-
await Assert.ThrowsAsync<ArgumentException>(async () =>
1202+
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
12031203
await this._agent.CreateSessionAsync(contextId!));
12041204
}
12051205

@@ -1218,7 +1218,7 @@ public async Task CreateSessionAsync_WithInvalidContextIdAndValidTaskId_ThrowsAr
12181218
const string TaskId = "valid-task-id";
12191219

12201220
// Act & Assert
1221-
await Assert.ThrowsAsync<ArgumentException>(async () =>
1221+
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
12221222
await this._agent.CreateSessionAsync(contextId!, TaskId));
12231223
}
12241224

@@ -1237,7 +1237,7 @@ public async Task CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsAr
12371237
const string ContextId = "valid-context-id";
12381238

12391239
// Act & Assert
1240-
await Assert.ThrowsAsync<ArgumentException>(async () =>
1240+
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
12411241
await this._agent.CreateSessionAsync(ContextId, taskId!));
12421242
}
12431243
#endregion

0 commit comments

Comments
 (0)