-
Notifications
You must be signed in to change notification settings - Fork 48
feat: Add AdditionalPropertiesExtensions with ToAdditionalProperties and ToA2AMetadata methods #260
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
SergeyMenshykh
merged 3 commits into
main
from
copilot/move-to-additional-properties-extensions
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,53 @@ | ||
| using A2A; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for converting between <see cref="AdditionalPropertiesDictionary"/> and A2A metadata dictionaries. | ||
| /// </summary> | ||
| public static class AdditionalPropertiesExtensions | ||
| { | ||
| /// <summary> | ||
| /// Creates an <see cref="AdditionalPropertiesDictionary"/> from A2A metadata. | ||
| /// </summary> | ||
| /// <param name="metadata">The A2A metadata dictionary to convert.</param> | ||
| /// <returns>An <see cref="AdditionalPropertiesDictionary"/> containing the metadata, or <see langword="null"/> if the metadata is empty.</returns> | ||
| public static AdditionalPropertiesDictionary? ToAdditionalProperties(this Dictionary<string, JsonElement>? metadata) | ||
| { | ||
| if (metadata is not { Count: > 0 }) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| AdditionalPropertiesDictionary props = []; | ||
| foreach (var kvp in metadata) | ||
| { | ||
| props[kvp.Key] = kvp.Value; | ||
| } | ||
|
|
||
| return props; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an A2A metadata dictionary from an <see cref="AdditionalPropertiesDictionary"/>. | ||
| /// </summary> | ||
| /// <param name="additionalProperties">The additional properties dictionary to convert.</param> | ||
| /// <returns>A dictionary of A2A metadata, or <see langword="null"/> if the additional properties dictionary is empty.</returns> | ||
| public static Dictionary<string, JsonElement>? ToA2AMetadata(this AdditionalPropertiesDictionary? additionalProperties) | ||
| { | ||
| if (additionalProperties is not { Count: > 0 }) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var metadata = new Dictionary<string, JsonElement>(); | ||
|
|
||
| foreach (var kvp in additionalProperties) | ||
| { | ||
| metadata[kvp.Key] = JsonSerializer.SerializeToElement(kvp.Value, A2AJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))); | ||
| } | ||
|
|
||
| return metadata; | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
tests/A2A.UnitTests/Models/AdditionalPropertiesExtensionsTests.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,129 @@ | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace A2A.UnitTests.Models | ||
| { | ||
| public sealed class AdditionalPropertiesExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void ToAdditionalProperties_ReturnsNullForNullMetadata() | ||
| { | ||
| Dictionary<string, JsonElement>? metadata = null; | ||
| var result = metadata.ToAdditionalProperties(); | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToAdditionalProperties_ReturnsNullForEmptyMetadata() | ||
| { | ||
| var metadata = new Dictionary<string, JsonElement>(); | ||
| var result = metadata.ToAdditionalProperties(); | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToAdditionalProperties_ConvertsMetadataToAdditionalProperties() | ||
| { | ||
| var metadata = new Dictionary<string, JsonElement> | ||
| { | ||
| ["num"] = JsonSerializer.SerializeToElement(42), | ||
| ["str"] = JsonSerializer.SerializeToElement("value"), | ||
| ["bool"] = JsonSerializer.SerializeToElement(true) | ||
| }; | ||
|
|
||
| var result = metadata.ToAdditionalProperties(); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal(3, result!.Count); | ||
| Assert.True(result.TryGetValue("num", out var numObj)); | ||
| Assert.True(result.TryGetValue("str", out var strObj)); | ||
| Assert.True(result.TryGetValue("bool", out var boolObj)); | ||
| var numJe = Assert.IsType<JsonElement>(numObj); | ||
| var strJe = Assert.IsType<JsonElement>(strObj); | ||
| var boolJe = Assert.IsType<JsonElement>(boolObj); | ||
| Assert.Equal(42, numJe.GetInt32()); | ||
| Assert.Equal("value", strJe.GetString()); | ||
| Assert.True(boolJe.GetBoolean()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToA2AMetadata_ReturnsNullForNullAdditionalProperties() | ||
| { | ||
| AdditionalPropertiesDictionary? additionalProperties = null; | ||
| var result = additionalProperties.ToA2AMetadata(); | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToA2AMetadata_ReturnsNullForEmptyAdditionalProperties() | ||
| { | ||
| var additionalProperties = new AdditionalPropertiesDictionary(); | ||
| var result = additionalProperties.ToA2AMetadata(); | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToA2AMetadata_ConvertsAdditionalPropertiesToMetadata() | ||
| { | ||
| var additionalProperties = new AdditionalPropertiesDictionary | ||
| { | ||
| ["num"] = 42, | ||
| ["str"] = "value", | ||
| ["bool"] = true | ||
| }; | ||
|
|
||
| var result = additionalProperties.ToA2AMetadata(); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal(3, result!.Count); | ||
| Assert.True(result.TryGetValue("num", out var numJe)); | ||
| Assert.True(result.TryGetValue("str", out var strJe)); | ||
| Assert.True(result.TryGetValue("bool", out var boolJe)); | ||
| Assert.Equal(42, numJe.GetInt32()); | ||
| Assert.Equal("value", strJe.GetString()); | ||
| Assert.True(boolJe.GetBoolean()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToA2AMetadata_HandlesJsonElementValues() | ||
| { | ||
| var additionalProperties = new AdditionalPropertiesDictionary | ||
| { | ||
| ["nested"] = JsonSerializer.SerializeToElement(new { key = "val" }) | ||
| }; | ||
|
|
||
| var result = additionalProperties.ToA2AMetadata(); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Single(result!); | ||
| Assert.True(result.TryGetValue("nested", out var nestedJe)); | ||
| Assert.Equal(JsonValueKind.Object, nestedJe.ValueKind); | ||
| Assert.Equal("val", nestedJe.GetProperty("key").GetString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RoundTrip_ToA2AMetadata_And_ToAdditionalProperties() | ||
| { | ||
| var originalProps = new AdditionalPropertiesDictionary | ||
| { | ||
| ["number"] = 123, | ||
| ["text"] = "hello" | ||
| }; | ||
|
|
||
| var metadata = originalProps.ToA2AMetadata(); | ||
| Assert.NotNull(metadata); | ||
|
|
||
| var restoredProps = metadata.ToAdditionalProperties(); | ||
| Assert.NotNull(restoredProps); | ||
| Assert.Equal(2, restoredProps!.Count); | ||
|
|
||
| // Values are now JsonElements after the round trip | ||
| Assert.True(restoredProps.TryGetValue("number", out var numObj)); | ||
| Assert.True(restoredProps.TryGetValue("text", out var textObj)); | ||
| var numJe = Assert.IsType<JsonElement>(numObj); | ||
| var textJe = Assert.IsType<JsonElement>(textObj); | ||
| Assert.Equal(123, numJe.GetInt32()); | ||
| Assert.Equal("hello", textJe.GetString()); | ||
| } | ||
| } | ||
| } |
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.