-
Notifications
You must be signed in to change notification settings - Fork 1.9k
.NET: Tweak AgentAbstractionsJsonUtilities to better match AIJsonUtilities #1094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
...t/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentAbstractionsJsonUtilitiesTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Text.Encodings.Web; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| #pragma warning disable CA1812 // Avoid uninstantiated internal classes | ||
|
|
||
| namespace Microsoft.Agents.AI.Abstractions.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="AgentAbstractionsJsonUtilities"/> | ||
| /// </summary> | ||
| public class AgentAbstractionsJsonUtilitiesTests | ||
| { | ||
| [Fact] | ||
| public void DefaultOptions_HasExpectedConfiguration() | ||
| { | ||
| var options = AgentAbstractionsJsonUtilities.DefaultOptions; | ||
|
|
||
| // Must be read-only singleton. | ||
| Assert.NotNull(options); | ||
| Assert.Same(options, AgentAbstractionsJsonUtilities.DefaultOptions); | ||
| Assert.True(options.IsReadOnly); | ||
|
|
||
| // Must conform to JsonSerializerDefaults.Web | ||
| Assert.Equal(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy); | ||
| Assert.True(options.PropertyNameCaseInsensitive); | ||
| Assert.Equal(JsonNumberHandling.AllowReadingFromString, options.NumberHandling); | ||
|
|
||
| // Additional settings | ||
| Assert.Equal(JsonIgnoreCondition.WhenWritingNull, options.DefaultIgnoreCondition); | ||
| Assert.Same(JavaScriptEncoder.UnsafeRelaxedJsonEscaping, options.Encoder); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("<script>alert('XSS')</script>", "<script>alert('XSS')</script>")] | ||
| [InlineData("""{"forecast":"sunny", "temperature":"75"}""", """{\"forecast\":\"sunny\", \"temperature\":\"75\"}""")] | ||
| [InlineData("""{"message":"Πάντα ῥεῖ."}""", """{\"message\":\"Πάντα ῥεῖ.\"}""")] | ||
| [InlineData("""{"message":"七転び八起き"}""", """{\"message\":\"七転び八起き\"}""")] | ||
| [InlineData("""☺️🤖🌍𝄞""", """☺️\uD83E\uDD16\uD83C\uDF0D\uD834\uDD1E""")] | ||
| public void DefaultOptions_UsesExpectedEscaping(string input, string expectedJsonString) | ||
| { | ||
| var options = AgentAbstractionsJsonUtilities.DefaultOptions; | ||
| string json = JsonSerializer.Serialize(input, options); | ||
| Assert.Equal($@"""{expectedJsonString}""", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DefaultOptions_UsesReflectionWhenDefault() | ||
| { | ||
| Type anonType = new { Name = 42 }.GetType(); | ||
| Assert.Equal(JsonSerializer.IsReflectionEnabledByDefault, AgentAbstractionsJsonUtilities.DefaultOptions.TryGetTypeInfo(anonType, out _)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DefaultOptions_AllowsReadingNumbersFromStrings_AndOmitsNulls() | ||
| { | ||
| var obj = JsonSerializer.Deserialize<NumberContainer>( | ||
| "{\"value\":\"42\",\"optional\":null}", // value as string, optional null | ||
| AgentAbstractionsJsonUtilities.DefaultOptions); | ||
| Assert.NotNull(obj); | ||
| Assert.Equal(42, obj!.Value); | ||
| Assert.Null(obj.Optional); | ||
| Assert.Equal("{\"value\":42}", | ||
| JsonSerializer.Serialize(obj, AgentAbstractionsJsonUtilities.DefaultOptions)); // null omitted | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DefaultOptions_SerializesEnumsAsStrings() | ||
| { | ||
| Assert.Equal("\"Monday\"", JsonSerializer.Serialize(DayOfWeek.Monday, AgentAbstractionsJsonUtilities.DefaultOptions)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DefaultOptions_UsesCamelCasePropertyNames_ForAgentRunResponse() | ||
| { | ||
| var response = new AgentRunResponse(new ChatMessage(ChatRole.Assistant, "Hello")); | ||
| string json = JsonSerializer.Serialize(response, AgentAbstractionsJsonUtilities.DefaultOptions); | ||
| Assert.Contains("\"messages\"", json); | ||
| Assert.DoesNotContain("\"Messages\"", json); | ||
| } | ||
|
|
||
| private sealed class NumberContainer | ||
| { | ||
| public int Value { get; set; } | ||
| public string? Optional { get; set; } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.