-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathOpenApiAiCapabilitiesExtension.cs
More file actions
395 lines (336 loc) · 14.9 KB
/
Copy pathOpenApiAiCapabilitiesExtension.cs
File metadata and controls
395 lines (336 loc) · 14.9 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using Kiota.Builder.Extensions;
using Microsoft.OpenApi;
namespace Kiota.Builder.OpenApiExtensions;
public class OpenApiAiCapabilitiesExtension : IOpenApiExtension
{
public static string Name => "x-ai-capabilities";
public ExtensionConfirmation? Confirmation
{
get; set;
}
public ExtensionResponseSemantics? ResponseSemantics
{
get; set;
}
public ExtensionSecurityInfo? SecurityInfo
{
get; set;
}
public static OpenApiAiCapabilitiesExtension Parse(JsonNode source)
{
if (source is not JsonObject rawObject) throw new ArgumentOutOfRangeException(nameof(source));
var extension = new OpenApiAiCapabilitiesExtension();
if (rawObject.TryGetPropertyValue(nameof(Confirmation).ToFirstCharacterLowerCase(), out var confirmation) && confirmation is JsonObject confirmationObject)
{
extension.Confirmation = ParseConfirmation(confirmationObject);
}
if (rawObject.TryGetPropertyValue(nameof(ResponseSemantics).ToFirstCharacterLowerCase().ToSnakeCase(), out var responseSemantics) && responseSemantics is JsonObject responseSemanticsObject)
{
extension.ResponseSemantics = ParseResponseSemantics(responseSemanticsObject);
}
if (rawObject.TryGetPropertyValue(nameof(SecurityInfo).ToFirstCharacterLowerCase().ToSnakeCase(), out var securityInfo) && securityInfo is JsonObject securityInfoObject)
{
extension.SecurityInfo = ParseSecurityInfo(securityInfoObject);
}
return extension;
}
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
ArgumentNullException.ThrowIfNull(writer);
// Only write the object if there's actual content to write
if (ResponseSemantics != null || Confirmation != null || SecurityInfo != null)
{
writer.WriteStartObject();
if (ResponseSemantics is not null)
{
writer.WritePropertyName(nameof(ResponseSemantics).ToFirstCharacterLowerCase().ToSnakeCase());
WriteResponseSemantics(writer, ResponseSemantics);
}
if (Confirmation is not null)
{
writer.WritePropertyName(nameof(Confirmation).ToFirstCharacterLowerCase());
WriteConfirmation(writer, Confirmation);
}
if (SecurityInfo is not null)
{
writer.WritePropertyName(nameof(SecurityInfo).ToFirstCharacterLowerCase().ToSnakeCase());
WriteSecurityInfo(writer, SecurityInfo);
}
writer.WriteEndObject();
}
}
private static ExtensionConfirmation ParseConfirmation(JsonObject source)
{
var confirmation = new ExtensionConfirmation();
if (source.TryGetPropertyValue(nameof(Confirmation.Type).ToFirstCharacterLowerCase(), out var type) &&
type is JsonValue typeValue && typeValue.GetValueKind() is JsonValueKind.String &&
typeValue.TryGetValue<string>(out var typeStrValue))
{
confirmation.Type = typeStrValue;
}
if (source.TryGetPropertyValue(nameof(Confirmation.Title).ToFirstCharacterLowerCase(), out var title) &&
title is JsonValue titleValue && titleValue.GetValueKind() is JsonValueKind.String &&
titleValue.TryGetValue<string>(out var titleStrValue))
{
confirmation.Title = titleStrValue;
}
if (source.TryGetPropertyValue(nameof(Confirmation.Body).ToFirstCharacterLowerCase(), out var body) &&
body is JsonValue bodyValue && bodyValue.GetValueKind() is JsonValueKind.String &&
bodyValue.TryGetValue<string>(out var bodyStrValue))
{
confirmation.Body = bodyStrValue;
}
return confirmation;
}
private void WriteConfirmation(IOpenApiWriter writer, ExtensionConfirmation confirmation)
{
writer.WriteStartObject();
if (!string.IsNullOrEmpty(confirmation.Type))
{
writer.WritePropertyName(nameof(Confirmation.Type).ToFirstCharacterLowerCase());
writer.WriteValue(confirmation.Type);
}
if (!string.IsNullOrEmpty(confirmation.Title))
{
writer.WritePropertyName(nameof(Confirmation.Title).ToFirstCharacterLowerCase());
writer.WriteValue(confirmation.Title);
}
if (!string.IsNullOrEmpty(confirmation.Body))
{
writer.WritePropertyName(nameof(Confirmation.Body).ToFirstCharacterLowerCase());
writer.WriteValue(confirmation.Body);
}
writer.WriteEndObject();
}
private static ExtensionResponseSemantics ParseResponseSemantics(JsonObject source)
{
var responseSemantics = new ExtensionResponseSemantics();
if (source.TryGetPropertyValue(nameof(ResponseSemantics.DataPath).ToFirstCharacterLowerCase().ToSnakeCase(), out var dataPath) &&
dataPath is JsonValue dataPathValue && dataPathValue.GetValueKind() is JsonValueKind.String &&
dataPathValue.TryGetValue<string>(out var dataPathStrValue))
{
responseSemantics.DataPath = dataPathStrValue;
}
if (source.TryGetPropertyValue(nameof(ResponseSemantics.StaticTemplate).ToFirstCharacterLowerCase().ToSnakeCase(), out var staticTemplate) &&
staticTemplate is JsonObject staticTemplateObj)
{
responseSemantics.StaticTemplate = staticTemplateObj;
}
if (source.TryGetPropertyValue(nameof(ResponseSemantics.Properties).ToFirstCharacterLowerCase(), out var properties) &&
properties is JsonObject propertiesObj)
{
responseSemantics.Properties = ParseResponseSemanticsProperties(propertiesObj);
}
if (source.TryGetPropertyValue(nameof(ResponseSemantics.OauthCardPath).ToFirstCharacterLowerCase().ToSnakeCase(), out var OauthCardPath) &&
OauthCardPath is JsonValue OauthCardPathValue && OauthCardPathValue.GetValueKind() is JsonValueKind.String &&
OauthCardPathValue.TryGetValue<string>(out var OauthCardPathStrValue))
{
responseSemantics.OauthCardPath = OauthCardPathStrValue;
}
return responseSemantics;
}
private void WriteResponseSemantics(IOpenApiWriter writer, ExtensionResponseSemantics responseSemantics)
{
writer.WriteStartObject();
writer.WritePropertyName(nameof(ResponseSemantics.DataPath).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(responseSemantics.DataPath);
if (responseSemantics.StaticTemplate != null && responseSemantics.StaticTemplate is JsonObject staticTemplateObj)
{
writer.WritePropertyName(nameof(ResponseSemantics.StaticTemplate).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteRaw(staticTemplateObj.ToJsonString());
}
if (responseSemantics.Properties != null)
{
writer.WritePropertyName(nameof(ResponseSemantics.Properties).ToFirstCharacterLowerCase());
WriteResponseSemanticsProperties(writer, responseSemantics.Properties);
}
if (!string.IsNullOrEmpty(responseSemantics.OauthCardPath))
{
writer.WritePropertyName(nameof(ResponseSemantics.OauthCardPath).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(responseSemantics.OauthCardPath);
}
writer.WriteEndObject();
}
private static ExtensionResponseSemanticsProperties ParseResponseSemanticsProperties(JsonObject source)
{
var properties = new ExtensionResponseSemanticsProperties();
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.Title).ToFirstCharacterLowerCase(), out var title) &&
title is JsonValue titleValue && titleValue.GetValueKind() is JsonValueKind.String &&
titleValue.TryGetValue<string>(out var titleStrValue))
{
properties.Title = titleStrValue;
}
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.Subtitle).ToFirstCharacterLowerCase(), out var subtitle) &&
subtitle is JsonValue subtitleValue && subtitleValue.GetValueKind() is JsonValueKind.String &&
subtitleValue.TryGetValue<string>(out var subtitleStrValue))
{
properties.Subtitle = subtitleStrValue;
}
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.Url).ToFirstCharacterLowerCase(), out var url) &&
url is JsonValue urlValue && urlValue.GetValueKind() is JsonValueKind.String &&
urlValue.TryGetValue<string>(out var urlStrValue))
{
properties.Url = urlStrValue;
}
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.ThumbnailUrl).ToFirstCharacterLowerCase().ToSnakeCase(), out var thumbnailUrl) &&
thumbnailUrl is JsonValue thumbnailUrlValue && thumbnailUrlValue.GetValueKind() is JsonValueKind.String &&
thumbnailUrlValue.TryGetValue<string>(out var thumbnailUrlStrValue))
{
properties.ThumbnailUrl = thumbnailUrlStrValue;
}
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.InformationProtectionLabel).ToFirstCharacterLowerCase().ToSnakeCase(), out var informationProtectionLabel) &&
informationProtectionLabel is JsonValue informationProtectionLabelValue && informationProtectionLabelValue.GetValueKind() is JsonValueKind.String &&
informationProtectionLabelValue.TryGetValue<string>(out var informationProtectionLabelStrValue))
{
properties.InformationProtectionLabel = informationProtectionLabelStrValue;
}
if (source.TryGetPropertyValue(nameof(ExtensionResponseSemanticsProperties.TemplateSelector).ToFirstCharacterLowerCase().ToSnakeCase(), out var templateSelector) &&
templateSelector is JsonValue templateSelectorValue && templateSelectorValue.GetValueKind() is JsonValueKind.String &&
templateSelectorValue.TryGetValue<string>(out var templateSelectorStrValue))
{
properties.TemplateSelector = templateSelectorStrValue;
}
return properties;
}
private void WriteResponseSemanticsProperties(IOpenApiWriter writer, ExtensionResponseSemanticsProperties properties)
{
writer.WriteStartObject();
if (!string.IsNullOrEmpty(properties.Title))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.Title).ToFirstCharacterLowerCase());
writer.WriteValue(properties.Title);
}
if (!string.IsNullOrEmpty(properties.Subtitle))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.Subtitle).ToFirstCharacterLowerCase());
writer.WriteValue(properties.Subtitle);
}
if (!string.IsNullOrEmpty(properties.Url))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.Url).ToFirstCharacterLowerCase());
writer.WriteValue(properties.Url);
}
if (!string.IsNullOrEmpty(properties.ThumbnailUrl))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.ThumbnailUrl).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(properties.ThumbnailUrl);
}
if (!string.IsNullOrEmpty(properties.InformationProtectionLabel))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.InformationProtectionLabel).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(properties.InformationProtectionLabel);
}
if (!string.IsNullOrEmpty(properties.TemplateSelector))
{
writer.WritePropertyName(nameof(ExtensionResponseSemanticsProperties.TemplateSelector).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteValue(properties.TemplateSelector);
}
writer.WriteEndObject();
}
private static ExtensionSecurityInfo ParseSecurityInfo(JsonObject source)
{
var securityInfo = new ExtensionSecurityInfo();
if (source.TryGetPropertyValue(nameof(ExtensionSecurityInfo.DataHandling).ToFirstCharacterLowerCase().ToSnakeCase(), out var dataHandling) &&
dataHandling is JsonArray dataHandlingArray)
{
var dataHandlingList = new List<string>();
foreach (var item in dataHandlingArray)
{
if (item is JsonValue valueItem && valueItem.GetValueKind() is JsonValueKind.String &&
valueItem.TryGetValue<string>(out var strValue))
{
dataHandlingList.Add(strValue);
}
}
securityInfo.DataHandling = dataHandlingList;
}
return securityInfo;
}
private void WriteSecurityInfo(IOpenApiWriter writer, ExtensionSecurityInfo securityInfo)
{
writer.WriteStartObject();
if (securityInfo.DataHandling != null && securityInfo.DataHandling.Count > 0)
{
writer.WritePropertyName(nameof(securityInfo.DataHandling).ToFirstCharacterLowerCase().ToSnakeCase());
writer.WriteStartArray();
foreach (var item in securityInfo.DataHandling)
{
writer.WriteValue(item);
}
writer.WriteEndArray();
}
writer.WriteEndObject();
}
}
public class ExtensionConfirmation
{
public string? Type
{
get; set;
}
public string? Title
{
get; set;
}
public string? Body
{
get; set;
}
}
public class ExtensionResponseSemantics
{
public string DataPath { get; set; } = string.Empty;
public object? StaticTemplate
{
get; set;
}
public ExtensionResponseSemanticsProperties? Properties
{
get; set;
}
public string? OauthCardPath
{
get; set;
}
}
public class ExtensionResponseSemanticsProperties
{
public string? Title
{
get; set;
}
public string? Subtitle
{
get; set;
}
#pragma warning disable CA1056 // URI-like properties should not be strings
public string? Url
{
get; set;
}
public string? ThumbnailUrl
{
get; set;
}
#pragma warning restore CA1056 // URI-like properties should not be strings
public string? InformationProtectionLabel
{
get; set;
}
public string? TemplateSelector
{
get; set;
}
}
public class ExtensionSecurityInfo
{
#pragma warning disable CA1002 // Do not expose generic lists
#pragma warning disable CA2227 // Collection properties should be read only
public List<string> DataHandling { get; set; } = new List<string>();
#pragma warning restore CA2227 // Collection properties should be read only
#pragma warning restore CA1002 // Do not expose generic lists
}