Skip to content

Commit add0d02

Browse files
committed
use the adaptive cards library instead of string builder
1 parent 26c7dee commit add0d02

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

src/Kiota.Builder/Kiota.Builder.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<NoWarn>$(NoWarn);CS8785;NU5048;NU5104;CA1724;CA1055;CA1848;CA1308;CA1822</NoWarn>
3636
</PropertyGroup>
3737
<ItemGroup>
38+
<PackageReference Include="AdaptiveCards" Version="3.1.0" />
3839
<PackageReference Include="AsyncKeyedLock" Version="7.1.4" />
3940
<PackageReference Include="DotNet.Glob" Version="3.1.3" />
4041
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />

src/Kiota.Builder/Plugins/AdaptiveCardGenerator.cs

+33-12
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,60 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using AdaptiveCards;
78
using Microsoft.OpenApi.Models;
89

910
namespace Kiota.Builder.Plugins
1011
{
1112
public class AdaptiveCardGenerator
1213
{
13-
public AdaptiveCardGenerator() { }
14+
public AdaptiveCardGenerator()
15+
{
16+
}
1417

1518
public string GenerateAdaptiveCard(OpenApiOperation operation)
1619
{
1720
ArgumentNullException.ThrowIfNull(operation);
18-
21+
1922
var responses = operation.Responses;
2023
var response = responses["200"];
2124
ArgumentNullException.ThrowIfNull(response);
22-
25+
2326
var schema = response.Content["application/json"].Schema;
2427
ArgumentNullException.ThrowIfNull(schema);
2528

2629
var properties = schema.Properties;
2730
ArgumentNullException.ThrowIfNull(properties);
28-
29-
var builder = new StringBuilder();
30-
builder.Append("{\"type\":\"AdaptiveCard\",\"$schema\":\"https://adaptivecards.io/schemas/adaptive-card.json\",\"version\":\"1.5\",\"body\":[");
31+
32+
AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 5));
33+
3134
foreach (var property in properties)
3235
{
33-
builder.Append("{\"type\":\"TextBlock\",\"text\":\"" + property.Key + ": ${if(" + property.Key + ", " + property.Key + ", 'N/A')}\"");
34-
builder.Append(",\"wrap\":true}");
35-
builder.Append(',');
36+
37+
if (property.Value.Type == "string" && property.Value.Format == "uri")
38+
{
39+
card.Body.Add(new AdaptiveImage()
40+
{
41+
Url = new Uri($"${{{property.Key}}}"),
42+
Size = AdaptiveImageSize.Large,
43+
});
44+
}
45+
else if (property.Value.Type == "array")
46+
{
47+
card.Body.Add(new AdaptiveTextBlock()
48+
{
49+
Text = $"${{{property.Key}.join(', ')}}",
50+
});
51+
}
52+
else
53+
{
54+
card.Body.Add(new AdaptiveTextBlock()
55+
{
56+
Text = $"${{{property.Key}, {property.Key}, 'N/A'}}",
57+
});
58+
}
3659
}
37-
builder.Remove(builder.Length - 1, 1);
38-
builder.Append("]}");
39-
return builder.ToString();
60+
return card.ToJson();
4061
}
4162
}
4263
}

tests/Kiota.Builder.Tests/Plugins/AdaptiveCardGeneratorTests.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.Json;
56
using System.Threading.Tasks;
67
using Kiota.Builder.Plugins;
78
using Microsoft.OpenApi.Models;
@@ -45,11 +46,15 @@ public void GenerateAdaptiveCardFromOperation()
4546
}
4647
}
4748
};
48-
var expected = @"{""type"":""AdaptiveCard"",""$schema"":""https://adaptivecards.io/schemas/adaptive-card.json"",""version"":""1.5"",""body"":[{""type"":""TextBlock"",""text"":""name: ${if(name, name, 'N/A')}"",""wrap"":true},{""type"":""TextBlock"",""text"":""age: ${if(age, age, 'N/A')}"",""wrap"":true}]}";
49+
string expected = "{\r\n \"type\": \"AdaptiveCard\",\r\n \"version\": \"1.5\",\r\n \"body\": [\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${name, name, 'N/A'}\"\r\n },\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${age, age, 'N/A'}\"\r\n }\r\n ]\r\n}";
4950

5051
var generator = new AdaptiveCardGenerator();
5152
var card = generator.GenerateAdaptiveCard(sample);
52-
Assert.Equal(expected, card);
53+
54+
var expectedJson = JsonDocument.Parse(expected).RootElement.GetRawText();
55+
var actualJson = JsonDocument.Parse(card).RootElement.GetRawText();
56+
57+
Assert.Equal(expectedJson, actualJson);
5358
}
5459
}
5560
}

tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ public async Task GeneratesManifestWithAdaptiveCardAsync()
900900
var manifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, $"{inputPluginName.ToLower()}-apiplugin.json"));
901901
using var jsonDocument = JsonDocument.Parse(manifestContent);
902902
var resultingManifest = PluginManifestDocument.Load(jsonDocument.RootElement);
903-
string expectedStaticTemplate = "{\"type\":\"AdaptiveCard\",\"$schema\":\"https://adaptivecards.io/schemas/adaptive-card.json\",\"version\":\"1.5\",\"body\":[{\"type\":\"TextBlock\",\"text\":\"id: ${if(id, id, 'N/A')}\",\"wrap\":true},{\"type\":\"TextBlock\",\"text\":\"displayName: ${if(displayName, displayName, 'N/A')}\",\"wrap\":true},{\"type\":\"TextBlock\",\"text\":\"otherNames: ${if(otherNames, otherNames, 'N/A')}\",\"wrap\":true},{\"type\":\"TextBlock\",\"text\":\"importance: ${if(importance, importance, 'N/A')}\",\"wrap\":true}]}";
903+
string expectedStaticTemplate = "{\r\n \"type\": \"AdaptiveCard\",\r\n \"version\": \"1.5\",\r\n \"body\": [\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${id, id, 'N/A'}\"\r\n },\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${displayName, displayName, 'N/A'}\"\r\n },\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${otherNames.join(', ')}\"\r\n },\r\n {\r\n \"type\": \"TextBlock\",\r\n \"text\": \"${importance, importance, 'N/A'}\"\r\n }\r\n ]\r\n}";
904904

905905
Assert.NotNull(resultingManifest.Document);
906906
Assert.Equal($"{inputPluginName.ToLower()}-openapi.yml", resultingManifest.Document.Runtimes.OfType<OpenApiRuntime>().First().Spec.Url);
@@ -910,7 +910,7 @@ public async Task GeneratesManifestWithAdaptiveCardAsync()
910910
if (actualJson.HasValue)
911911
{
912912
// Properties to compare
913-
List<string> propertiesToCompare = new List<string> { "type", "$schema", "version" };
913+
List<string> propertiesToCompare = new List<string> { "type", "version", "body"};
914914

915915
// Compare properties
916916
foreach (string prop in propertiesToCompare)

0 commit comments

Comments
 (0)