Skip to content

Commit aa1e909

Browse files
authored
Merge pull request #296 from microsoft/users/tracyboehrer/ac-fixes
AdaptiveCardInvokeResponseFactory.AdaptiveCard accepts wrong Value content
2 parents 489bc9e + 10dc0da commit aa1e909

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/libraries/Builder/Microsoft.Agents.Builder/App/AdaptiveCards/AdaptiveCardInvokeResponseFactory.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.Agents.Core.Models;
5+
using System.Net;
56

67
namespace Microsoft.Agents.Builder.App.AdaptiveCards
78
{
@@ -13,15 +14,15 @@ public static class AdaptiveCardInvokeResponseFactory
1314
/// <summary>
1415
/// Returns response with type "application/vnd.microsoft.card.adaptive".
1516
/// </summary>
16-
/// <param name="adaptiveCard">An Adaptive Card.</param>
17+
/// <param name="adaptiveCardJson">An AdaptiveCard JSON value.</param>
1718
/// <returns>The response that includes an Adaptive Card that the client should display.</returns>
18-
public static AdaptiveCardInvokeResponse AdaptiveCard(AdaptiveCard adaptiveCard)
19+
public static AdaptiveCardInvokeResponse AdaptiveCard(string adaptiveCardJson)
1920
{
2021
return new AdaptiveCardInvokeResponse
2122
{
2223
StatusCode = 200,
2324
Type = "application/vnd.microsoft.card.adaptive",
24-
Value = adaptiveCard
25+
Value = adaptiveCardJson
2526
};
2627
}
2728

@@ -39,5 +40,52 @@ public static AdaptiveCardInvokeResponse Message(string message)
3940
Value = message
4041
};
4142
}
43+
44+
/// <summary>
45+
/// Creates an Error of type "BadRequest" AdaptiveCardInvokeResponse.
46+
/// </summary>
47+
/// <param name="message"></param>
48+
public static AdaptiveCardInvokeResponse BadRequest(string message)
49+
{
50+
return Error(HttpStatusCode.BadRequest, "BadRequest", message);
51+
}
52+
53+
/// <summary>
54+
/// Creates an Error of type "NotSupported" AdaptiveCardInvokeResponse.
55+
/// </summary>
56+
/// <param name="message"></param>
57+
public static AdaptiveCardInvokeResponse NotSupported(string message)
58+
{
59+
return Error(HttpStatusCode.BadRequest, "NotSupported", message);
60+
}
61+
62+
/// <summary>
63+
/// Creates an Error of type InternalError AdaptiveCardInvokeResponse.
64+
/// </summary>
65+
/// <param name="message"></param>
66+
public static AdaptiveCardInvokeResponse InternalError(string message)
67+
{
68+
return Error(HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError.ToString(), message);
69+
}
70+
71+
/// <summary>
72+
/// Creates an Error AdaptiveCardInvokeResponse.
73+
/// </summary>
74+
/// <param name="message"></param>
75+
/// <param name="statusCode">Defaults to HttpStatusCode.BadRequest.</param>
76+
/// <param name="code">Defaults to HttpStatusCode.ToString()</param>
77+
public static AdaptiveCardInvokeResponse Error(HttpStatusCode statusCode, string code, string message)
78+
{
79+
return new AdaptiveCardInvokeResponse()
80+
{
81+
StatusCode = (int)statusCode,
82+
Type = "application/vnd.microsoft.error",
83+
Value = new Error()
84+
{
85+
Code = code ?? statusCode.ToString(),
86+
Message = message
87+
}
88+
};
89+
}
4290
}
43-
}
91+
}

src/libraries/Builder/Microsoft.Agents.Builder/Microsoft.Agents.Builder.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="AdaptiveCards" />
2322
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
2423
<PackageReference Include="Microsoft.Extensions.Logging" />
2524
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
2625
<PackageReference Include="System.Memory.Data" />
2726
<PackageReference Include="System.Text.Json" />
2827
<PackageReference Include="Microsoft.Extensions.Http" />
29-
<PackageReference Include="Newtonsoft.Json" />
3028
</ItemGroup>
3129

3230
<ItemGroup>

src/samples/Compat/SkillsDialog/DialogSkillBot/Dialogs/ActivityRouterDialog.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.Agents.Builder.Dialogs;
77
using Microsoft.Agents.Core.Models;
8-
using Newtonsoft.Json;
8+
using Microsoft.Agents.Core.Serialization;
99

1010
namespace DialogSkillBot.Dialogs
1111
{
@@ -97,7 +97,7 @@ private static async Task<DialogTurnResult> BeginGetWeather(WaterfallStepContext
9797
var location = new Location();
9898
if (activity.Value != null)
9999
{
100-
location = JsonConvert.DeserializeObject<Location>(JsonConvert.SerializeObject(activity.Value));
100+
location = ProtocolJsonSerializer.ToObject<Location>(activity.Value);
101101
}
102102

103103
// We haven't implemented the GetWeatherDialog so we just display a TODO message.
@@ -113,14 +113,14 @@ private async Task<DialogTurnResult> BeginBookFlight(WaterfallStepContext stepCo
113113
var bookingDetails = new BookingDetails();
114114
if (activity.Value != null)
115115
{
116-
bookingDetails = JsonConvert.DeserializeObject<BookingDetails>(JsonConvert.SerializeObject(activity.Value));
116+
bookingDetails = ProtocolJsonSerializer.ToObject<BookingDetails>(activity.Value);
117117
}
118118

119119
// Start the booking dialog.
120120
var bookingDialog = FindDialog(nameof(BookingDialog));
121121
return await stepContext.BeginDialogAsync(bookingDialog.Id, bookingDetails, cancellationToken);
122122
}
123123

124-
private string GetObjectAsJsonString(object value) => value == null ? string.Empty : JsonConvert.SerializeObject(value);
124+
private string GetObjectAsJsonString(object value) => value == null ? string.Empty : ProtocolJsonSerializer.ToJson(value);
125125
}
126126
}

0 commit comments

Comments
 (0)