Skip to content

Commit 3dbdbf8

Browse files
CopilotCopilot
andcommitted
Fix non-generic IDictionary key handling in ParseArguments (microsoft#5897)
Use direct (string) cast for dictionary keys instead of ToString() coercion, matching the established pattern in ObjectExtensions and PortableValueExtensions. This validates keys are actually strings rather than silently accepting and coercing non-string keys. Add test verifying non-string dictionary keys throw InvalidCastException. Co-authored-by: Copilot <[email protected]>
1 parent 18220be commit 3dbdbf8

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ internal AgentResponseUpdate ConvertToAgentResponseUpdate(ToolExecutionCompleteE
431431
var result = new Dictionary<string, object?>();
432432
foreach (System.Collections.DictionaryEntry entry in nonGenericDict)
433433
{
434-
result[entry.Key?.ToString() ?? string.Empty] = entry.Value;
434+
result[(string)entry.Key] = entry.Value;
435435
}
436436

437437
return result;

‎dotnet/tests/Microsoft.Agents.AI.GitHub.Copilot.UnitTests/ToolExecutionEventProjectionTests.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,28 @@ public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithNonGenericD
353353
Assert.Equal("42", functionCall.Arguments!["key2"]?.ToString());
354354
}
355355

356+
[Fact]
357+
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithNonStringDictionaryKey_Throws()
358+
{
359+
// Arrange
360+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
361+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
362+
363+
var args = new Hashtable { [1] = "value1", [2] = "value2" };
364+
var startEvent = new ToolExecutionStartEvent
365+
{
366+
Data = new ToolExecutionStartData
367+
{
368+
ToolCallId = "call-bad-keys",
369+
ToolName = "badTool",
370+
Arguments = args
371+
}
372+
};
373+
374+
// Act & Assert
375+
Assert.Throws<InvalidCastException>(() => agent.ConvertToAgentResponseUpdate(startEvent));
376+
}
377+
356378
[Fact]
357379
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithJsonElement_ParsesArguments()
358380
{

0 commit comments

Comments
 (0)