.NET: [Feature]: .NET Improve ChatClientAgentSession constructor#7142
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts .NET ChatClientAgentSession JSON deserialization behavior to work with stricter System.Text.Json settings (notably RespectRequiredConstructorParametersDefault=true) when optional properties are omitted due to JsonIgnoreCondition.WhenWritingNull.
Changes:
- Makes the
[JsonConstructor]constructor parameters optional by adding default values, preventing deserialization failures when JSON omits null properties. - Adds a unit test to validate round-trip behavior when serialization uses
DefaultIgnoreCondition = WhenWritingNull. - Adds XML documentation for the JSON constructor parameters.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs | Updates the [JsonConstructor] signature to accept omitted JSON properties via optional parameters and documents the constructor. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs | Adds a test covering WhenWritingNull serialization + subsequent deserialization using provided options. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
westey-m
approved these changes
Jul 16, 2026
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.
rogerbarreto
approved these changes
Jul 16, 2026
westey-m
approved these changes
Jul 16, 2026
rogerbarreto
enabled auto-merge
July 16, 2026 14:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #7109