From 355a260d8f79fda94b535b642623706838c658ae Mon Sep 17 00:00:00 2001 From: feiyun0112 Date: Thu, 16 Jul 2026 05:33:57 +0000 Subject: [PATCH 1/3] .NET: [Feature]: .NET Improve ChatClientAgentSession constructor --- .../ChatClient/ChatClientAgentSession.cs | 7 ++++++- .../ChatClient/ChatClientAgentSessionTests.cs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs index 400bfbcaf62..102aee83e68 100644 --- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs +++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs @@ -21,8 +21,13 @@ internal ChatClientAgentSession() { } + /// + /// Initializes a new instance of the class with optional conversation and state data. + /// + /// The underlying service chat history identifier, if available. + /// The state bag to initialize the session with. [JsonConstructor] - internal ChatClientAgentSession(string? conversationId, AgentSessionStateBag? stateBag) : base(stateBag ?? new()) + internal ChatClientAgentSession(string? conversationId = null, AgentSessionStateBag? stateBag = null) : base(stateBag ?? new()) { this.ConversationId = conversationId; } diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs index 1f5e5aa9cd5..773be578eec 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs @@ -2,7 +2,9 @@ using System; using System.Linq; +using System.Reflection; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.Extensions.AI; #pragma warning disable CA1861 // Avoid constant arrays as arguments @@ -117,6 +119,23 @@ public void DeserializeWithInvalidJsonThrows() Assert.Throws(() => ChatClientAgentSession.Deserialize(invalidJson)); } + [Fact] + public void VerifyDeserializeWithWhenWritingNullOptions() + { + // Arrange + var session = new ChatClientAgentSession(); + JsonSerializerOptions options = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; + options.TypeInfoResolverChain.Add(AgentJsonUtilities.DefaultOptions.TypeInfoResolver!); + + // Act + var serializedSession = JsonSerializer.SerializeToElement(session, options.GetTypeInfo(typeof(ChatClientAgentSession))); + var deserializedSession = ChatClientAgentSession.Deserialize(serializedSession, options); + + // Assert + Assert.False(serializedSession.TryGetProperty("conversationId", out _)); + Assert.Null(deserializedSession.ConversationId); + } + #endregion Deserialize Tests #region Serialize Tests From 1fc69c3feb66f4fd388604d52c6ac3d0a0d5e3ed Mon Sep 17 00:00:00 2001 From: feiyun0112 Date: Thu, 16 Jul 2026 11:14:13 +0000 Subject: [PATCH 2/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../ChatClient/ChatClientAgentSessionTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs index 773be578eec..6226bf7c11c 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs @@ -2,7 +2,6 @@ using System; using System.Linq; -using System.Reflection; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.Extensions.AI; From add95b9eff4443fdff52ebb608d84e7f49f2bcb0 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:52:02 +0100 Subject: [PATCH 3/3] test: make deserialize test actually reproduce issue #7109 VerifyDeserializeWithWhenWritingNullOptions passed against both the old and the fixed constructor, so it did not guard against the regression. The bug only reproduces when required constructor parameters are respected (the issue uses RespectRequiredConstructorParametersDefault=true). With WhenWritingNull a null conversationId is omitted from the JSON, and STJ then throws 'missing required properties including: conversationId' because the constructor parameter had no default value. Adding RespectRequiredConstructorParameters = true to the test options makes the test red against the parameter-without-default constructor and green with the default-valued constructor parameters, so it now protects the fix. --- .../ChatClient/ChatClientAgentSessionTests.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs index 6226bf7c11c..20bf40c393b 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs @@ -123,7 +123,11 @@ public void VerifyDeserializeWithWhenWritingNullOptions() { // Arrange var session = new ChatClientAgentSession(); - JsonSerializerOptions options = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; + JsonSerializerOptions options = new() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + RespectRequiredConstructorParameters = true, + }; options.TypeInfoResolverChain.Add(AgentJsonUtilities.DefaultOptions.TypeInfoResolver!); // Act