-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathOpenApiAiAdaptiveCardExtension.cs
More file actions
84 lines (77 loc) · 3.54 KB
/
Copy pathOpenApiAiAdaptiveCardExtension.cs
File metadata and controls
84 lines (77 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using Kiota.Builder.Extensions;
using Microsoft.OpenApi;
namespace Kiota.Builder.OpenApiExtensions;
public class OpenApiAiAdaptiveCardExtension : IOpenApiExtension
{
public static string Name => "x-ai-adaptive-card";
public string? DataPath
{
get; set;
}
public string? File
{
get; set;
}
public string? Title
{
get; set;
}
#pragma warning disable CA1056 // URI-like properties should not be strings
public string? Url
#pragma warning restore CA1056 // URI-like properties should not be strings
{
get; set;
}
public static OpenApiAiAdaptiveCardExtension Parse(JsonNode source)
{
// We are supporting empty extension to avoid creating the template when emitting from typespec scenario
var emptyExtension = new OpenApiAiAdaptiveCardExtension();
if (source is not JsonObject rawObject)
return emptyExtension;
var extension = new OpenApiAiAdaptiveCardExtension();
if (rawObject.TryGetPropertyValue(nameof(DataPath).ToFirstCharacterLowerCase().ToSnakeCase(), out var dataPath) && dataPath is JsonValue dataPathValue && dataPathValue.GetValueKind() is JsonValueKind.String && dataPathValue.TryGetValue<string>(out var dataPathStrValue))
{
extension.DataPath = dataPathStrValue;
}
if (rawObject.TryGetPropertyValue(nameof(File).ToFirstCharacterLowerCase(), out var file) && file is JsonValue fileValue && fileValue.GetValueKind() is JsonValueKind.String && fileValue.TryGetValue<string>(out var fileStrValue))
{
extension.File = fileStrValue;
}
if (rawObject.TryGetPropertyValue(nameof(Title).ToFirstCharacterLowerCase(), out var title) && title is JsonValue titleValue && titleValue.GetValueKind() is JsonValueKind.String && titleValue.TryGetValue<string>(out var titleStrValue))
{
extension.Title = titleStrValue;
}
if (rawObject.TryGetPropertyValue(nameof(Url).ToFirstCharacterLowerCase(), out var url) && url is JsonValue urlValue && urlValue.GetValueKind() is JsonValueKind.String && urlValue.TryGetValue<string>(out var urlStrValue))
{
extension.Url = urlStrValue;
}
// We are supporting empty extension to avoid creating the template when emitting from typespec scenario
if (string.IsNullOrEmpty(extension.DataPath) || string.IsNullOrEmpty(extension.File) || string.IsNullOrEmpty(extension.Title))
return emptyExtension;
return extension;
}
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
ArgumentNullException.ThrowIfNull(writer);
// Only write the object if there's actual content to write
if (!string.IsNullOrEmpty(DataPath) && !string.IsNullOrEmpty(File) && !string.IsNullOrEmpty(Title))
{
writer.WriteStartObject();
writer.WritePropertyName(nameof(DataPath).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(DataPath);
writer.WritePropertyName(nameof(File).ToFirstCharacterLowerCase());
writer.WriteValue(File);
writer.WritePropertyName(nameof(Title).ToFirstCharacterLowerCase());
writer.WriteValue(Title);
if (!string.IsNullOrEmpty(Url))
{
writer.WritePropertyName(nameof(Url).ToFirstCharacterLowerCase());
writer.WriteValue(Url);
}
writer.WriteEndObject();
}
}
}