Skip to content

Now serializing empty lists #280

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 1 commit into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/libraries/Core/Microsoft.Agents.Core/Models/Activity.cs
Original file line number Diff line number Diff line change
@@ -35,6 +35,14 @@ public class Activity :
/// <summary> Initializes a new instance of Activity. </summary>
public Activity()
{
MembersAdded = [];
MembersRemoved = [];
ReactionsAdded = [];
ReactionsRemoved = [];
Attachments = [];
Entities = [];
ListenFor = [];
TextHighlights = [];
}

/// <summary> Initializes a new instance of Activity. </summary>
@@ -98,19 +106,19 @@ public Activity(string type = default, string id = default, System.DateTimeOffse
Recipient = recipient;
TextFormat = textFormat;
AttachmentLayout = attachmentLayout;
MembersAdded = membersAdded;
MembersRemoved = membersRemoved;
ReactionsAdded = reactionsAdded;
ReactionsRemoved = reactionsRemoved;
MembersAdded = membersAdded ?? [];
MembersRemoved = membersRemoved ?? [];
ReactionsAdded = reactionsAdded ?? [];
ReactionsRemoved = reactionsRemoved ?? [];
TopicName = topicName;
Locale = locale;
Text = text;
Speak = speak;
InputHint = inputHint;
Summary = summary;
SuggestedActions = suggestedActions;
Attachments = attachments;
Entities = entities;
Attachments = attachments ?? [];
Entities = entities ?? [];
ChannelData = channelData;
Action = action;
ReplyToId = replyToId;
@@ -123,8 +131,8 @@ public Activity(string type = default, string id = default, System.DateTimeOffse
Expiration = expiration;
Importance = importance;
DeliveryMode = deliveryMode;
ListenFor = listenFor;
TextHighlights = textHighlights;
ListenFor = listenFor ?? [];
TextHighlights = textHighlights ?? [];
SemanticAction = semanticAction;
}

Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ public BasicCard(string title = default, string subtitle = default, string text
Title = title;
Subtitle = subtitle;
Text = text;
Images = images;
Buttons = buttons;
Images = images ?? [];
Buttons = buttons ?? [];
Tap = tap;
}

Original file line number Diff line number Diff line change
@@ -10,7 +10,11 @@ namespace Microsoft.Agents.Core.Models
/// <summary> Video card. </summary>
public class VideoCard
{
public VideoCard() { }
public VideoCard()
{
Media = [];
Buttons = [];
}

/// <summary> Initializes a new instance of VideoCard. </summary>
/// <param name="title"> Title of this card. </param>
Original file line number Diff line number Diff line change
@@ -65,14 +65,15 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
{
var propertyValue = property.GetValue(value);

#if SKIP_EMPTY_LISTS
if (propertyValue is IList list)
{
if (list == null || list.Count == 0)
{
continue;
}
}

#endif
if (propertyValue != null || !(options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull))

{
@@ -267,6 +268,7 @@ private void ReadProperty(ref Utf8JsonReader reader, T value, string propertyNam
var CollectionPropertyValue = System.Text.Json.JsonSerializer.Deserialize(ref reader, property.PropertyType, options);
if (CollectionPropertyValue is IList prospectiveList)
{
#if SKIP_EMPTY_LISTS
if (prospectiveList.Count != 0)
{
property.SetValue(value, CollectionPropertyValue);
@@ -275,6 +277,9 @@ private void ReadProperty(ref Utf8JsonReader reader, T value, string propertyNam
{
property.SetValue(value, null);
}
#else
property.SetValue(value, CollectionPropertyValue);
#endif
}
return;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Agents.Core.Serialization;
using Microsoft.Agents.Core.Serialization.Converters;
using Microsoft.Agents.Extensions.Teams.Models;
using System.Text.Json;

namespace Microsoft.Agents.Extensions.Teams.Serialization.Converters
{
// This is required because ConnectorConverter supports derived type handling.
// In this case for the 'Task' property of type TaskModuleResponseBase.
internal class MessagingExtensionAttachmentConverter : ConnectorConverter<MessagingExtensionAttachment>
{
/// <inheritdoc/>
protected override void ReadExtensionData(ref Utf8JsonReader reader, MessagingExtensionAttachment value, string propertyName, JsonSerializerOptions options)
{
var extensionData = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
value.Properties.Add(propertyName, extensionData);
}

/// <inheritdoc/>
protected override bool TryReadExtensionData(ref Utf8JsonReader reader, MessagingExtensionAttachment value, string propertyName, JsonSerializerOptions options)
{
if (!propertyName.Equals(nameof(value.Properties)))
{
return false;
}

var propertyValue = JsonSerializer.Deserialize<object>(ref reader, options);

foreach (var element in propertyValue.ToJsonElements())
{
value.Properties.Add(element.Key, element.Value);
}

return true;
}

/// <inheritdoc/>
protected override bool TryWriteExtensionData(Utf8JsonWriter writer, MessagingExtensionAttachment value, string propertyName)
{
if (!propertyName.Equals(nameof(value.Properties)))
{
return false;
}

foreach (var extensionData in value.Properties)
{
writer.WritePropertyName(extensionData.Key);
extensionData.Value.WriteTo(writer);
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"commandId":"commandId","commandContext":"commandContext","botMessagePreviewAction":"botMessagePreviewAction","botActivityPreview":[{"type":"message"}],"messagePayload":{"id":"id","replyToId":"replyToId","messageType":"messageType","createdDateTime":"2025-06-15T13:45:30","lastModifiedDateTime":"2025-07-17T14:46:40","deleted":false,"subject":"subject","summary":"summary","importance":"importance","locale":"locale","from":{"user":{"userIdentityType":"userIdentityType","id":"id","displayName":"displayName"},"application":{"applicationIdentityType":"applicationIdentityType","id":"id","displayName":"displayName"},"conversation":{"conversationIdentityType":"conversationIdentityType","id":"id","displayName":"displayName"}},"body":{"contentType":"contentType","content":"content"},"attachmentLayout":"attachmentLayout","attachments":[{"id":"id","contentType":"contentType","contentUrl":"contentUrl","content":"content","name":"name","thumbnailUrl":"thumbnailUrl"}],"mentions":[{"id":1,"mentionText":"mentionText","mentioned":{}}],"reactions":[{"reactionType":"reactionType","createdDateTime":"createdDateTime","user":{}}]}}
{"commandId":"commandId","commandContext":"commandContext","botMessagePreviewAction":"botMessagePreviewAction","botActivityPreview":[{"type":"message","membersAdded":[],"membersRemoved":[],"reactionsAdded":[],"reactionsRemoved":[],"attachments":[],"entities":[],"listenFor":[],"textHighlights":[]}],"messagePayload":{"id":"id","replyToId":"replyToId","messageType":"messageType","createdDateTime":"2025-06-15T13:45:30","lastModifiedDateTime":"2025-07-17T14:46:40","deleted":false,"subject":"subject","summary":"summary","importance":"importance","locale":"locale","from":{"user":{"userIdentityType":"userIdentityType","id":"id","displayName":"displayName"},"application":{"applicationIdentityType":"applicationIdentityType","id":"id","displayName":"displayName"},"conversation":{"conversationIdentityType":"conversationIdentityType","id":"id","displayName":"displayName"}},"body":{"contentType":"contentType","content":"content"},"attachmentLayout":"attachmentLayout","attachments":[{"id":"id","contentType":"contentType","contentUrl":"contentUrl","content":"content","name":"name","thumbnailUrl":"thumbnailUrl"}],"mentions":[{"id":1,"mentionText":"mentionText","mentioned":{}}],"reactions":[{"reactionType":"reactionType","createdDateTime":"createdDateTime","user":{}}]}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"task":{"type":"message","prop1":"prop1"},"composeExtension":{"attachmentLayout":"attachmentLayout","type":"type","attachments":[{"properties":{}}],"suggestedActions":{"actions":[{"type":"type","title":"title","image":"image","imageAltText":"imageAltText","text":"text","displayText":"displayText","value":{"value":"value"},"channelData":{"channelData":"channelData"}}]},"text":"text","activityPreview":{}},"cacheInfo":{"cacheType":"cacheType","cacheDuration":1}}
{"task":{"type":"message","prop1":"prop1"},"composeExtension":{"attachmentLayout":"attachmentLayout","type":"type","attachments":[{}],"suggestedActions":{"actions":[{"type":"type","title":"title","image":"image","imageAltText":"imageAltText","text":"text","displayText":"displayText","value":{"value":"value"},"channelData":{"channelData":"channelData"}}]},"text":"text","activityPreview":{"membersAdded":[],"membersRemoved":[],"reactionsAdded":[],"reactionsRemoved":[],"attachments":[],"entities":[],"listenFor":[],"textHighlights":[]}},"cacheInfo":{"cacheType":"cacheType","cacheDuration":1}}
Original file line number Diff line number Diff line change
@@ -25,7 +25,11 @@ public void ConversationParametersRoundTrip()
var outJson = ProtocolJsonSerializer.ToJson(outConversation);

// Specifically, should include: "bot": {} and not "agent": {}
#if SKIP_EMPTY_LISTS
var outExpected = "{\"isGroup\":false,\"bot\":{\"id\":\"agent\"},\"members\":[{\"id\":\"member\"}],\"topicName\":\"topicName\",\"tenantId\":\"tenantId\",\"activity\":{\"type\":\"message\"}}";
#else
var outExpected = "{\"isGroup\":false,\"bot\":{\"id\":\"agent\"},\"members\":[{\"id\":\"member\"}],\"topicName\":\"topicName\",\"tenantId\":\"tenantId\",\"activity\":{\"type\":\"message\",\"membersAdded\":[],\"membersRemoved\":[],\"reactionsAdded\":[],\"reactionsRemoved\":[],\"attachments\":[],\"entities\":[],\"listenFor\":[],\"textHighlights\":[]}}";
#endif

Assert.Equal(outExpected, outJson);

Original file line number Diff line number Diff line change
@@ -58,7 +58,11 @@ public void InActivityConversationReferenceSerialize()
};

var outJson = ProtocolJsonSerializer.ToJson(activity);
#if SKIP_EMPTY_LISTS
var outExpected = "{\"relatesTo\":{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\"},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"}}";
#else
var outExpected = "{\"membersAdded\":[],\"membersRemoved\":[],\"reactionsAdded\":[],\"reactionsRemoved\":[],\"attachments\":[],\"entities\":[],\"relatesTo\":{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\"},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"},\"listenFor\":[],\"textHighlights\":[]}";
#endif
Assert.Equal(outExpected, outJson);
}
}
Original file line number Diff line number Diff line change
@@ -362,6 +362,7 @@ public void ValidateActivitySerializer(string baseFileName)
Assert.Equal(resultingText, outboundJson);
}

#if SKIP_EMPTY_LISTS
[Fact]
public void EmptyListDoesntSerialzie()
{
@@ -386,7 +387,7 @@ public void EmptyListDoesntSerialzie()
var expected = "{\"suggestedActions\":{\"to\":[\"test\"]}}";
Assert.Equal(expected, json);
}

#endif

private Activity RoundTrip(Activity outActivity)
{
@@ -408,6 +409,10 @@ private void AssertPropertyValues(Activity activity)
Assert.NotEmpty(activity.ReactionsAdded);
Assert.NotEmpty(activity.ReactionsRemoved);

Assert.NotNull(activity.Attachments);
Assert.NotNull(activity.ListenFor);
Assert.NotNull(activity.TextHighlights);

// validate .value, .channeldata and the activity additional properties are present
Assert.NotNull(activity.Value);
var valueTestObject = ProtocolJsonSerializer.ToObject<TestObjectClass>(activity.Value);
Original file line number Diff line number Diff line change
@@ -47,7 +47,6 @@
],
"locale": "en-US",
"text": "validation",
"attachments": [],
"entities": [
{
"type": "ClientCapabilities",
@@ -77,7 +76,7 @@
"TestObject": null
}
},
"listenFor": [],
"listenFor": null,
"textHighlights": [],
"cci_bot_id": "215797fa-5550-4f12-a967-c15437884964",
"cci_tenant_id": "9f6be790-4a16-4dd6-9850-44a0d2649aef",
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
{"type":"event","id":"470d790f-dcfb-4e7e-9d38-fdae48fcdba0","timestamp":"2024-11-14T03:21:53.5114019\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"channelData":{"streamType":"final","streamId":"e5084817-2e0d-4fca-bb27-3eca80ae49f8"},"replyToId":"406ffa1e-67a7-4ec9-9eef-f90cfbe0e389","valueType":"DynamicPlanStepBindUpdate","value":{"taskDialogId":"crd17_getsCurrentWeather.topic.MSNWeatherGetcurrentweather","arguments":{"units":"I","Location":"New York"}},"name":"DynamicPlanStepBindUpdate"}
{
"type": "event",
"id": "470d790f-dcfb-4e7e-9d38-fdae48fcdba0",
"timestamp": "2024-11-14T03:21:53.5114019\u002B00:00",
"channelId": "pva-published-engine-direct",
"from": {
"id": "9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab",
"name": "crd17_getsCurrentWeather",
"role": "bot"
},
"conversation": { "id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f" },
"recipient": {
"id": "94219457-c13e-44ca-b91c-24078a4997c6",
"aadObjectId": "94219457-c13e-44ca-b91c-24078a4997c6",
"role": "user"
},
"membersAdded": [],
"membersRemoved": [],
"reactionsAdded": [],
"reactionsRemoved": [],
"attachments": [],
"entities": [],
"channelData": {
"streamType": "final",
"streamId": "e5084817-2e0d-4fca-bb27-3eca80ae49f8"
},
"replyToId": "406ffa1e-67a7-4ec9-9eef-f90cfbe0e389",
"valueType": "DynamicPlanStepBindUpdate",
"value": {
"taskDialogId": "crd17_getsCurrentWeather.topic.MSNWeatherGetcurrentweather",
"arguments": {
"units": "I",
"Location": "New York"
}
},
"name": "DynamicPlanStepBindUpdate",
"listenFor": [],
"textHighlights": []
}
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
{"type":"message","id":"8a82a3f3-cddf-444b-b607-58c79f3486b8","timestamp":"2024-11-14T03:18:57.5507994\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"textFormat":"markdown","text":"Hello, I\u0027m Gets Current weather, a virtual assistant. I can answer general questions about the current weather as well as forecasts for today and tomorrow. Just so you are aware, I sometimes use AI to answer your questions.\n\n_**Note**: You can now customize this copilot\u0027s topics and knowledge for your own needs. For more information about this template and how to modify it, visit the template [documentation.](https://go.microsoft.com/fwlink/?linkid=2271069)_","speak":"Thanks for calling, how can I help?","inputHint":"acceptingInput","suggestedActions":{"actions":[{"type":"imBack","title":"What can I ask?","text":"What can I ask?","value":"What can I ask?"}]},"channelData":{"streamType":"final","streamId":"8a82a3f3-cddf-444b-b607-58c79f3486b8"},"replyToId":"b7b28891-a1a4-460d-ad37-11326f46fead"}
{
"type": "message",
"id": "8a82a3f3-cddf-444b-b607-58c79f3486b8",
"timestamp": "2024-11-14T03:18:57.5507994\u002B00:00",
"channelId": "pva-published-engine-direct",
"from": {
"id": "9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab",
"name": "crd17_getsCurrentWeather",
"role": "bot"
},
"conversation": { "id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f" },
"recipient": {
"id": "94219457-c13e-44ca-b91c-24078a4997c6",
"aadObjectId": "94219457-c13e-44ca-b91c-24078a4997c6",
"role": "user"
},
"textFormat": "markdown",
"membersAdded": [],
"membersRemoved": [],
"reactionsAdded": [],
"reactionsRemoved": [],
"text": "Hello, I\u0027m Gets Current weather, a virtual assistant. I can answer general questions about the current weather as well as forecasts for today and tomorrow. Just so you are aware, I sometimes use AI to answer your questions.\n\n_**Note**: You can now customize this copilot\u0027s topics and knowledge for your own needs. For more information about this template and how to modify it, visit the template [documentation.](https://go.microsoft.com/fwlink/?linkid=2271069)_",
"speak": "Thanks for calling, how can I help?",
"inputHint": "acceptingInput",
"suggestedActions": {
"to": [],
"actions": [
{
"type": "imBack",
"title": "What can I ask?",
"text": "What can I ask?",
"value": "What can I ask?"
}
]
},
"attachments": [],
"entities": [],
"channelData": {
"streamType": "final",
"streamId": "8a82a3f3-cddf-444b-b607-58c79f3486b8"
},
"replyToId": "b7b28891-a1a4-460d-ad37-11326f46fead",
"listenFor": [],
"textHighlights": []
}
Original file line number Diff line number Diff line change
@@ -8,18 +8,21 @@
"name": "crd17_getsCurrentWeather",
"role": "bot"
},
"conversation": {
"id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"
},
"conversation": { "id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f" },
"recipient": {
"id": "94219457-c13e-44ca-b91c-24078a4997c6",
"aadObjectId": "94219457-c13e-44ca-b91c-24078a4997c6",
"role": "user"
},
"textFormat": "markdown",
"membersAdded": [],
"membersRemoved": [],
"reactionsAdded": [],
"reactionsRemoved": [],
"text": "To clarify, did you mean:",
"inputHint": "acceptingInput",
"suggestedActions": {
"to": [],
"actions": [
{
"type": "imBack",
@@ -47,9 +50,13 @@
}
]
},
"attachments": [],
"entities": [],
"channelData": {
"streamType": "final",
"streamId": "80c0db85-7b1e-4a45-a10d-210e7bb0aefd"
},
"replyToId": "40d50db7-7fef-41c2-b804-8346ef266f3d"
"replyToId": "40d50db7-7fef-41c2-b804-8346ef266f3d",
"listenFor": [],
"textHighlights": []
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"type": "typing",
"id": "typing-1"
"id": "typing-1",
"membersAdded": [],
"membersRemoved": [],
"reactionsAdded": [],
"reactionsRemoved": [],
"attachments": [],
"entities": [],
"listenFor": [],
"textHighlights": []
}
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.