@microsoft/m365-spec-parser 0.2.12-alpha.d9ea25f7c.0 → 0.2.12-alpha.eaecfe55f.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1636 +1,1596 @@
1
1
  import SwaggerParser from '@apidevtools/swagger-parser';
2
2
 
3
- /******************************************************************************
4
- Copyright (c) Microsoft Corporation.
5
-
6
- Permission to use, copy, modify, and/or distribute this software for any
7
- purpose with or without fee is hereby granted.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- PERFORMANCE OF THIS SOFTWARE.
16
- ***************************************************************************** */
17
-
18
- function __awaiter(thisArg, _arguments, P, generator) {
19
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
- return new (P || (P = Promise))(function (resolve, reject) {
21
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
- step((generator = generator.apply(thisArg, _arguments || [])).next());
25
- });
26
- }
27
-
28
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
29
- var e = new Error(message);
30
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
31
- };
32
-
33
- // Copyright (c) Microsoft Corporation.
34
- /**
35
- * An enum that represents the types of errors that can occur during validation.
36
- */
37
- var ErrorType;
38
- (function (ErrorType) {
39
- ErrorType["SpecNotValid"] = "spec-not-valid";
40
- ErrorType["RemoteRefNotSupported"] = "remote-ref-not-supported";
41
- ErrorType["NoServerInformation"] = "no-server-information";
42
- ErrorType["UrlProtocolNotSupported"] = "url-protocol-not-supported";
43
- ErrorType["RelativeServerUrlNotSupported"] = "relative-server-url-not-supported";
44
- ErrorType["NoSupportedApi"] = "no-supported-api";
45
- ErrorType["NoExtraAPICanBeAdded"] = "no-extra-api-can-be-added";
46
- ErrorType["AddedAPINotInOriginalSpec"] = "added-api-not-in-original-spec";
47
- ErrorType["ResolveServerUrlFailed"] = "resolve-server-url-failed";
48
- ErrorType["SwaggerNotSupported"] = "swagger-not-supported";
49
- ErrorType["MultipleAuthNotSupported"] = "multiple-auth-not-supported";
50
- ErrorType["SpecVersionNotSupported"] = "spec-version-not-supported";
51
- ErrorType["CircularReferenceNotSupported"] = "circular-reference-not-supported";
52
- ErrorType["ListFailed"] = "list-failed";
53
- ErrorType["listSupportedAPIInfoFailed"] = "list-supported-api-info-failed";
54
- ErrorType["FilterSpecFailed"] = "filter-spec-failed";
55
- ErrorType["UpdateManifestFailed"] = "update-manifest-failed";
56
- ErrorType["GenerateAdaptiveCardFailed"] = "generate-adaptive-card-failed";
57
- ErrorType["GenerateFailed"] = "generate-failed";
58
- ErrorType["ValidateFailed"] = "validate-failed";
59
- ErrorType["GetSpecFailed"] = "get-spec-failed";
60
- ErrorType["AuthTypeIsNotSupported"] = "auth-type-is-not-supported";
61
- ErrorType["MissingOperationId"] = "missing-operation-id";
62
- ErrorType["PostBodyContainMultipleMediaTypes"] = "post-body-contain-multiple-media-types";
63
- ErrorType["ResponseContainMultipleMediaTypes"] = "response-contain-multiple-media-types";
64
- ErrorType["ResponseJsonIsEmpty"] = "response-json-is-empty";
65
- ErrorType["PostBodyContainsRequiredUnsupportedSchema"] = "post-body-contains-required-unsupported-schema";
66
- ErrorType["ParamsContainRequiredUnsupportedSchema"] = "params-contain-required-unsupported-schema";
67
- ErrorType["ExceededRequiredParamsLimit"] = "exceeded-required-params-limit";
68
- ErrorType["NoParameter"] = "no-parameter";
69
- ErrorType["NoAPIInfo"] = "no-api-info";
70
- ErrorType["MethodNotAllowed"] = "method-not-allowed";
71
- ErrorType["UrlPathNotExist"] = "url-path-not-exist";
72
- ErrorType["Cancelled"] = "cancelled";
73
- ErrorType["Unknown"] = "unknown";
74
- ErrorType["AddAuthFailed"] = "add-auth-failed";
75
- })(ErrorType || (ErrorType = {}));
76
- /**
77
- * An enum that represents the types of warnings that can occur during validation.
78
- */
79
- var WarningType;
80
- (function (WarningType) {
81
- WarningType["OperationIdMissing"] = "operationid-missing";
82
- WarningType["GenerateCardFailed"] = "generate-card-failed";
83
- WarningType["OperationOnlyContainsOptionalParam"] = "operation-only-contains-optional-param";
84
- WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
85
- WarningType["OpenAPI31ConvertTo30"] = "openapi31-convert-to-30";
86
- WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
87
- WarningType["UnsupportedAuthType"] = "unsupported-auth-type";
88
- WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
89
- WarningType["Unknown"] = "unknown";
90
- })(WarningType || (WarningType = {}));
91
- /**
92
- * An enum that represents the validation status of an OpenAPI specification file.
93
- */
94
- var ValidationStatus;
95
- (function (ValidationStatus) {
96
- ValidationStatus[ValidationStatus["Valid"] = 0] = "Valid";
97
- ValidationStatus[ValidationStatus["Warning"] = 1] = "Warning";
98
- ValidationStatus[ValidationStatus["Error"] = 2] = "Error";
99
- })(ValidationStatus || (ValidationStatus = {}));
100
- var ProjectType;
101
- (function (ProjectType) {
102
- ProjectType[ProjectType["Copilot"] = 0] = "Copilot";
103
- ProjectType[ProjectType["SME"] = 1] = "SME";
104
- ProjectType[ProjectType["TeamsAi"] = 2] = "TeamsAi";
105
- })(ProjectType || (ProjectType = {}));
106
- var AdaptiveCardUpdateStrategy;
107
- (function (AdaptiveCardUpdateStrategy) {
108
- AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["CreateNew"] = 0] = "CreateNew";
109
- AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["KeepExisting"] = 1] = "KeepExisting";
3
+ // Copyright (c) Microsoft Corporation.
4
+ /**
5
+ * An enum that represents the types of errors that can occur during validation.
6
+ */
7
+ var ErrorType;
8
+ (function (ErrorType) {
9
+ ErrorType["SpecNotValid"] = "spec-not-valid";
10
+ ErrorType["RemoteRefNotSupported"] = "remote-ref-not-supported";
11
+ ErrorType["NoServerInformation"] = "no-server-information";
12
+ ErrorType["UrlProtocolNotSupported"] = "url-protocol-not-supported";
13
+ ErrorType["RelativeServerUrlNotSupported"] = "relative-server-url-not-supported";
14
+ ErrorType["NoSupportedApi"] = "no-supported-api";
15
+ ErrorType["NoExtraAPICanBeAdded"] = "no-extra-api-can-be-added";
16
+ ErrorType["AddedAPINotInOriginalSpec"] = "added-api-not-in-original-spec";
17
+ ErrorType["ResolveServerUrlFailed"] = "resolve-server-url-failed";
18
+ ErrorType["SwaggerNotSupported"] = "swagger-not-supported";
19
+ ErrorType["MultipleAuthNotSupported"] = "multiple-auth-not-supported";
20
+ ErrorType["SpecVersionNotSupported"] = "spec-version-not-supported";
21
+ ErrorType["CircularReferenceNotSupported"] = "circular-reference-not-supported";
22
+ ErrorType["ListFailed"] = "list-failed";
23
+ ErrorType["listSupportedAPIInfoFailed"] = "list-supported-api-info-failed";
24
+ ErrorType["FilterSpecFailed"] = "filter-spec-failed";
25
+ ErrorType["UpdateManifestFailed"] = "update-manifest-failed";
26
+ ErrorType["GenerateAdaptiveCardFailed"] = "generate-adaptive-card-failed";
27
+ ErrorType["GenerateFailed"] = "generate-failed";
28
+ ErrorType["ValidateFailed"] = "validate-failed";
29
+ ErrorType["GetSpecFailed"] = "get-spec-failed";
30
+ ErrorType["AuthTypeIsNotSupported"] = "auth-type-is-not-supported";
31
+ ErrorType["MissingOperationId"] = "missing-operation-id";
32
+ ErrorType["PostBodyContainMultipleMediaTypes"] = "post-body-contain-multiple-media-types";
33
+ ErrorType["ResponseContainMultipleMediaTypes"] = "response-contain-multiple-media-types";
34
+ ErrorType["ResponseJsonIsEmpty"] = "response-json-is-empty";
35
+ ErrorType["PostBodyContainsRequiredUnsupportedSchema"] = "post-body-contains-required-unsupported-schema";
36
+ ErrorType["ParamsContainRequiredUnsupportedSchema"] = "params-contain-required-unsupported-schema";
37
+ ErrorType["ExceededRequiredParamsLimit"] = "exceeded-required-params-limit";
38
+ ErrorType["NoParameter"] = "no-parameter";
39
+ ErrorType["NoAPIInfo"] = "no-api-info";
40
+ ErrorType["MethodNotAllowed"] = "method-not-allowed";
41
+ ErrorType["UrlPathNotExist"] = "url-path-not-exist";
42
+ ErrorType["Cancelled"] = "cancelled";
43
+ ErrorType["Unknown"] = "unknown";
44
+ ErrorType["AddAuthFailed"] = "add-auth-failed";
45
+ })(ErrorType || (ErrorType = {}));
46
+ /**
47
+ * An enum that represents the types of warnings that can occur during validation.
48
+ */
49
+ var WarningType;
50
+ (function (WarningType) {
51
+ WarningType["OperationIdMissing"] = "operationid-missing";
52
+ WarningType["GenerateCardFailed"] = "generate-card-failed";
53
+ WarningType["OperationOnlyContainsOptionalParam"] = "operation-only-contains-optional-param";
54
+ WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
55
+ WarningType["OpenAPI31ConvertTo30"] = "openapi31-convert-to-30";
56
+ WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
57
+ WarningType["UnsupportedAuthType"] = "unsupported-auth-type";
58
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
59
+ WarningType["Unknown"] = "unknown";
60
+ })(WarningType || (WarningType = {}));
61
+ /**
62
+ * An enum that represents the validation status of an OpenAPI specification file.
63
+ */
64
+ var ValidationStatus;
65
+ (function (ValidationStatus) {
66
+ ValidationStatus[ValidationStatus["Valid"] = 0] = "Valid";
67
+ ValidationStatus[ValidationStatus["Warning"] = 1] = "Warning";
68
+ ValidationStatus[ValidationStatus["Error"] = 2] = "Error";
69
+ })(ValidationStatus || (ValidationStatus = {}));
70
+ var ProjectType;
71
+ (function (ProjectType) {
72
+ ProjectType[ProjectType["Copilot"] = 0] = "Copilot";
73
+ ProjectType[ProjectType["SME"] = 1] = "SME";
74
+ ProjectType[ProjectType["TeamsAi"] = 2] = "TeamsAi";
75
+ })(ProjectType || (ProjectType = {}));
76
+ var AdaptiveCardUpdateStrategy;
77
+ (function (AdaptiveCardUpdateStrategy) {
78
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["CreateNew"] = 0] = "CreateNew";
79
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["KeepExisting"] = 1] = "KeepExisting";
110
80
  })(AdaptiveCardUpdateStrategy || (AdaptiveCardUpdateStrategy = {}));
111
81
 
112
- // Copyright (c) Microsoft Corporation.
113
- class SpecParserError extends Error {
114
- constructor(message, errorType) {
115
- super(message);
116
- this.errorType = errorType;
117
- }
82
+ // Copyright (c) Microsoft Corporation.
83
+ class SpecParserError extends Error {
84
+ errorType;
85
+ constructor(message, errorType) {
86
+ super(message);
87
+ this.errorType = errorType;
88
+ }
118
89
  }
119
90
 
120
- // Copyright (c) Microsoft Corporation.
121
- class ConstantString {
122
- }
123
- ConstantString.CancelledMessage = "Operation is cancelled.";
124
- ConstantString.NoServerInformation = "No server information is found in the OpenAPI description document.";
125
- ConstantString.RemoteRefNotSupported = "Remote reference is not supported: %s.";
126
- ConstantString.MissingOperationId = "Missing operationIds: %s.";
127
- ConstantString.NoSupportedApi = "No supported API is found in the OpenAPI description document: only GET and POST methods are supported, additionally, there can be at most one required parameter, and no auth is allowed.";
128
- ConstantString.AdditionalPropertiesNotSupported = "'additionalProperties' is not supported, and will be ignored.";
129
- ConstantString.SchemaNotSupported = "'oneOf', 'allOf', 'anyOf', and 'not' schema are not supported: %s.";
130
- ConstantString.UnknownSchema = "Unknown schema: %s.";
131
- ConstantString.UrlProtocolNotSupported = "Server url is not correct: protocol %s is not supported, you should use https protocol instead.";
132
- ConstantString.RelativeServerUrlNotSupported = "Server url is not correct: relative server url is not supported.";
133
- ConstantString.ResolveServerUrlFailed = "Unable to resolve the server URL: please make sure that the environment variable %s is defined.";
134
- ConstantString.OperationOnlyContainsOptionalParam = "Operation %s contains multiple optional parameters. The first optional parameter is used for this command.";
135
- ConstantString.ConvertSwaggerToOpenAPI = "The Swagger 2.0 file has been converted to OpenAPI 3.0.";
136
- ConstantString.OpenAPI31ConvertTo30 = "OpenAPI 3.1 document has been converted to OpenAPI 3.0.";
137
- ConstantString.SwaggerNotSupported = "Swagger 2.0 is not supported. Please convert to OpenAPI 3.0 manually before proceeding.";
138
- ConstantString.SpecVersionNotSupported = "Unsupported OpenAPI version %s. Please use version 3.0.x.";
139
- ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are unsupported. Ensure all selected APIs use identical authentication.";
140
- ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
141
- ConstantString.AuthTypeIsNotSupported = "Unsupported authorization type in API '%s'. No authorization will be used.";
142
- ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
143
- ConstantString.FuncDescriptionTooLong = "The description of the function '%s' is too long. The current length is %s characters, while the maximum allowed length is %s characters.";
144
- ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
145
- ConstantString.WrappedCardVersion = "1.0";
146
- ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/v1.19/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
147
- ConstantString.WrappedCardResponseLayout = "list";
148
- ConstantString.GetMethod = "get";
149
- ConstantString.PostMethod = "post";
150
- ConstantString.AdaptiveCardVersion = "1.5";
151
- ConstantString.AdaptiveCardSchema = "https://adaptivecards.io/schemas/adaptive-card.json";
152
- ConstantString.AdaptiveCardType = "AdaptiveCard";
153
- ConstantString.TextBlockType = "TextBlock";
154
- ConstantString.ImageType = "Image";
155
- ConstantString.ContainerType = "Container";
156
- ConstantString.RegistrationIdPostfix = "REGISTRATION_ID";
157
- ConstantString.ResponseCodeFor20X = [
158
- "200",
159
- "201",
160
- "202",
161
- "203",
162
- "204",
163
- "205",
164
- "206",
165
- "207",
166
- "208",
167
- "226",
168
- "2XX",
169
- "default",
170
- ];
171
- ConstantString.AllOperationMethods = [
172
- "get",
173
- "post",
174
- "put",
175
- "delete",
176
- "patch",
177
- "head",
178
- "options",
179
- "trace",
180
- ];
181
- // TODO: update after investigating the usage of these constants.
182
- ConstantString.WellknownResultNames = [
183
- "result",
184
- "data",
185
- "items",
186
- "root",
187
- "matches",
188
- "queries",
189
- "list",
190
- "output",
191
- ];
192
- ConstantString.WellknownTitleName = ["title", "name", "summary", "caption", "subject", "label"];
193
- ConstantString.WellknownSubtitleName = [
194
- "subtitle",
195
- "id",
196
- "uid",
197
- "description",
198
- "desc",
199
- "detail",
200
- ];
201
- ConstantString.WellknownImageName = [
202
- "image",
203
- "icon",
204
- "avatar",
205
- "picture",
206
- "photo",
207
- "logo",
208
- "pic",
209
- "thumbnail",
210
- "img",
211
- ];
212
- ConstantString.ShortDescriptionMaxLens = 80;
213
- ConstantString.FullDescriptionMaxLens = 4000;
214
- ConstantString.CommandDescriptionMaxLens = 128;
215
- ConstantString.ParameterDescriptionMaxLens = 128;
216
- ConstantString.ConversationStarterMaxLens = 50;
217
- ConstantString.CommandTitleMaxLens = 32;
218
- ConstantString.ParameterTitleMaxLens = 32;
219
- ConstantString.SMERequiredParamsMaxNum = 5;
220
- ConstantString.FunctionDescriptionMaxLens = 100;
221
- ConstantString.DefaultPluginId = "plugin_1";
222
- ConstantString.PluginManifestSchema = "https://developer.microsoft.com/json-schemas/copilot/plugin/v2.2/schema.json";
91
+ // Copyright (c) Microsoft Corporation.
92
+ class ConstantString {
93
+ static CancelledMessage = "Operation is cancelled.";
94
+ static NoServerInformation = "No server information is found in the OpenAPI description document.";
95
+ static RemoteRefNotSupported = "Remote reference is not supported: %s.";
96
+ static MissingOperationId = "Missing operationIds: %s.";
97
+ static NoSupportedApi = "No supported API is found in the OpenAPI description document: only GET and POST methods are supported, additionally, there can be at most one required parameter, and no auth is allowed.";
98
+ static AdditionalPropertiesNotSupported = "'additionalProperties' is not supported, and will be ignored.";
99
+ static SchemaNotSupported = "'oneOf', 'allOf', 'anyOf', and 'not' schema are not supported: %s.";
100
+ static UnknownSchema = "Unknown schema: %s.";
101
+ static UrlProtocolNotSupported = "Server url is not correct: protocol %s is not supported, you should use https protocol instead.";
102
+ static RelativeServerUrlNotSupported = "Server url is not correct: relative server url is not supported.";
103
+ static ResolveServerUrlFailed = "Unable to resolve the server URL: please make sure that the environment variable %s is defined.";
104
+ static OperationOnlyContainsOptionalParam = "Operation %s contains multiple optional parameters. The first optional parameter is used for this command.";
105
+ static ConvertSwaggerToOpenAPI = "The Swagger 2.0 file has been converted to OpenAPI 3.0.";
106
+ static OpenAPI31ConvertTo30 = "OpenAPI 3.1 document has been converted to OpenAPI 3.0.";
107
+ static SwaggerNotSupported = "Swagger 2.0 is not supported. Please convert to OpenAPI 3.0 manually before proceeding.";
108
+ static SpecVersionNotSupported = "Unsupported OpenAPI version %s. Please use version 3.0.x.";
109
+ static MultipleAuthNotSupported = "Multiple authentication methods are unsupported. Ensure all selected APIs use identical authentication.";
110
+ static OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
111
+ static AuthTypeIsNotSupported = "Unsupported authorization type in API '%s'. No authorization will be used.";
112
+ static UnsupportedSchema = "Unsupported schema in %s %s: %s";
113
+ static FuncDescriptionTooLong = "The description of the function '%s' is too long. The current length is %s characters, while the maximum allowed length is %s characters.";
114
+ static GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
115
+ static WrappedCardVersion = "1.0";
116
+ static WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/v1.19/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
117
+ static WrappedCardResponseLayout = "list";
118
+ static GetMethod = "get";
119
+ static PostMethod = "post";
120
+ static AdaptiveCardVersion = "1.5";
121
+ static AdaptiveCardSchema = "https://adaptivecards.io/schemas/adaptive-card.json";
122
+ static AdaptiveCardType = "AdaptiveCard";
123
+ static TextBlockType = "TextBlock";
124
+ static ImageType = "Image";
125
+ static ContainerType = "Container";
126
+ static RegistrationIdPostfix = "REGISTRATION_ID";
127
+ static ResponseCodeFor20X = [
128
+ "200",
129
+ "201",
130
+ "202",
131
+ "203",
132
+ "204",
133
+ "205",
134
+ "206",
135
+ "207",
136
+ "208",
137
+ "226",
138
+ "2XX",
139
+ "default",
140
+ ];
141
+ static AllOperationMethods = [
142
+ "get",
143
+ "post",
144
+ "put",
145
+ "delete",
146
+ "patch",
147
+ "head",
148
+ "options",
149
+ "trace",
150
+ ];
151
+ // TODO: update after investigating the usage of these constants.
152
+ static WellknownResultNames = [
153
+ "result",
154
+ "data",
155
+ "items",
156
+ "root",
157
+ "matches",
158
+ "queries",
159
+ "list",
160
+ "output",
161
+ ];
162
+ static WellknownTitleName = ["title", "name", "summary", "caption", "subject", "label"];
163
+ static WellknownSubtitleName = [
164
+ "subtitle",
165
+ "id",
166
+ "uid",
167
+ "description",
168
+ "desc",
169
+ "detail",
170
+ ];
171
+ static WellknownImageName = [
172
+ "image",
173
+ "icon",
174
+ "avatar",
175
+ "picture",
176
+ "photo",
177
+ "logo",
178
+ "pic",
179
+ "thumbnail",
180
+ "img",
181
+ ];
182
+ static ShortDescriptionMaxLens = 80;
183
+ static FullDescriptionMaxLens = 4000;
184
+ static CommandDescriptionMaxLens = 128;
185
+ static ParameterDescriptionMaxLens = 128;
186
+ static ConversationStarterMaxLens = 50;
187
+ static CommandTitleMaxLens = 32;
188
+ static ParameterTitleMaxLens = 32;
189
+ static SMERequiredParamsMaxNum = 5;
190
+ static FunctionDescriptionMaxLens = 100;
191
+ static DefaultPluginId = "plugin_1";
192
+ static PluginManifestSchema = "https://developer.microsoft.com/json-schemas/copilot/plugin/v2.2/schema.json";
193
+ }
223
194
 
224
- // Copyright (c) Microsoft Corporation.
225
- class Utils {
226
- static isObjectSchema(schema) {
227
- return schema.type === "object" || (!schema.type && !!schema.properties);
228
- }
229
- static containMultipleMediaTypes(bodyObject) {
230
- return Object.keys((bodyObject === null || bodyObject === void 0 ? void 0 : bodyObject.content) || {}).length > 1;
231
- }
232
- static isBearerTokenAuth(authScheme) {
233
- return authScheme.type === "http" && authScheme.scheme.toLowerCase() === "bearer";
234
- }
235
- static isAPIKeyAuth(authScheme) {
236
- return authScheme.type === "apiKey";
237
- }
238
- static isAPIKeyAuthButNotInCookie(authScheme) {
239
- return authScheme.type === "apiKey" && authScheme.in !== "cookie";
240
- }
241
- static isOAuthWithAuthCodeFlow(authScheme) {
242
- return !!(authScheme.type === "oauth2" &&
243
- authScheme.flows &&
244
- authScheme.flows.authorizationCode);
245
- }
246
- static isNotSupportedAuth(authSchemeArray) {
247
- if (authSchemeArray.length === 0) {
248
- return false;
249
- }
250
- if (authSchemeArray.length > 0 && authSchemeArray.every((auths) => auths.length > 1)) {
251
- return true;
252
- }
253
- for (const auths of authSchemeArray) {
254
- if (auths.length === 1) {
255
- if (Utils.isOAuthWithAuthCodeFlow(auths[0].authScheme) ||
256
- Utils.isBearerTokenAuth(auths[0].authScheme) ||
257
- Utils.isAPIKeyAuthButNotInCookie(auths[0].authScheme)) {
258
- return false;
259
- }
260
- }
261
- }
262
- return true;
263
- }
264
- static getAuthArray(securities, spec) {
265
- var _a;
266
- const result = [];
267
- const securitySchemas = (_a = spec.components) === null || _a === void 0 ? void 0 : _a.securitySchemes;
268
- const securitiesArr = securities !== null && securities !== void 0 ? securities : spec.security;
269
- if (securitiesArr && securitySchemas) {
270
- for (let i = 0; i < securitiesArr.length; i++) {
271
- const security = securitiesArr[i];
272
- const authArray = [];
273
- for (const name in security) {
274
- const auth = securitySchemas[name];
275
- authArray.push({
276
- authScheme: auth,
277
- name: name,
278
- });
279
- }
280
- if (authArray.length > 0) {
281
- result.push(authArray);
282
- }
283
- }
284
- }
285
- result.sort((a, b) => a[0].name.localeCompare(b[0].name));
286
- return result;
287
- }
288
- static getAuthMap(spec) {
289
- const authMap = {};
290
- for (const url in spec.paths) {
291
- for (const method in spec.paths[url]) {
292
- const operation = spec.paths[url][method];
293
- const authArray = Utils.getAuthArray(operation.security, spec);
294
- if (authArray && authArray.length > 0) {
295
- const currentAuth = authArray[0][0];
296
- authMap[operation.operationId] = currentAuth;
297
- }
298
- }
299
- }
300
- return authMap;
301
- }
302
- static getAuthInfo(spec) {
303
- let authInfo = undefined;
304
- for (const url in spec.paths) {
305
- for (const method in spec.paths[url]) {
306
- const operation = spec.paths[url][method];
307
- const authArray = Utils.getAuthArray(operation.security, spec);
308
- if (authArray && authArray.length > 0) {
309
- const currentAuth = authArray[0][0];
310
- if (!authInfo) {
311
- authInfo = authArray[0][0];
312
- }
313
- else if (authInfo.name !== currentAuth.name) {
314
- throw new SpecParserError(ConstantString.MultipleAuthNotSupported, ErrorType.MultipleAuthNotSupported);
315
- }
316
- }
317
- }
318
- }
319
- return authInfo;
320
- }
321
- static updateFirstLetter(str) {
322
- return str.charAt(0).toUpperCase() + str.slice(1);
323
- }
324
- static getResponseJson(operationObject, allowMultipleMediaType = false) {
325
- var _a;
326
- let json = {};
327
- let multipleMediaType = false;
328
- for (const code of ConstantString.ResponseCodeFor20X) {
329
- const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
330
- if (!responseObject) {
331
- continue;
332
- }
333
- multipleMediaType = Utils.containMultipleMediaTypes(responseObject);
334
- if (!allowMultipleMediaType && multipleMediaType) {
335
- json = {};
336
- continue;
337
- }
338
- const mediaObj = Utils.getJsonContentType(responseObject);
339
- if (Object.keys(mediaObj).length > 0) {
340
- json = mediaObj;
341
- return { json, multipleMediaType };
342
- }
343
- }
344
- return { json, multipleMediaType };
345
- }
346
- static getJsonContentType(responseObject) {
347
- if (responseObject.content) {
348
- for (const contentType of Object.keys(responseObject.content)) {
349
- // json media type can also be "application/json; charset=utf-8"
350
- if (contentType.indexOf("application/json") >= 0) {
351
- return responseObject.content[contentType];
352
- }
353
- }
354
- }
355
- return {};
356
- }
357
- static convertPathToCamelCase(path) {
358
- const pathSegments = path.split(/[./{]/);
359
- const camelCaseSegments = pathSegments.map((segment) => {
360
- segment = segment.replace(/}/g, "");
361
- return segment.charAt(0).toUpperCase() + segment.slice(1);
362
- });
363
- const camelCasePath = camelCaseSegments.join("");
364
- return camelCasePath;
365
- }
366
- static getUrlProtocol(urlString) {
367
- try {
368
- const url = new URL(urlString);
369
- return url.protocol;
370
- }
371
- catch (err) {
372
- return undefined;
373
- }
374
- }
375
- static resolveEnv(str) {
376
- const placeHolderReg = /\${{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*}}/g;
377
- let matches = placeHolderReg.exec(str);
378
- let newStr = str;
379
- while (matches != null) {
380
- const envVar = matches[1];
381
- const envVal = process.env[envVar];
382
- if (!envVal) {
383
- throw new Error(Utils.format(ConstantString.ResolveServerUrlFailed, envVar));
384
- }
385
- else {
386
- newStr = newStr.replace(matches[0], envVal);
387
- }
388
- matches = placeHolderReg.exec(str);
389
- }
390
- return newStr;
391
- }
392
- static checkServerUrl(servers, allowHttp = false) {
393
- const errors = [];
394
- let serverUrl;
395
- try {
396
- serverUrl = Utils.resolveEnv(servers[0].url);
397
- }
398
- catch (err) {
399
- errors.push({
400
- type: ErrorType.ResolveServerUrlFailed,
401
- content: err.message,
402
- data: servers,
403
- });
404
- return errors;
405
- }
406
- const protocol = Utils.getUrlProtocol(serverUrl);
407
- if (!protocol) {
408
- // Relative server url is not supported
409
- errors.push({
410
- type: ErrorType.RelativeServerUrlNotSupported,
411
- content: ConstantString.RelativeServerUrlNotSupported,
412
- data: servers,
413
- });
414
- }
415
- else if (protocol !== "https:" && !(protocol === "http:" && allowHttp)) {
416
- const protocolString = protocol.slice(0, -1);
417
- errors.push({
418
- type: ErrorType.UrlProtocolNotSupported,
419
- content: Utils.format(ConstantString.UrlProtocolNotSupported, protocol.slice(0, -1)),
420
- data: protocolString,
421
- });
422
- }
423
- return errors;
424
- }
425
- static validateServer(spec, options) {
426
- var _a;
427
- const errors = [];
428
- let hasTopLevelServers = false;
429
- let hasPathLevelServers = false;
430
- let hasOperationLevelServers = false;
431
- const allowHttp = options.projectType === ProjectType.Copilot;
432
- if (spec.servers && spec.servers.length >= 1) {
433
- hasTopLevelServers = true;
434
- // for multiple server, we only use the first url
435
- const serverErrors = Utils.checkServerUrl(spec.servers, allowHttp);
436
- errors.push(...serverErrors);
437
- }
438
- const paths = spec.paths;
439
- for (const path in paths) {
440
- const methods = paths[path];
441
- if ((methods === null || methods === void 0 ? void 0 : methods.servers) && methods.servers.length >= 1) {
442
- hasPathLevelServers = true;
443
- const serverErrors = Utils.checkServerUrl(methods.servers, allowHttp);
444
- errors.push(...serverErrors);
445
- }
446
- for (const method in methods) {
447
- const operationObject = methods[method];
448
- if (((_a = options.allowMethods) === null || _a === void 0 ? void 0 : _a.includes(method)) && operationObject) {
449
- if ((operationObject === null || operationObject === void 0 ? void 0 : operationObject.servers) && operationObject.servers.length >= 1) {
450
- hasOperationLevelServers = true;
451
- const serverErrors = Utils.checkServerUrl(operationObject.servers, allowHttp);
452
- errors.push(...serverErrors);
453
- }
454
- }
455
- }
456
- }
457
- if (!hasTopLevelServers && !hasPathLevelServers && !hasOperationLevelServers) {
458
- errors.push({
459
- type: ErrorType.NoServerInformation,
460
- content: ConstantString.NoServerInformation,
461
- });
462
- }
463
- return errors;
464
- }
465
- static isWellKnownName(name, wellknownNameList) {
466
- for (let i = 0; i < wellknownNameList.length; i++) {
467
- name = name.replace(/_/g, "").replace(/-/g, "");
468
- if (name.toLowerCase().includes(wellknownNameList[i])) {
469
- return true;
470
- }
471
- }
472
- return false;
473
- }
474
- static generateParametersFromSchema(schema, name, allowMultipleParameters, isRequired = false) {
475
- var _a, _b;
476
- const requiredParams = [];
477
- const optionalParams = [];
478
- if (schema.type === "string" ||
479
- schema.type === "integer" ||
480
- schema.type === "boolean" ||
481
- schema.type === "number") {
482
- const parameter = {
483
- name: name,
484
- title: Utils.updateFirstLetter(name).slice(0, ConstantString.ParameterTitleMaxLens),
485
- description: ((_a = schema.description) !== null && _a !== void 0 ? _a : "").slice(0, ConstantString.ParameterDescriptionMaxLens),
486
- };
487
- if (allowMultipleParameters) {
488
- Utils.updateParameterWithInputType(schema, parameter);
489
- }
490
- if (isRequired && schema.default === undefined) {
491
- parameter.isRequired = true;
492
- requiredParams.push(parameter);
493
- }
494
- else {
495
- optionalParams.push(parameter);
496
- }
497
- }
498
- else if (Utils.isObjectSchema(schema)) {
499
- const { properties } = schema;
500
- for (const property in properties) {
501
- let isRequired = false;
502
- if (schema.required && ((_b = schema.required) === null || _b === void 0 ? void 0 : _b.indexOf(property)) >= 0) {
503
- isRequired = true;
504
- }
505
- const [requiredP, optionalP] = Utils.generateParametersFromSchema(properties[property], property, allowMultipleParameters, isRequired);
506
- requiredParams.push(...requiredP);
507
- optionalParams.push(...optionalP);
508
- }
509
- }
510
- return [requiredParams, optionalParams];
511
- }
512
- static updateParameterWithInputType(schema, param) {
513
- if (schema.enum) {
514
- param.inputType = "choiceset";
515
- param.choices = [];
516
- for (let i = 0; i < schema.enum.length; i++) {
517
- param.choices.push({
518
- title: schema.enum[i],
519
- value: schema.enum[i],
520
- });
521
- }
522
- }
523
- else if (schema.type === "string") {
524
- param.inputType = "text";
525
- }
526
- else if (schema.type === "integer" || schema.type === "number") {
527
- param.inputType = "number";
528
- }
529
- else if (schema.type === "boolean") {
530
- param.inputType = "toggle";
531
- }
532
- if (schema.default) {
533
- param.value = schema.default;
534
- }
535
- }
536
- static parseApiInfo(operationItem, options) {
537
- var _a, _b;
538
- const requiredParams = [];
539
- const optionalParams = [];
540
- const paramObject = operationItem.parameters;
541
- if (paramObject) {
542
- paramObject.forEach((param) => {
543
- var _a;
544
- const parameter = {
545
- name: param.name,
546
- title: Utils.updateFirstLetter(param.name).slice(0, ConstantString.ParameterTitleMaxLens),
547
- description: ((_a = param.description) !== null && _a !== void 0 ? _a : "").slice(0, ConstantString.ParameterDescriptionMaxLens),
548
- };
549
- const schema = param.schema;
550
- if (options.allowMultipleParameters && schema) {
551
- Utils.updateParameterWithInputType(schema, parameter);
552
- }
553
- if (param.in !== "header" && param.in !== "cookie") {
554
- if (param.required && (schema === null || schema === void 0 ? void 0 : schema.default) === undefined) {
555
- parameter.isRequired = true;
556
- requiredParams.push(parameter);
557
- }
558
- else {
559
- optionalParams.push(parameter);
560
- }
561
- }
562
- });
563
- }
564
- if (operationItem.requestBody) {
565
- const requestBody = operationItem.requestBody;
566
- const requestJson = requestBody.content["application/json"];
567
- if (Object.keys(requestJson).length !== 0) {
568
- const schema = requestJson.schema;
569
- const [requiredP, optionalP] = Utils.generateParametersFromSchema(schema, "requestBody", !!options.allowMultipleParameters, requestBody.required);
570
- requiredParams.push(...requiredP);
571
- optionalParams.push(...optionalP);
572
- }
573
- }
574
- const operationId = operationItem.operationId;
575
- const parameters = [...requiredParams, ...optionalParams];
576
- const command = {
577
- context: ["compose"],
578
- type: "query",
579
- title: ((_a = operationItem.summary) !== null && _a !== void 0 ? _a : "").slice(0, ConstantString.CommandTitleMaxLens),
580
- id: operationId,
581
- parameters: parameters,
582
- description: ((_b = operationItem.description) !== null && _b !== void 0 ? _b : "").slice(0, ConstantString.CommandDescriptionMaxLens),
583
- };
584
- return command;
585
- }
586
- static format(str, ...args) {
587
- let index = 0;
588
- return str.replace(/%s/g, () => {
589
- const arg = args[index++];
590
- return arg !== undefined ? arg : "";
591
- });
592
- }
593
- static getSafeRegistrationIdEnvName(authName) {
594
- if (!authName) {
595
- return "";
596
- }
597
- let safeRegistrationIdEnvName = authName.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
598
- if (!safeRegistrationIdEnvName.match(/^[A-Z]/)) {
599
- safeRegistrationIdEnvName = "PREFIX_" + safeRegistrationIdEnvName;
600
- }
601
- return safeRegistrationIdEnvName;
602
- }
603
- static getServerObject(spec, method, path) {
604
- const pathObj = spec.paths[path];
605
- const operationObject = pathObj[method];
606
- const rootServer = spec.servers && spec.servers[0];
607
- const methodServer = spec.paths[path].servers && spec.paths[path].servers[0];
608
- const operationServer = operationObject.servers && operationObject.servers[0];
609
- const serverUrl = operationServer || methodServer || rootServer;
610
- return serverUrl;
611
- }
612
- static getAuthSchemaObject(authType, authParameters) {
613
- switch (authType) {
614
- case "oauth":
615
- case "microsoft-entra":
616
- return {
617
- type: "oauth2",
618
- flows: {
619
- authorizationCode: {
620
- authorizationUrl: authParameters.authorizationUrl,
621
- tokenUrl: authParameters.tokenUrl,
622
- refreshUrl: authParameters.refreshUrl,
623
- scopes: authParameters.scopes,
624
- },
625
- },
626
- };
627
- case "api-key":
628
- return {
629
- type: "apiKey",
630
- in: authParameters.in,
631
- name: authParameters.name,
632
- };
633
- case "bearer-token":
634
- default:
635
- return {
636
- type: "http",
637
- scheme: "bearer",
638
- };
639
- }
640
- }
195
+ // Copyright (c) Microsoft Corporation.
196
+ class Utils {
197
+ static isObjectSchema(schema) {
198
+ return schema.type === "object" || (!schema.type && !!schema.properties);
199
+ }
200
+ static containMultipleMediaTypes(bodyObject) {
201
+ return Object.keys(bodyObject?.content || {}).length > 1;
202
+ }
203
+ static isBearerTokenAuth(authScheme) {
204
+ return authScheme.type === "http" && authScheme.scheme.toLowerCase() === "bearer";
205
+ }
206
+ static isAPIKeyAuth(authScheme) {
207
+ return authScheme.type === "apiKey";
208
+ }
209
+ static isAPIKeyAuthButNotInCookie(authScheme) {
210
+ return authScheme.type === "apiKey" && authScheme.in !== "cookie";
211
+ }
212
+ static isOAuthWithAuthCodeFlow(authScheme) {
213
+ return !!(authScheme.type === "oauth2" &&
214
+ authScheme.flows &&
215
+ authScheme.flows.authorizationCode);
216
+ }
217
+ static isNotSupportedAuth(authSchemeArray) {
218
+ if (authSchemeArray.length === 0) {
219
+ return false;
220
+ }
221
+ if (authSchemeArray.length > 0 && authSchemeArray.every((auths) => auths.length > 1)) {
222
+ return true;
223
+ }
224
+ for (const auths of authSchemeArray) {
225
+ if (auths.length === 1) {
226
+ if (Utils.isOAuthWithAuthCodeFlow(auths[0].authScheme) ||
227
+ Utils.isBearerTokenAuth(auths[0].authScheme) ||
228
+ Utils.isAPIKeyAuthButNotInCookie(auths[0].authScheme)) {
229
+ return false;
230
+ }
231
+ }
232
+ }
233
+ return true;
234
+ }
235
+ static getAuthArray(securities, spec) {
236
+ const result = [];
237
+ const securitySchemas = spec.components?.securitySchemes;
238
+ const securitiesArr = securities ?? spec.security;
239
+ if (securitiesArr && securitySchemas) {
240
+ for (let i = 0; i < securitiesArr.length; i++) {
241
+ const security = securitiesArr[i];
242
+ const authArray = [];
243
+ for (const name in security) {
244
+ const auth = securitySchemas[name];
245
+ authArray.push({
246
+ authScheme: auth,
247
+ name: name,
248
+ });
249
+ }
250
+ if (authArray.length > 0) {
251
+ result.push(authArray);
252
+ }
253
+ }
254
+ }
255
+ result.sort((a, b) => a[0].name.localeCompare(b[0].name));
256
+ return result;
257
+ }
258
+ static getAuthMap(spec) {
259
+ const authMap = {};
260
+ for (const url in spec.paths) {
261
+ for (const method in spec.paths[url]) {
262
+ const operation = spec.paths[url][method];
263
+ const authArray = Utils.getAuthArray(operation.security, spec);
264
+ if (authArray && authArray.length > 0) {
265
+ const currentAuth = authArray[0][0];
266
+ authMap[operation.operationId] = currentAuth;
267
+ }
268
+ }
269
+ }
270
+ return authMap;
271
+ }
272
+ static getAuthInfo(spec) {
273
+ let authInfo = undefined;
274
+ for (const url in spec.paths) {
275
+ for (const method in spec.paths[url]) {
276
+ const operation = spec.paths[url][method];
277
+ const authArray = Utils.getAuthArray(operation.security, spec);
278
+ if (authArray && authArray.length > 0) {
279
+ const currentAuth = authArray[0][0];
280
+ if (!authInfo) {
281
+ authInfo = authArray[0][0];
282
+ }
283
+ else if (authInfo.name !== currentAuth.name) {
284
+ throw new SpecParserError(ConstantString.MultipleAuthNotSupported, ErrorType.MultipleAuthNotSupported);
285
+ }
286
+ }
287
+ }
288
+ }
289
+ return authInfo;
290
+ }
291
+ static updateFirstLetter(str) {
292
+ return str.charAt(0).toUpperCase() + str.slice(1);
293
+ }
294
+ static getResponseJson(operationObject, allowMultipleMediaType = false) {
295
+ let json = {};
296
+ let multipleMediaType = false;
297
+ for (const code of ConstantString.ResponseCodeFor20X) {
298
+ const responseObject = operationObject?.responses?.[code];
299
+ if (!responseObject) {
300
+ continue;
301
+ }
302
+ multipleMediaType = Utils.containMultipleMediaTypes(responseObject);
303
+ if (!allowMultipleMediaType && multipleMediaType) {
304
+ json = {};
305
+ continue;
306
+ }
307
+ const mediaObj = Utils.getJsonContentType(responseObject);
308
+ if (Object.keys(mediaObj).length > 0) {
309
+ json = mediaObj;
310
+ return { json, multipleMediaType };
311
+ }
312
+ }
313
+ return { json, multipleMediaType };
314
+ }
315
+ static getJsonContentType(responseObject) {
316
+ if (responseObject.content) {
317
+ for (const contentType of Object.keys(responseObject.content)) {
318
+ // json media type can also be "application/json; charset=utf-8"
319
+ if (contentType.indexOf("application/json") >= 0) {
320
+ return responseObject.content[contentType];
321
+ }
322
+ }
323
+ }
324
+ return {};
325
+ }
326
+ static convertPathToCamelCase(path) {
327
+ const pathSegments = path.split(/[./{]/);
328
+ const camelCaseSegments = pathSegments.map((segment) => {
329
+ segment = segment.replace(/}/g, "");
330
+ return segment.charAt(0).toUpperCase() + segment.slice(1);
331
+ });
332
+ const camelCasePath = camelCaseSegments.join("");
333
+ return camelCasePath;
334
+ }
335
+ static getUrlProtocol(urlString) {
336
+ try {
337
+ const url = new URL(urlString);
338
+ return url.protocol;
339
+ }
340
+ catch (err) {
341
+ return undefined;
342
+ }
343
+ }
344
+ static resolveEnv(str) {
345
+ const placeHolderReg = /\${{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*}}/g;
346
+ let matches = placeHolderReg.exec(str);
347
+ let newStr = str;
348
+ while (matches != null) {
349
+ const envVar = matches[1];
350
+ const envVal = process.env[envVar];
351
+ if (!envVal) {
352
+ throw new Error(Utils.format(ConstantString.ResolveServerUrlFailed, envVar));
353
+ }
354
+ else {
355
+ newStr = newStr.replace(matches[0], envVal);
356
+ }
357
+ matches = placeHolderReg.exec(str);
358
+ }
359
+ return newStr;
360
+ }
361
+ static checkServerUrl(servers, allowHttp = false) {
362
+ const errors = [];
363
+ let serverUrl;
364
+ try {
365
+ serverUrl = Utils.resolveEnv(servers[0].url);
366
+ }
367
+ catch (err) {
368
+ errors.push({
369
+ type: ErrorType.ResolveServerUrlFailed,
370
+ content: err.message,
371
+ data: servers,
372
+ });
373
+ return errors;
374
+ }
375
+ const protocol = Utils.getUrlProtocol(serverUrl);
376
+ if (!protocol) {
377
+ // Relative server url is not supported
378
+ errors.push({
379
+ type: ErrorType.RelativeServerUrlNotSupported,
380
+ content: ConstantString.RelativeServerUrlNotSupported,
381
+ data: servers,
382
+ });
383
+ }
384
+ else if (protocol !== "https:" && !(protocol === "http:" && allowHttp)) {
385
+ const protocolString = protocol.slice(0, -1);
386
+ errors.push({
387
+ type: ErrorType.UrlProtocolNotSupported,
388
+ content: Utils.format(ConstantString.UrlProtocolNotSupported, protocol.slice(0, -1)),
389
+ data: protocolString,
390
+ });
391
+ }
392
+ return errors;
393
+ }
394
+ static validateServer(spec, options) {
395
+ const errors = [];
396
+ let hasTopLevelServers = false;
397
+ let hasPathLevelServers = false;
398
+ let hasOperationLevelServers = false;
399
+ const allowHttp = options.projectType === ProjectType.Copilot;
400
+ if (spec.servers && spec.servers.length >= 1) {
401
+ hasTopLevelServers = true;
402
+ // for multiple server, we only use the first url
403
+ const serverErrors = Utils.checkServerUrl(spec.servers, allowHttp);
404
+ errors.push(...serverErrors);
405
+ }
406
+ const paths = spec.paths;
407
+ for (const path in paths) {
408
+ const methods = paths[path];
409
+ if (methods?.servers && methods.servers.length >= 1) {
410
+ hasPathLevelServers = true;
411
+ const serverErrors = Utils.checkServerUrl(methods.servers, allowHttp);
412
+ errors.push(...serverErrors);
413
+ }
414
+ for (const method in methods) {
415
+ const operationObject = methods[method];
416
+ if (options.allowMethods?.includes(method) && operationObject) {
417
+ if (operationObject?.servers && operationObject.servers.length >= 1) {
418
+ hasOperationLevelServers = true;
419
+ const serverErrors = Utils.checkServerUrl(operationObject.servers, allowHttp);
420
+ errors.push(...serverErrors);
421
+ }
422
+ }
423
+ }
424
+ }
425
+ if (!hasTopLevelServers && !hasPathLevelServers && !hasOperationLevelServers) {
426
+ errors.push({
427
+ type: ErrorType.NoServerInformation,
428
+ content: ConstantString.NoServerInformation,
429
+ });
430
+ }
431
+ return errors;
432
+ }
433
+ static isWellKnownName(name, wellknownNameList) {
434
+ for (let i = 0; i < wellknownNameList.length; i++) {
435
+ name = name.replace(/_/g, "").replace(/-/g, "");
436
+ if (name.toLowerCase().includes(wellknownNameList[i])) {
437
+ return true;
438
+ }
439
+ }
440
+ return false;
441
+ }
442
+ static generateParametersFromSchema(schema, name, allowMultipleParameters, isRequired = false) {
443
+ const requiredParams = [];
444
+ const optionalParams = [];
445
+ if (schema.type === "string" ||
446
+ schema.type === "integer" ||
447
+ schema.type === "boolean" ||
448
+ schema.type === "number") {
449
+ const parameter = {
450
+ name: name,
451
+ title: Utils.updateFirstLetter(name).slice(0, ConstantString.ParameterTitleMaxLens),
452
+ description: (schema.description ?? "").slice(0, ConstantString.ParameterDescriptionMaxLens),
453
+ };
454
+ if (allowMultipleParameters) {
455
+ Utils.updateParameterWithInputType(schema, parameter);
456
+ }
457
+ if (isRequired && schema.default === undefined) {
458
+ parameter.isRequired = true;
459
+ requiredParams.push(parameter);
460
+ }
461
+ else {
462
+ optionalParams.push(parameter);
463
+ }
464
+ }
465
+ else if (Utils.isObjectSchema(schema)) {
466
+ const { properties } = schema;
467
+ for (const property in properties) {
468
+ let isRequired = false;
469
+ if (schema.required && schema.required?.indexOf(property) >= 0) {
470
+ isRequired = true;
471
+ }
472
+ const [requiredP, optionalP] = Utils.generateParametersFromSchema(properties[property], property, allowMultipleParameters, isRequired);
473
+ requiredParams.push(...requiredP);
474
+ optionalParams.push(...optionalP);
475
+ }
476
+ }
477
+ return [requiredParams, optionalParams];
478
+ }
479
+ static updateParameterWithInputType(schema, param) {
480
+ if (schema.enum) {
481
+ param.inputType = "choiceset";
482
+ param.choices = [];
483
+ for (let i = 0; i < schema.enum.length; i++) {
484
+ param.choices.push({
485
+ title: schema.enum[i],
486
+ value: schema.enum[i],
487
+ });
488
+ }
489
+ }
490
+ else if (schema.type === "string") {
491
+ param.inputType = "text";
492
+ }
493
+ else if (schema.type === "integer" || schema.type === "number") {
494
+ param.inputType = "number";
495
+ }
496
+ else if (schema.type === "boolean") {
497
+ param.inputType = "toggle";
498
+ }
499
+ if (schema.default) {
500
+ param.value = schema.default;
501
+ }
502
+ }
503
+ static parseApiInfo(operationItem, options) {
504
+ const requiredParams = [];
505
+ const optionalParams = [];
506
+ const paramObject = operationItem.parameters;
507
+ if (paramObject) {
508
+ paramObject.forEach((param) => {
509
+ const parameter = {
510
+ name: param.name,
511
+ title: Utils.updateFirstLetter(param.name).slice(0, ConstantString.ParameterTitleMaxLens),
512
+ description: (param.description ?? "").slice(0, ConstantString.ParameterDescriptionMaxLens),
513
+ };
514
+ const schema = param.schema;
515
+ if (options.allowMultipleParameters && schema) {
516
+ Utils.updateParameterWithInputType(schema, parameter);
517
+ }
518
+ if (param.in !== "header" && param.in !== "cookie") {
519
+ if (param.required && schema?.default === undefined) {
520
+ parameter.isRequired = true;
521
+ requiredParams.push(parameter);
522
+ }
523
+ else {
524
+ optionalParams.push(parameter);
525
+ }
526
+ }
527
+ });
528
+ }
529
+ if (operationItem.requestBody) {
530
+ const requestBody = operationItem.requestBody;
531
+ const requestJson = requestBody.content["application/json"];
532
+ if (Object.keys(requestJson).length !== 0) {
533
+ const schema = requestJson.schema;
534
+ const [requiredP, optionalP] = Utils.generateParametersFromSchema(schema, "requestBody", !!options.allowMultipleParameters, requestBody.required);
535
+ requiredParams.push(...requiredP);
536
+ optionalParams.push(...optionalP);
537
+ }
538
+ }
539
+ const operationId = operationItem.operationId;
540
+ const parameters = [...requiredParams, ...optionalParams];
541
+ const command = {
542
+ context: ["compose"],
543
+ type: "query",
544
+ title: (operationItem.summary ?? "").slice(0, ConstantString.CommandTitleMaxLens),
545
+ id: operationId,
546
+ parameters: parameters,
547
+ description: (operationItem.description ?? "").slice(0, ConstantString.CommandDescriptionMaxLens),
548
+ };
549
+ return command;
550
+ }
551
+ static format(str, ...args) {
552
+ let index = 0;
553
+ return str.replace(/%s/g, () => {
554
+ const arg = args[index++];
555
+ return arg !== undefined ? arg : "";
556
+ });
557
+ }
558
+ static getSafeRegistrationIdEnvName(authName) {
559
+ if (!authName) {
560
+ return "";
561
+ }
562
+ let safeRegistrationIdEnvName = authName.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
563
+ if (!safeRegistrationIdEnvName.match(/^[A-Z]/)) {
564
+ safeRegistrationIdEnvName = "PREFIX_" + safeRegistrationIdEnvName;
565
+ }
566
+ return safeRegistrationIdEnvName;
567
+ }
568
+ static getServerObject(spec, method, path) {
569
+ const pathObj = spec.paths[path];
570
+ const operationObject = pathObj[method];
571
+ const rootServer = spec.servers && spec.servers[0];
572
+ const methodServer = spec.paths[path].servers && spec.paths[path].servers[0];
573
+ const operationServer = operationObject.servers && operationObject.servers[0];
574
+ const serverUrl = operationServer || methodServer || rootServer;
575
+ return serverUrl;
576
+ }
577
+ static getAuthSchemaObject(authType, authParameters) {
578
+ switch (authType) {
579
+ case "oauth":
580
+ case "microsoft-entra":
581
+ return {
582
+ type: "oauth2",
583
+ flows: {
584
+ authorizationCode: {
585
+ authorizationUrl: authParameters.authorizationUrl,
586
+ tokenUrl: authParameters.tokenUrl,
587
+ refreshUrl: authParameters.refreshUrl,
588
+ scopes: authParameters.scopes,
589
+ },
590
+ },
591
+ };
592
+ case "api-key":
593
+ return {
594
+ type: "apiKey",
595
+ in: authParameters.in,
596
+ name: authParameters.name,
597
+ };
598
+ case "bearer-token":
599
+ default:
600
+ return {
601
+ type: "http",
602
+ scheme: "bearer",
603
+ };
604
+ }
605
+ }
641
606
  }
642
607
 
643
- // Copyright (c) Microsoft Corporation.
644
- class Validator {
645
- constructor() {
646
- this.hasCircularReference = false;
647
- }
648
- checkCircularReference() {
649
- try {
650
- JSON.stringify(this.spec);
651
- }
652
- catch (e) {
653
- if (e.message.includes("Converting circular structure to JSON")) {
654
- this.hasCircularReference = true;
655
- }
656
- }
657
- }
658
- listAPIs() {
659
- var _a;
660
- if (this.apiMap) {
661
- return this.apiMap;
662
- }
663
- const paths = this.spec.paths;
664
- const result = {};
665
- for (const path in paths) {
666
- const methods = paths[path];
667
- for (const method in methods) {
668
- const operationObject = methods[method];
669
- if (((_a = this.options.allowMethods) === null || _a === void 0 ? void 0 : _a.includes(method)) && operationObject) {
670
- const validateResult = this.validateAPI(method, path);
671
- result[`${method.toUpperCase()} ${path}`] = {
672
- operation: operationObject,
673
- isValid: validateResult.isValid,
674
- reason: validateResult.reason,
675
- };
676
- }
677
- }
678
- }
679
- this.apiMap = result;
680
- return result;
681
- }
682
- validateSpecVersion() {
683
- const result = { errors: [], warnings: [] };
684
- if (this.spec.openapi >= "3.1.0") {
685
- result.errors.push({
686
- type: ErrorType.SpecVersionNotSupported,
687
- content: Utils.format(ConstantString.SpecVersionNotSupported, this.spec.openapi),
688
- data: this.spec.openapi,
689
- });
690
- }
691
- return result;
692
- }
693
- validateSpecServer() {
694
- const result = { errors: [], warnings: [] };
695
- const serverErrors = Utils.validateServer(this.spec, this.options);
696
- result.errors.push(...serverErrors);
697
- return result;
698
- }
699
- validateSpecNoSupportAPI() {
700
- const result = { errors: [], warnings: [] };
701
- const apiMap = this.listAPIs();
702
- const validAPIs = Object.entries(apiMap).filter(([, value]) => value.isValid);
703
- if (validAPIs.length === 0) {
704
- const data = [];
705
- for (const key in apiMap) {
706
- const { reason } = apiMap[key];
707
- const apiInvalidReason = { api: key, reason: reason };
708
- data.push(apiInvalidReason);
709
- }
710
- result.errors.push({
711
- type: ErrorType.NoSupportedApi,
712
- content: ConstantString.NoSupportedApi,
713
- data,
714
- });
715
- }
716
- return result;
717
- }
718
- validateSpecOperationId() {
719
- const result = { errors: [], warnings: [] };
720
- const apiMap = this.listAPIs();
721
- // OperationId missing
722
- const apisMissingOperationId = [];
723
- for (const key in apiMap) {
724
- const { operation } = apiMap[key];
725
- if (!operation.operationId) {
726
- apisMissingOperationId.push(key);
727
- }
728
- }
729
- if (apisMissingOperationId.length > 0) {
730
- result.warnings.push({
731
- type: WarningType.OperationIdMissing,
732
- content: Utils.format(ConstantString.MissingOperationId, apisMissingOperationId.join(", ")),
733
- data: apisMissingOperationId,
734
- });
735
- }
736
- return result;
737
- }
738
- validateMethodAndPath(method, path) {
739
- const result = { isValid: true, reason: [] };
740
- if (this.options.allowMethods && !this.options.allowMethods.includes(method)) {
741
- result.isValid = false;
742
- result.reason.push(ErrorType.MethodNotAllowed);
743
- return result;
744
- }
745
- const pathObj = this.spec.paths[path];
746
- if (!pathObj || !pathObj[method]) {
747
- result.isValid = false;
748
- result.reason.push(ErrorType.UrlPathNotExist);
749
- return result;
750
- }
751
- return result;
752
- }
753
- validateCircularReference(method, path) {
754
- const result = { isValid: true, reason: [] };
755
- if (this.hasCircularReference) {
756
- const operationObject = this.spec.paths[path][method];
757
- try {
758
- JSON.stringify(operationObject);
759
- }
760
- catch (e) {
761
- if (e.message.includes("Converting circular structure to JSON")) {
762
- result.isValid = false;
763
- result.reason.push(ErrorType.CircularReferenceNotSupported);
764
- }
765
- }
766
- }
767
- return result;
768
- }
769
- validateServer(method, path) {
770
- const result = { isValid: true, reason: [] };
771
- const serverObj = Utils.getServerObject(this.spec, method, path);
772
- if (!serverObj) {
773
- // should contain server URL
774
- result.reason.push(ErrorType.NoServerInformation);
775
- }
776
- else {
777
- const allowHttp = this.projectType === ProjectType.Copilot;
778
- const serverValidateResult = Utils.checkServerUrl([serverObj], allowHttp);
779
- result.reason.push(...serverValidateResult.map((item) => item.type));
780
- }
781
- return result;
782
- }
783
- validateAuth(method, path) {
784
- const pathObj = this.spec.paths[path];
785
- const operationObject = pathObj[method];
786
- const securities = operationObject.security;
787
- const authSchemeArray = Utils.getAuthArray(securities, this.spec);
788
- if (authSchemeArray.length === 0) {
789
- return { isValid: true, reason: [] };
790
- }
791
- if (this.options.allowAPIKeyAuth ||
792
- this.options.allowOauth2 ||
793
- this.options.allowBearerTokenAuth) {
794
- // Currently we don't support multiple auth in one operation
795
- if (authSchemeArray.length > 0 && authSchemeArray.every((auths) => auths.length > 1)) {
796
- return {
797
- isValid: false,
798
- reason: [ErrorType.MultipleAuthNotSupported],
799
- };
800
- }
801
- if (this.projectType === ProjectType.Copilot) {
802
- return { isValid: true, reason: [] };
803
- }
804
- for (const auths of authSchemeArray) {
805
- if (auths.length === 1) {
806
- if ((this.options.allowAPIKeyAuth && Utils.isAPIKeyAuth(auths[0].authScheme)) ||
807
- (this.options.allowOauth2 && Utils.isOAuthWithAuthCodeFlow(auths[0].authScheme)) ||
808
- (this.options.allowBearerTokenAuth && Utils.isBearerTokenAuth(auths[0].authScheme))) {
809
- return { isValid: true, reason: [] };
810
- }
811
- }
812
- }
813
- }
814
- return { isValid: false, reason: [ErrorType.AuthTypeIsNotSupported] };
815
- }
608
+ // Copyright (c) Microsoft Corporation.
609
+ class Validator {
610
+ projectType;
611
+ spec;
612
+ options;
613
+ apiMap;
614
+ hasCircularReference = false;
615
+ checkCircularReference() {
616
+ try {
617
+ JSON.stringify(this.spec);
618
+ }
619
+ catch (e) {
620
+ if (e.message.includes("Converting circular structure to JSON")) {
621
+ this.hasCircularReference = true;
622
+ }
623
+ }
624
+ }
625
+ listAPIs() {
626
+ if (this.apiMap) {
627
+ return this.apiMap;
628
+ }
629
+ const paths = this.spec.paths;
630
+ const result = {};
631
+ for (const path in paths) {
632
+ const methods = paths[path];
633
+ for (const method in methods) {
634
+ const operationObject = methods[method];
635
+ if (this.options.allowMethods?.includes(method) && operationObject) {
636
+ const validateResult = this.validateAPI(method, path);
637
+ result[`${method.toUpperCase()} ${path}`] = {
638
+ operation: operationObject,
639
+ isValid: validateResult.isValid,
640
+ reason: validateResult.reason,
641
+ };
642
+ }
643
+ }
644
+ }
645
+ this.apiMap = result;
646
+ return result;
647
+ }
648
+ validateSpecVersion() {
649
+ const result = { errors: [], warnings: [] };
650
+ if (this.spec.openapi >= "3.1.0") {
651
+ result.errors.push({
652
+ type: ErrorType.SpecVersionNotSupported,
653
+ content: Utils.format(ConstantString.SpecVersionNotSupported, this.spec.openapi),
654
+ data: this.spec.openapi,
655
+ });
656
+ }
657
+ return result;
658
+ }
659
+ validateSpecServer() {
660
+ const result = { errors: [], warnings: [] };
661
+ const serverErrors = Utils.validateServer(this.spec, this.options);
662
+ result.errors.push(...serverErrors);
663
+ return result;
664
+ }
665
+ validateSpecNoSupportAPI() {
666
+ const result = { errors: [], warnings: [] };
667
+ const apiMap = this.listAPIs();
668
+ const validAPIs = Object.entries(apiMap).filter(([, value]) => value.isValid);
669
+ if (validAPIs.length === 0) {
670
+ const data = [];
671
+ for (const key in apiMap) {
672
+ const { reason } = apiMap[key];
673
+ const apiInvalidReason = { api: key, reason: reason };
674
+ data.push(apiInvalidReason);
675
+ }
676
+ result.errors.push({
677
+ type: ErrorType.NoSupportedApi,
678
+ content: ConstantString.NoSupportedApi,
679
+ data,
680
+ });
681
+ }
682
+ return result;
683
+ }
684
+ validateSpecOperationId() {
685
+ const result = { errors: [], warnings: [] };
686
+ const apiMap = this.listAPIs();
687
+ // OperationId missing
688
+ const apisMissingOperationId = [];
689
+ for (const key in apiMap) {
690
+ const { operation } = apiMap[key];
691
+ if (!operation.operationId) {
692
+ apisMissingOperationId.push(key);
693
+ }
694
+ }
695
+ if (apisMissingOperationId.length > 0) {
696
+ result.warnings.push({
697
+ type: WarningType.OperationIdMissing,
698
+ content: Utils.format(ConstantString.MissingOperationId, apisMissingOperationId.join(", ")),
699
+ data: apisMissingOperationId,
700
+ });
701
+ }
702
+ return result;
703
+ }
704
+ validateMethodAndPath(method, path) {
705
+ const result = { isValid: true, reason: [] };
706
+ if (this.options.allowMethods && !this.options.allowMethods.includes(method)) {
707
+ result.isValid = false;
708
+ result.reason.push(ErrorType.MethodNotAllowed);
709
+ return result;
710
+ }
711
+ const pathObj = this.spec.paths[path];
712
+ if (!pathObj || !pathObj[method]) {
713
+ result.isValid = false;
714
+ result.reason.push(ErrorType.UrlPathNotExist);
715
+ return result;
716
+ }
717
+ return result;
718
+ }
719
+ validateCircularReference(method, path) {
720
+ const result = { isValid: true, reason: [] };
721
+ if (this.hasCircularReference) {
722
+ const operationObject = this.spec.paths[path][method];
723
+ try {
724
+ JSON.stringify(operationObject);
725
+ }
726
+ catch (e) {
727
+ if (e.message.includes("Converting circular structure to JSON")) {
728
+ result.isValid = false;
729
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
730
+ }
731
+ }
732
+ }
733
+ return result;
734
+ }
735
+ validateServer(method, path) {
736
+ const result = { isValid: true, reason: [] };
737
+ const serverObj = Utils.getServerObject(this.spec, method, path);
738
+ if (!serverObj) {
739
+ // should contain server URL
740
+ result.reason.push(ErrorType.NoServerInformation);
741
+ }
742
+ else {
743
+ const allowHttp = this.projectType === ProjectType.Copilot;
744
+ const serverValidateResult = Utils.checkServerUrl([serverObj], allowHttp);
745
+ result.reason.push(...serverValidateResult.map((item) => item.type));
746
+ }
747
+ return result;
748
+ }
749
+ validateAuth(method, path) {
750
+ const pathObj = this.spec.paths[path];
751
+ const operationObject = pathObj[method];
752
+ const securities = operationObject.security;
753
+ const authSchemeArray = Utils.getAuthArray(securities, this.spec);
754
+ if (authSchemeArray.length === 0) {
755
+ return { isValid: true, reason: [] };
756
+ }
757
+ if (this.options.allowAPIKeyAuth ||
758
+ this.options.allowOauth2 ||
759
+ this.options.allowBearerTokenAuth) {
760
+ // Currently we don't support multiple auth in one operation
761
+ if (authSchemeArray.length > 0 && authSchemeArray.every((auths) => auths.length > 1)) {
762
+ return {
763
+ isValid: false,
764
+ reason: [ErrorType.MultipleAuthNotSupported],
765
+ };
766
+ }
767
+ if (this.projectType === ProjectType.Copilot) {
768
+ return { isValid: true, reason: [] };
769
+ }
770
+ for (const auths of authSchemeArray) {
771
+ if (auths.length === 1) {
772
+ if ((this.options.allowAPIKeyAuth && Utils.isAPIKeyAuth(auths[0].authScheme)) ||
773
+ (this.options.allowOauth2 && Utils.isOAuthWithAuthCodeFlow(auths[0].authScheme)) ||
774
+ (this.options.allowBearerTokenAuth && Utils.isBearerTokenAuth(auths[0].authScheme))) {
775
+ return { isValid: true, reason: [] };
776
+ }
777
+ }
778
+ }
779
+ }
780
+ return { isValid: false, reason: [ErrorType.AuthTypeIsNotSupported] };
781
+ }
816
782
  }
817
783
 
818
- // Copyright (c) Microsoft Corporation.
819
- class CopilotValidator extends Validator {
820
- constructor(spec, options) {
821
- super();
822
- this.projectType = ProjectType.Copilot;
823
- this.options = options;
824
- this.spec = spec;
825
- }
826
- validateSpec() {
827
- const result = { errors: [], warnings: [] };
828
- // validate spec version
829
- let validationResult = this.validateSpecVersion();
830
- result.errors.push(...validationResult.errors);
831
- // validate spec server
832
- validationResult = this.validateSpecServer();
833
- result.errors.push(...validationResult.errors);
834
- // validate no supported API
835
- validationResult = this.validateSpecNoSupportAPI();
836
- result.errors.push(...validationResult.errors);
837
- // validate operationId missing
838
- validationResult = this.validateSpecOperationId();
839
- result.warnings.push(...validationResult.warnings);
840
- return result;
841
- }
842
- validateAPI(method, path) {
843
- const result = { isValid: true, reason: [] };
844
- method = method.toLocaleLowerCase();
845
- // validate method and path
846
- const methodAndPathResult = this.validateMethodAndPath(method, path);
847
- if (!methodAndPathResult.isValid) {
848
- return methodAndPathResult;
849
- }
850
- const operationObject = this.spec.paths[path][method];
851
- // validate auth
852
- const authCheckResult = this.validateAuth(method, path);
853
- result.reason.push(...authCheckResult.reason);
854
- // validate operationId
855
- if (!this.options.allowMissingId && !operationObject.operationId) {
856
- result.reason.push(ErrorType.MissingOperationId);
857
- }
858
- // validate server
859
- const validateServerResult = this.validateServer(method, path);
860
- result.reason.push(...validateServerResult.reason);
861
- if (result.reason.length > 0) {
862
- result.isValid = false;
863
- }
864
- return result;
865
- }
784
+ // Copyright (c) Microsoft Corporation.
785
+ class CopilotValidator extends Validator {
786
+ constructor(spec, options) {
787
+ super();
788
+ this.projectType = ProjectType.Copilot;
789
+ this.options = options;
790
+ this.spec = spec;
791
+ }
792
+ validateSpec() {
793
+ const result = { errors: [], warnings: [] };
794
+ // validate spec version
795
+ let validationResult = this.validateSpecVersion();
796
+ result.errors.push(...validationResult.errors);
797
+ // validate spec server
798
+ validationResult = this.validateSpecServer();
799
+ result.errors.push(...validationResult.errors);
800
+ // validate no supported API
801
+ validationResult = this.validateSpecNoSupportAPI();
802
+ result.errors.push(...validationResult.errors);
803
+ // validate operationId missing
804
+ validationResult = this.validateSpecOperationId();
805
+ result.warnings.push(...validationResult.warnings);
806
+ return result;
807
+ }
808
+ validateAPI(method, path) {
809
+ const result = { isValid: true, reason: [] };
810
+ method = method.toLocaleLowerCase();
811
+ // validate method and path
812
+ const methodAndPathResult = this.validateMethodAndPath(method, path);
813
+ if (!methodAndPathResult.isValid) {
814
+ return methodAndPathResult;
815
+ }
816
+ const operationObject = this.spec.paths[path][method];
817
+ // validate auth
818
+ const authCheckResult = this.validateAuth(method, path);
819
+ result.reason.push(...authCheckResult.reason);
820
+ // validate operationId
821
+ if (!this.options.allowMissingId && !operationObject.operationId) {
822
+ result.reason.push(ErrorType.MissingOperationId);
823
+ }
824
+ // validate server
825
+ const validateServerResult = this.validateServer(method, path);
826
+ result.reason.push(...validateServerResult.reason);
827
+ if (result.reason.length > 0) {
828
+ result.isValid = false;
829
+ }
830
+ return result;
831
+ }
866
832
  }
867
833
 
868
- // Copyright (c) Microsoft Corporation.
869
- class SMEValidator extends Validator {
870
- constructor(spec, options) {
871
- super();
872
- this.projectType = ProjectType.SME;
873
- this.options = options;
874
- this.spec = spec;
875
- this.checkCircularReference();
876
- }
877
- validateSpec() {
878
- const result = { errors: [], warnings: [] };
879
- // validate spec version
880
- let validationResult = this.validateSpecVersion();
881
- result.errors.push(...validationResult.errors);
882
- // validate spec server
883
- validationResult = this.validateSpecServer();
884
- result.errors.push(...validationResult.errors);
885
- // validate no supported API
886
- validationResult = this.validateSpecNoSupportAPI();
887
- result.errors.push(...validationResult.errors);
888
- // validate operationId missing
889
- if (this.options.allowMissingId) {
890
- validationResult = this.validateSpecOperationId();
891
- result.warnings.push(...validationResult.warnings);
892
- }
893
- return result;
894
- }
895
- validateAPI(method, path) {
896
- const result = { isValid: true, reason: [] };
897
- method = method.toLocaleLowerCase();
898
- // validate method and path
899
- const methodAndPathResult = this.validateMethodAndPath(method, path);
900
- if (!methodAndPathResult.isValid) {
901
- return methodAndPathResult;
902
- }
903
- const circularReferenceResult = this.validateCircularReference(method, path);
904
- if (!circularReferenceResult.isValid) {
905
- return circularReferenceResult;
906
- }
907
- const operationObject = this.spec.paths[path][method];
908
- // validate auth
909
- const authCheckResult = this.validateAuth(method, path);
910
- result.reason.push(...authCheckResult.reason);
911
- // validate operationId
912
- if (!this.options.allowMissingId && !operationObject.operationId) {
913
- result.reason.push(ErrorType.MissingOperationId);
914
- }
915
- // validate server
916
- const validateServerResult = this.validateServer(method, path);
917
- result.reason.push(...validateServerResult.reason);
918
- // validate response
919
- const validateResponseResult = this.validateResponse(method, path);
920
- result.reason.push(...validateResponseResult.reason);
921
- let postBodyResult = {
922
- requiredNum: 0,
923
- optionalNum: 0,
924
- isValid: true,
925
- reason: [],
926
- };
927
- // validate requestBody
928
- const requestBody = operationObject.requestBody;
929
- const requestJsonBody = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content["application/json"];
930
- if (Utils.containMultipleMediaTypes(requestBody)) {
931
- result.reason.push(ErrorType.PostBodyContainMultipleMediaTypes);
932
- }
933
- if (requestJsonBody) {
934
- const requestBodySchema = requestJsonBody.schema;
935
- postBodyResult = this.checkPostBodySchema(requestBodySchema, requestBody.required);
936
- result.reason.push(...postBodyResult.reason);
937
- }
938
- // validate parameters
939
- const paramObject = operationObject.parameters;
940
- const paramResult = this.checkParamSchema(paramObject);
941
- result.reason.push(...paramResult.reason);
942
- // validate total parameters count
943
- if (paramResult.isValid && postBodyResult.isValid) {
944
- const paramCountResult = this.validateParamCount(postBodyResult, paramResult);
945
- result.reason.push(...paramCountResult.reason);
946
- }
947
- if (result.reason.length > 0) {
948
- result.isValid = false;
949
- }
950
- return result;
951
- }
952
- validateResponse(method, path) {
953
- const result = { isValid: true, reason: [] };
954
- const operationObject = this.spec.paths[path][method];
955
- const { json, multipleMediaType } = Utils.getResponseJson(operationObject);
956
- // only support response body only contains “application/json” content type
957
- if (multipleMediaType) {
958
- result.reason.push(ErrorType.ResponseContainMultipleMediaTypes);
959
- }
960
- else if (Object.keys(json).length === 0) {
961
- // response body should not be empty
962
- result.reason.push(ErrorType.ResponseJsonIsEmpty);
963
- }
964
- return result;
965
- }
966
- checkPostBodySchema(schema, isRequired = false) {
967
- var _a;
968
- const paramResult = {
969
- requiredNum: 0,
970
- optionalNum: 0,
971
- isValid: true,
972
- reason: [],
973
- };
974
- if (Object.keys(schema).length === 0) {
975
- return paramResult;
976
- }
977
- const isRequiredWithoutDefault = isRequired && schema.default === undefined;
978
- const isCopilot = this.projectType === ProjectType.Copilot;
979
- if (schema.type === "string" ||
980
- schema.type === "integer" ||
981
- schema.type === "boolean" ||
982
- schema.type === "number") {
983
- if (isRequiredWithoutDefault) {
984
- paramResult.requiredNum = paramResult.requiredNum + 1;
985
- }
986
- else {
987
- paramResult.optionalNum = paramResult.optionalNum + 1;
988
- }
989
- }
990
- else if (Utils.isObjectSchema(schema)) {
991
- const { properties } = schema;
992
- for (const property in properties) {
993
- let isRequired = false;
994
- if (schema.required && ((_a = schema.required) === null || _a === void 0 ? void 0 : _a.indexOf(property)) >= 0) {
995
- isRequired = true;
996
- }
997
- const result = this.checkPostBodySchema(properties[property], isRequired);
998
- paramResult.requiredNum += result.requiredNum;
999
- paramResult.optionalNum += result.optionalNum;
1000
- paramResult.isValid = paramResult.isValid && result.isValid;
1001
- paramResult.reason.push(...result.reason);
1002
- }
1003
- }
1004
- else {
1005
- if (isRequiredWithoutDefault && !isCopilot) {
1006
- paramResult.isValid = false;
1007
- paramResult.reason.push(ErrorType.PostBodyContainsRequiredUnsupportedSchema);
1008
- }
1009
- }
1010
- return paramResult;
1011
- }
1012
- checkParamSchema(paramObject) {
1013
- const paramResult = {
1014
- requiredNum: 0,
1015
- optionalNum: 0,
1016
- isValid: true,
1017
- reason: [],
1018
- };
1019
- if (!paramObject) {
1020
- return paramResult;
1021
- }
1022
- for (let i = 0; i < paramObject.length; i++) {
1023
- const param = paramObject[i];
1024
- const schema = param.schema;
1025
- const isRequiredWithoutDefault = param.required && schema.default === undefined;
1026
- if (param.in === "header" || param.in === "cookie") {
1027
- if (isRequiredWithoutDefault) {
1028
- paramResult.isValid = false;
1029
- paramResult.reason.push(ErrorType.ParamsContainRequiredUnsupportedSchema);
1030
- }
1031
- continue;
1032
- }
1033
- if (schema.type !== "boolean" &&
1034
- schema.type !== "string" &&
1035
- schema.type !== "number" &&
1036
- schema.type !== "integer") {
1037
- if (isRequiredWithoutDefault) {
1038
- paramResult.isValid = false;
1039
- paramResult.reason.push(ErrorType.ParamsContainRequiredUnsupportedSchema);
1040
- }
1041
- continue;
1042
- }
1043
- if (param.in === "query" || param.in === "path") {
1044
- if (isRequiredWithoutDefault) {
1045
- paramResult.requiredNum = paramResult.requiredNum + 1;
1046
- }
1047
- else {
1048
- paramResult.optionalNum = paramResult.optionalNum + 1;
1049
- }
1050
- }
1051
- }
1052
- return paramResult;
1053
- }
1054
- validateParamCount(postBodyResult, paramResult) {
1055
- const result = { isValid: true, reason: [] };
1056
- const totalRequiredParams = postBodyResult.requiredNum + paramResult.requiredNum;
1057
- const totalParams = totalRequiredParams + postBodyResult.optionalNum + paramResult.optionalNum;
1058
- if (totalRequiredParams > 1) {
1059
- if (!this.options.allowMultipleParameters ||
1060
- totalRequiredParams > SMEValidator.SMERequiredParamsMaxNum) {
1061
- result.reason.push(ErrorType.ExceededRequiredParamsLimit);
1062
- }
1063
- }
1064
- else if (totalParams === 0) {
1065
- result.reason.push(ErrorType.NoParameter);
1066
- }
1067
- return result;
1068
- }
1069
- }
1070
- SMEValidator.SMERequiredParamsMaxNum = 5;
834
+ // Copyright (c) Microsoft Corporation.
835
+ class SMEValidator extends Validator {
836
+ static SMERequiredParamsMaxNum = 5;
837
+ constructor(spec, options) {
838
+ super();
839
+ this.projectType = ProjectType.SME;
840
+ this.options = options;
841
+ this.spec = spec;
842
+ this.checkCircularReference();
843
+ }
844
+ validateSpec() {
845
+ const result = { errors: [], warnings: [] };
846
+ // validate spec version
847
+ let validationResult = this.validateSpecVersion();
848
+ result.errors.push(...validationResult.errors);
849
+ // validate spec server
850
+ validationResult = this.validateSpecServer();
851
+ result.errors.push(...validationResult.errors);
852
+ // validate no supported API
853
+ validationResult = this.validateSpecNoSupportAPI();
854
+ result.errors.push(...validationResult.errors);
855
+ // validate operationId missing
856
+ if (this.options.allowMissingId) {
857
+ validationResult = this.validateSpecOperationId();
858
+ result.warnings.push(...validationResult.warnings);
859
+ }
860
+ return result;
861
+ }
862
+ validateAPI(method, path) {
863
+ const result = { isValid: true, reason: [] };
864
+ method = method.toLocaleLowerCase();
865
+ // validate method and path
866
+ const methodAndPathResult = this.validateMethodAndPath(method, path);
867
+ if (!methodAndPathResult.isValid) {
868
+ return methodAndPathResult;
869
+ }
870
+ const circularReferenceResult = this.validateCircularReference(method, path);
871
+ if (!circularReferenceResult.isValid) {
872
+ return circularReferenceResult;
873
+ }
874
+ const operationObject = this.spec.paths[path][method];
875
+ // validate auth
876
+ const authCheckResult = this.validateAuth(method, path);
877
+ result.reason.push(...authCheckResult.reason);
878
+ // validate operationId
879
+ if (!this.options.allowMissingId && !operationObject.operationId) {
880
+ result.reason.push(ErrorType.MissingOperationId);
881
+ }
882
+ // validate server
883
+ const validateServerResult = this.validateServer(method, path);
884
+ result.reason.push(...validateServerResult.reason);
885
+ // validate response
886
+ const validateResponseResult = this.validateResponse(method, path);
887
+ result.reason.push(...validateResponseResult.reason);
888
+ let postBodyResult = {
889
+ requiredNum: 0,
890
+ optionalNum: 0,
891
+ isValid: true,
892
+ reason: [],
893
+ };
894
+ // validate requestBody
895
+ const requestBody = operationObject.requestBody;
896
+ const requestJsonBody = requestBody?.content["application/json"];
897
+ if (Utils.containMultipleMediaTypes(requestBody)) {
898
+ result.reason.push(ErrorType.PostBodyContainMultipleMediaTypes);
899
+ }
900
+ if (requestJsonBody) {
901
+ const requestBodySchema = requestJsonBody.schema;
902
+ postBodyResult = this.checkPostBodySchema(requestBodySchema, requestBody.required);
903
+ result.reason.push(...postBodyResult.reason);
904
+ }
905
+ // validate parameters
906
+ const paramObject = operationObject.parameters;
907
+ const paramResult = this.checkParamSchema(paramObject);
908
+ result.reason.push(...paramResult.reason);
909
+ // validate total parameters count
910
+ if (paramResult.isValid && postBodyResult.isValid) {
911
+ const paramCountResult = this.validateParamCount(postBodyResult, paramResult);
912
+ result.reason.push(...paramCountResult.reason);
913
+ }
914
+ if (result.reason.length > 0) {
915
+ result.isValid = false;
916
+ }
917
+ return result;
918
+ }
919
+ validateResponse(method, path) {
920
+ const result = { isValid: true, reason: [] };
921
+ const operationObject = this.spec.paths[path][method];
922
+ const { json, multipleMediaType } = Utils.getResponseJson(operationObject);
923
+ // only support response body only contains “application/json” content type
924
+ if (multipleMediaType) {
925
+ result.reason.push(ErrorType.ResponseContainMultipleMediaTypes);
926
+ }
927
+ else if (Object.keys(json).length === 0) {
928
+ // response body should not be empty
929
+ result.reason.push(ErrorType.ResponseJsonIsEmpty);
930
+ }
931
+ return result;
932
+ }
933
+ checkPostBodySchema(schema, isRequired = false) {
934
+ const paramResult = {
935
+ requiredNum: 0,
936
+ optionalNum: 0,
937
+ isValid: true,
938
+ reason: [],
939
+ };
940
+ if (Object.keys(schema).length === 0) {
941
+ return paramResult;
942
+ }
943
+ const isRequiredWithoutDefault = isRequired && schema.default === undefined;
944
+ const isCopilot = this.projectType === ProjectType.Copilot;
945
+ if (schema.type === "string" ||
946
+ schema.type === "integer" ||
947
+ schema.type === "boolean" ||
948
+ schema.type === "number") {
949
+ if (isRequiredWithoutDefault) {
950
+ paramResult.requiredNum = paramResult.requiredNum + 1;
951
+ }
952
+ else {
953
+ paramResult.optionalNum = paramResult.optionalNum + 1;
954
+ }
955
+ }
956
+ else if (Utils.isObjectSchema(schema)) {
957
+ const { properties } = schema;
958
+ for (const property in properties) {
959
+ let isRequired = false;
960
+ if (schema.required && schema.required?.indexOf(property) >= 0) {
961
+ isRequired = true;
962
+ }
963
+ const result = this.checkPostBodySchema(properties[property], isRequired);
964
+ paramResult.requiredNum += result.requiredNum;
965
+ paramResult.optionalNum += result.optionalNum;
966
+ paramResult.isValid = paramResult.isValid && result.isValid;
967
+ paramResult.reason.push(...result.reason);
968
+ }
969
+ }
970
+ else {
971
+ if (isRequiredWithoutDefault && !isCopilot) {
972
+ paramResult.isValid = false;
973
+ paramResult.reason.push(ErrorType.PostBodyContainsRequiredUnsupportedSchema);
974
+ }
975
+ }
976
+ return paramResult;
977
+ }
978
+ checkParamSchema(paramObject) {
979
+ const paramResult = {
980
+ requiredNum: 0,
981
+ optionalNum: 0,
982
+ isValid: true,
983
+ reason: [],
984
+ };
985
+ if (!paramObject) {
986
+ return paramResult;
987
+ }
988
+ for (let i = 0; i < paramObject.length; i++) {
989
+ const param = paramObject[i];
990
+ const schema = param.schema;
991
+ const isRequiredWithoutDefault = param.required && schema.default === undefined;
992
+ if (param.in === "header" || param.in === "cookie") {
993
+ if (isRequiredWithoutDefault) {
994
+ paramResult.isValid = false;
995
+ paramResult.reason.push(ErrorType.ParamsContainRequiredUnsupportedSchema);
996
+ }
997
+ continue;
998
+ }
999
+ if (schema.type !== "boolean" &&
1000
+ schema.type !== "string" &&
1001
+ schema.type !== "number" &&
1002
+ schema.type !== "integer") {
1003
+ if (isRequiredWithoutDefault) {
1004
+ paramResult.isValid = false;
1005
+ paramResult.reason.push(ErrorType.ParamsContainRequiredUnsupportedSchema);
1006
+ }
1007
+ continue;
1008
+ }
1009
+ if (param.in === "query" || param.in === "path") {
1010
+ if (isRequiredWithoutDefault) {
1011
+ paramResult.requiredNum = paramResult.requiredNum + 1;
1012
+ }
1013
+ else {
1014
+ paramResult.optionalNum = paramResult.optionalNum + 1;
1015
+ }
1016
+ }
1017
+ }
1018
+ return paramResult;
1019
+ }
1020
+ validateParamCount(postBodyResult, paramResult) {
1021
+ const result = { isValid: true, reason: [] };
1022
+ const totalRequiredParams = postBodyResult.requiredNum + paramResult.requiredNum;
1023
+ const totalParams = totalRequiredParams + postBodyResult.optionalNum + paramResult.optionalNum;
1024
+ if (totalRequiredParams > 1) {
1025
+ if (!this.options.allowMultipleParameters ||
1026
+ totalRequiredParams > SMEValidator.SMERequiredParamsMaxNum) {
1027
+ result.reason.push(ErrorType.ExceededRequiredParamsLimit);
1028
+ }
1029
+ }
1030
+ else if (totalParams === 0) {
1031
+ result.reason.push(ErrorType.NoParameter);
1032
+ }
1033
+ return result;
1034
+ }
1035
+ }
1071
1036
 
1072
- // Copyright (c) Microsoft Corporation.
1073
- class TeamsAIValidator extends Validator {
1074
- constructor(spec, options) {
1075
- super();
1076
- this.projectType = ProjectType.TeamsAi;
1077
- this.options = options;
1078
- this.spec = spec;
1079
- this.checkCircularReference();
1080
- }
1081
- validateSpec() {
1082
- const result = { errors: [], warnings: [] };
1083
- // validate spec server
1084
- let validationResult = this.validateSpecServer();
1085
- result.errors.push(...validationResult.errors);
1086
- // validate no supported API
1087
- validationResult = this.validateSpecNoSupportAPI();
1088
- result.errors.push(...validationResult.errors);
1089
- return result;
1090
- }
1091
- validateAPI(method, path) {
1092
- const result = { isValid: true, reason: [] };
1093
- method = method.toLocaleLowerCase();
1094
- // validate method and path
1095
- const methodAndPathResult = this.validateMethodAndPath(method, path);
1096
- if (!methodAndPathResult.isValid) {
1097
- return methodAndPathResult;
1098
- }
1099
- const circularReferenceResult = this.validateCircularReference(method, path);
1100
- if (!circularReferenceResult.isValid) {
1101
- return circularReferenceResult;
1102
- }
1103
- const operationObject = this.spec.paths[path][method];
1104
- // validate operationId
1105
- if (!this.options.allowMissingId && !operationObject.operationId) {
1106
- result.reason.push(ErrorType.MissingOperationId);
1107
- }
1108
- // validate server
1109
- const validateServerResult = this.validateServer(method, path);
1110
- result.reason.push(...validateServerResult.reason);
1111
- if (result.reason.length > 0) {
1112
- result.isValid = false;
1113
- }
1114
- return result;
1115
- }
1037
+ // Copyright (c) Microsoft Corporation.
1038
+ class TeamsAIValidator extends Validator {
1039
+ constructor(spec, options) {
1040
+ super();
1041
+ this.projectType = ProjectType.TeamsAi;
1042
+ this.options = options;
1043
+ this.spec = spec;
1044
+ this.checkCircularReference();
1045
+ }
1046
+ validateSpec() {
1047
+ const result = { errors: [], warnings: [] };
1048
+ // validate spec server
1049
+ let validationResult = this.validateSpecServer();
1050
+ result.errors.push(...validationResult.errors);
1051
+ // validate no supported API
1052
+ validationResult = this.validateSpecNoSupportAPI();
1053
+ result.errors.push(...validationResult.errors);
1054
+ return result;
1055
+ }
1056
+ validateAPI(method, path) {
1057
+ const result = { isValid: true, reason: [] };
1058
+ method = method.toLocaleLowerCase();
1059
+ // validate method and path
1060
+ const methodAndPathResult = this.validateMethodAndPath(method, path);
1061
+ if (!methodAndPathResult.isValid) {
1062
+ return methodAndPathResult;
1063
+ }
1064
+ const circularReferenceResult = this.validateCircularReference(method, path);
1065
+ if (!circularReferenceResult.isValid) {
1066
+ return circularReferenceResult;
1067
+ }
1068
+ const operationObject = this.spec.paths[path][method];
1069
+ // validate operationId
1070
+ if (!this.options.allowMissingId && !operationObject.operationId) {
1071
+ result.reason.push(ErrorType.MissingOperationId);
1072
+ }
1073
+ // validate server
1074
+ const validateServerResult = this.validateServer(method, path);
1075
+ result.reason.push(...validateServerResult.reason);
1076
+ if (result.reason.length > 0) {
1077
+ result.isValid = false;
1078
+ }
1079
+ return result;
1080
+ }
1116
1081
  }
1117
1082
 
1118
- class ValidatorFactory {
1119
- static create(spec, options) {
1120
- var _a;
1121
- const type = (_a = options.projectType) !== null && _a !== void 0 ? _a : ProjectType.SME;
1122
- switch (type) {
1123
- case ProjectType.SME:
1124
- return new SMEValidator(spec, options);
1125
- case ProjectType.Copilot:
1126
- return new CopilotValidator(spec, options);
1127
- case ProjectType.TeamsAi:
1128
- return new TeamsAIValidator(spec, options);
1129
- default:
1130
- throw new Error(`Invalid project type: ${type}`);
1131
- }
1132
- }
1083
+ class ValidatorFactory {
1084
+ static create(spec, options) {
1085
+ const type = options.projectType ?? ProjectType.SME;
1086
+ switch (type) {
1087
+ case ProjectType.SME:
1088
+ return new SMEValidator(spec, options);
1089
+ case ProjectType.Copilot:
1090
+ return new CopilotValidator(spec, options);
1091
+ case ProjectType.TeamsAi:
1092
+ return new TeamsAIValidator(spec, options);
1093
+ default:
1094
+ throw new Error(`Invalid project type: ${type}`);
1095
+ }
1096
+ }
1133
1097
  }
1134
1098
 
1135
- // Copyright (c) Microsoft Corporation.
1136
- /**
1137
- * A class that parses an OpenAPI specification file and provides methods to validate, list, and generate artifacts.
1138
- */
1139
- class SpecParser {
1140
- /**
1141
- * Creates a new instance of the SpecParser class.
1142
- * @param pathOrDoc The path to the OpenAPI specification file or the OpenAPI specification object.
1143
- * @param options The options for parsing the OpenAPI specification file.
1144
- */
1145
- constructor(pathOrDoc, options) {
1146
- this.defaultOptions = {
1147
- allowMissingId: false,
1148
- allowSwagger: false,
1149
- allowAPIKeyAuth: false,
1150
- allowMultipleParameters: false,
1151
- allowBearerTokenAuth: false,
1152
- allowOauth2: false,
1153
- allowMethods: ["get", "post"],
1154
- allowConversationStarters: false,
1155
- allowResponseSemantics: false,
1156
- allowConfirmation: false,
1157
- projectType: ProjectType.SME,
1158
- isGptPlugin: false,
1159
- };
1160
- this.pathOrSpec = pathOrDoc;
1161
- this.parser = new SwaggerParser();
1162
- this.options = Object.assign(Object.assign({}, this.defaultOptions), (options !== null && options !== void 0 ? options : {}));
1163
- }
1164
- /**
1165
- * Validates the OpenAPI specification file and returns a validation result.
1166
- *
1167
- * @returns A validation result object that contains information about any errors or warnings in the specification file.
1168
- */
1169
- validate() {
1170
- return __awaiter(this, void 0, void 0, function* () {
1171
- try {
1172
- try {
1173
- yield this.loadSpec();
1174
- yield this.parser.validate(this.spec, {
1175
- validate: {
1176
- schema: false,
1177
- },
1178
- });
1179
- }
1180
- catch (e) {
1181
- return {
1182
- status: ValidationStatus.Error,
1183
- warnings: [],
1184
- errors: [{ type: ErrorType.SpecNotValid, content: e.toString() }],
1185
- };
1186
- }
1187
- const errors = [];
1188
- const warnings = [];
1189
- if (!this.options.allowSwagger && this.isSwaggerFile) {
1190
- return {
1191
- status: ValidationStatus.Error,
1192
- warnings: [],
1193
- errors: [
1194
- {
1195
- type: ErrorType.SwaggerNotSupported,
1196
- content: ConstantString.SwaggerNotSupported,
1197
- },
1198
- ],
1199
- };
1200
- }
1201
- // Remote reference not supported
1202
- const refPaths = this.parser.$refs.paths();
1203
- // refPaths [0] is the current spec file path
1204
- if (refPaths.length > 1) {
1205
- errors.push({
1206
- type: ErrorType.RemoteRefNotSupported,
1207
- content: Utils.format(ConstantString.RemoteRefNotSupported, refPaths.join(", ")),
1208
- data: refPaths,
1209
- });
1210
- }
1211
- const validator = this.getValidator(this.spec);
1212
- const validationResult = validator.validateSpec();
1213
- warnings.push(...validationResult.warnings);
1214
- errors.push(...validationResult.errors);
1215
- let status = ValidationStatus.Valid;
1216
- if (warnings.length > 0 && errors.length === 0) {
1217
- status = ValidationStatus.Warning;
1218
- }
1219
- else if (errors.length > 0) {
1220
- status = ValidationStatus.Error;
1221
- }
1222
- return {
1223
- status: status,
1224
- warnings: warnings,
1225
- errors: errors,
1226
- };
1227
- }
1228
- catch (err) {
1229
- throw new SpecParserError(err.toString(), ErrorType.ValidateFailed);
1230
- }
1231
- });
1232
- }
1233
- listSupportedAPIInfo() {
1234
- return __awaiter(this, void 0, void 0, function* () {
1235
- try {
1236
- yield this.loadSpec();
1237
- const apiMap = this.getAPIs(this.spec);
1238
- const apiInfos = [];
1239
- for (const key in apiMap) {
1240
- const { operation, isValid } = apiMap[key];
1241
- if (!isValid) {
1242
- continue;
1243
- }
1244
- const [method, path] = key.split(" ");
1245
- const operationId = operation.operationId;
1246
- // In Browser environment, this api is by default not support api without operationId
1247
- if (!operationId) {
1248
- continue;
1249
- }
1250
- const command = Utils.parseApiInfo(operation, this.options);
1251
- const apiInfo = {
1252
- method: method,
1253
- path: path,
1254
- title: command.title,
1255
- id: operationId,
1256
- parameters: command.parameters,
1257
- description: command.description,
1258
- };
1259
- apiInfos.push(apiInfo);
1260
- }
1261
- return apiInfos;
1262
- }
1263
- catch (err) {
1264
- throw new SpecParserError(err.toString(), ErrorType.listSupportedAPIInfoFailed);
1265
- }
1266
- });
1267
- }
1268
- /**
1269
- * Lists all the OpenAPI operations in the specification file.
1270
- * @returns A string array that represents the HTTP method and path of each operation, such as ['GET /pets/{petId}', 'GET /user/{userId}']
1271
- * according to copilot plugin spec, only list get and post method without auth
1272
- */
1273
- // eslint-disable-next-line @typescript-eslint/require-await
1274
- list() {
1275
- return __awaiter(this, void 0, void 0, function* () {
1276
- throw new Error("Method not implemented.");
1277
- });
1278
- }
1279
- /**
1280
- * Generate specs according to the filters.
1281
- * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1282
- */
1283
- // eslint-disable-next-line @typescript-eslint/require-await
1284
- getFilteredSpecs(filter, signal) {
1285
- return __awaiter(this, void 0, void 0, function* () {
1286
- throw new Error("Method not implemented.");
1287
- });
1288
- }
1289
- /**
1290
- * Generates and update artifacts from the OpenAPI specification file. Generate Adaptive Cards, update Teams app manifest, and generate a new OpenAPI specification file.
1291
- * @param manifestPath A file path of the Teams app manifest file to update.
1292
- * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1293
- * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
1294
- * @param pluginFilePath File path of the api plugin file to generate.
1295
- */
1296
- // eslint-disable-next-line @typescript-eslint/require-await
1297
- generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, signal) {
1298
- return __awaiter(this, void 0, void 0, function* () {
1299
- throw new Error("Method not implemented.");
1300
- });
1301
- }
1302
- /**
1303
- * Generates and update artifacts from the OpenAPI specification file. Generate Adaptive Cards, update Teams app manifest, and generate a new OpenAPI specification file.
1304
- * @param manifestPath A file path of the Teams app manifest file to update.
1305
- * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1306
- * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
1307
- * @param adaptiveCardFolder Folder path where the Adaptive Card files will be generated. If not specified or empty, Adaptive Card files will not be generated.
1308
- * @param isMe Boolean that indicates whether the project is an Messaging Extension. For Messaging Extension, composeExtensions will be added in Teams app manifest.
1309
- */
1310
- // eslint-disable-next-line @typescript-eslint/require-await
1311
- generate(manifestPath, filter, outputSpecPath, adaptiveCardFolder, signal) {
1312
- return __awaiter(this, void 0, void 0, function* () {
1313
- throw new Error("Method not implemented.");
1314
- });
1315
- }
1316
- loadSpec() {
1317
- return __awaiter(this, void 0, void 0, function* () {
1318
- if (!this.spec) {
1319
- this.unResolveSpec = (yield this.parser.parse(this.pathOrSpec));
1320
- if (!this.unResolveSpec.openapi && this.unResolveSpec.swagger === "2.0") {
1321
- this.isSwaggerFile = true;
1322
- }
1323
- const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
1324
- this.spec = (yield this.parser.dereference(clonedUnResolveSpec));
1325
- }
1326
- });
1327
- }
1328
- getAPIs(spec) {
1329
- const validator = this.getValidator(spec);
1330
- const apiMap = validator.listAPIs();
1331
- return apiMap;
1332
- }
1333
- getValidator(spec) {
1334
- if (this.validator) {
1335
- return this.validator;
1336
- }
1337
- const validator = ValidatorFactory.create(spec, this.options);
1338
- this.validator = validator;
1339
- return validator;
1340
- }
1099
+ // Copyright (c) Microsoft Corporation.
1100
+ /**
1101
+ * A class that parses an OpenAPI specification file and provides methods to validate, list, and generate artifacts.
1102
+ */
1103
+ class SpecParser {
1104
+ pathOrSpec;
1105
+ parser;
1106
+ options;
1107
+ spec;
1108
+ validator;
1109
+ unResolveSpec;
1110
+ isSwaggerFile;
1111
+ defaultOptions = {
1112
+ allowMissingId: false,
1113
+ allowSwagger: false,
1114
+ allowAPIKeyAuth: false,
1115
+ allowMultipleParameters: false,
1116
+ allowBearerTokenAuth: false,
1117
+ allowOauth2: false,
1118
+ allowMethods: ["get", "post"],
1119
+ allowConversationStarters: false,
1120
+ allowResponseSemantics: false,
1121
+ allowConfirmation: false,
1122
+ projectType: ProjectType.SME,
1123
+ isGptPlugin: false,
1124
+ };
1125
+ /**
1126
+ * Creates a new instance of the SpecParser class.
1127
+ * @param pathOrDoc The path to the OpenAPI specification file or the OpenAPI specification object.
1128
+ * @param options The options for parsing the OpenAPI specification file.
1129
+ */
1130
+ constructor(pathOrDoc, options) {
1131
+ this.pathOrSpec = pathOrDoc;
1132
+ this.parser = new SwaggerParser();
1133
+ this.options = {
1134
+ ...this.defaultOptions,
1135
+ ...(options ?? {}),
1136
+ };
1137
+ }
1138
+ /**
1139
+ * Validates the OpenAPI specification file and returns a validation result.
1140
+ *
1141
+ * @returns A validation result object that contains information about any errors or warnings in the specification file.
1142
+ */
1143
+ async validate() {
1144
+ try {
1145
+ try {
1146
+ await this.loadSpec();
1147
+ await this.parser.validate(this.spec, {
1148
+ validate: {
1149
+ schema: false,
1150
+ },
1151
+ });
1152
+ }
1153
+ catch (e) {
1154
+ return {
1155
+ status: ValidationStatus.Error,
1156
+ warnings: [],
1157
+ errors: [{ type: ErrorType.SpecNotValid, content: e.toString() }],
1158
+ };
1159
+ }
1160
+ const errors = [];
1161
+ const warnings = [];
1162
+ if (!this.options.allowSwagger && this.isSwaggerFile) {
1163
+ return {
1164
+ status: ValidationStatus.Error,
1165
+ warnings: [],
1166
+ errors: [
1167
+ {
1168
+ type: ErrorType.SwaggerNotSupported,
1169
+ content: ConstantString.SwaggerNotSupported,
1170
+ },
1171
+ ],
1172
+ };
1173
+ }
1174
+ // Remote reference not supported
1175
+ const refPaths = this.parser.$refs.paths();
1176
+ // refPaths [0] is the current spec file path
1177
+ if (refPaths.length > 1) {
1178
+ errors.push({
1179
+ type: ErrorType.RemoteRefNotSupported,
1180
+ content: Utils.format(ConstantString.RemoteRefNotSupported, refPaths.join(", ")),
1181
+ data: refPaths,
1182
+ });
1183
+ }
1184
+ const validator = this.getValidator(this.spec);
1185
+ const validationResult = validator.validateSpec();
1186
+ warnings.push(...validationResult.warnings);
1187
+ errors.push(...validationResult.errors);
1188
+ let status = ValidationStatus.Valid;
1189
+ if (warnings.length > 0 && errors.length === 0) {
1190
+ status = ValidationStatus.Warning;
1191
+ }
1192
+ else if (errors.length > 0) {
1193
+ status = ValidationStatus.Error;
1194
+ }
1195
+ return {
1196
+ status: status,
1197
+ warnings: warnings,
1198
+ errors: errors,
1199
+ };
1200
+ }
1201
+ catch (err) {
1202
+ throw new SpecParserError(err.toString(), ErrorType.ValidateFailed);
1203
+ }
1204
+ }
1205
+ async listSupportedAPIInfo() {
1206
+ try {
1207
+ await this.loadSpec();
1208
+ const apiMap = this.getAPIs(this.spec);
1209
+ const apiInfos = [];
1210
+ for (const key in apiMap) {
1211
+ const { operation, isValid } = apiMap[key];
1212
+ if (!isValid) {
1213
+ continue;
1214
+ }
1215
+ const [method, path] = key.split(" ");
1216
+ const operationId = operation.operationId;
1217
+ // In Browser environment, this api is by default not support api without operationId
1218
+ if (!operationId) {
1219
+ continue;
1220
+ }
1221
+ const command = Utils.parseApiInfo(operation, this.options);
1222
+ const apiInfo = {
1223
+ method: method,
1224
+ path: path,
1225
+ title: command.title,
1226
+ id: operationId,
1227
+ parameters: command.parameters,
1228
+ description: command.description,
1229
+ };
1230
+ apiInfos.push(apiInfo);
1231
+ }
1232
+ return apiInfos;
1233
+ }
1234
+ catch (err) {
1235
+ throw new SpecParserError(err.toString(), ErrorType.listSupportedAPIInfoFailed);
1236
+ }
1237
+ }
1238
+ /**
1239
+ * Lists all the OpenAPI operations in the specification file.
1240
+ * @returns A string array that represents the HTTP method and path of each operation, such as ['GET /pets/{petId}', 'GET /user/{userId}']
1241
+ * according to copilot plugin spec, only list get and post method without auth
1242
+ */
1243
+ // eslint-disable-next-line @typescript-eslint/require-await
1244
+ async list() {
1245
+ throw new Error("Method not implemented.");
1246
+ }
1247
+ /**
1248
+ * Generate specs according to the filters.
1249
+ * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1250
+ */
1251
+ // eslint-disable-next-line @typescript-eslint/require-await
1252
+ async getFilteredSpecs(filter, signal) {
1253
+ throw new Error("Method not implemented.");
1254
+ }
1255
+ /**
1256
+ * Generates and update artifacts from the OpenAPI specification file. Generate Adaptive Cards, update Teams app manifest, and generate a new OpenAPI specification file.
1257
+ * @param manifestPath A file path of the Teams app manifest file to update.
1258
+ * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1259
+ * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
1260
+ * @param pluginFilePath File path of the api plugin file to generate.
1261
+ */
1262
+ // eslint-disable-next-line @typescript-eslint/require-await
1263
+ async generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, signal) {
1264
+ throw new Error("Method not implemented.");
1265
+ }
1266
+ /**
1267
+ * Generates and update artifacts from the OpenAPI specification file. Generate Adaptive Cards, update Teams app manifest, and generate a new OpenAPI specification file.
1268
+ * @param manifestPath A file path of the Teams app manifest file to update.
1269
+ * @param filter An array of strings that represent the filters to apply when generating the artifacts. If filter is empty, it would process nothing.
1270
+ * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
1271
+ * @param adaptiveCardFolder Folder path where the Adaptive Card files will be generated. If not specified or empty, Adaptive Card files will not be generated.
1272
+ * @param isMe Boolean that indicates whether the project is an Messaging Extension. For Messaging Extension, composeExtensions will be added in Teams app manifest.
1273
+ */
1274
+ // eslint-disable-next-line @typescript-eslint/require-await
1275
+ async generate(manifestPath, filter, outputSpecPath, adaptiveCardFolder, signal) {
1276
+ throw new Error("Method not implemented.");
1277
+ }
1278
+ async loadSpec() {
1279
+ if (!this.spec) {
1280
+ this.unResolveSpec = (await this.parser.parse(this.pathOrSpec));
1281
+ if (!this.unResolveSpec.openapi && this.unResolveSpec.swagger === "2.0") {
1282
+ this.isSwaggerFile = true;
1283
+ }
1284
+ const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
1285
+ this.spec = (await this.parser.dereference(clonedUnResolveSpec));
1286
+ }
1287
+ }
1288
+ getAPIs(spec) {
1289
+ const validator = this.getValidator(spec);
1290
+ const apiMap = validator.listAPIs();
1291
+ return apiMap;
1292
+ }
1293
+ getValidator(spec) {
1294
+ if (this.validator) {
1295
+ return this.validator;
1296
+ }
1297
+ const validator = ValidatorFactory.create(spec, this.options);
1298
+ this.validator = validator;
1299
+ return validator;
1300
+ }
1341
1301
  }
1342
1302
 
1343
- // Copyright (c) Microsoft Corporation.
1344
- class JsonDataGenerator {
1345
- static generate(schema) {
1346
- return this.generateMockData(schema);
1347
- }
1348
- static generateMockData(schema) {
1349
- if (this.visitedSchemas.has(schema)) {
1350
- return null; // Prevent circular reference
1351
- }
1352
- this.visitedSchemas.add(schema);
1353
- let result;
1354
- if (schema.anyOf) {
1355
- // Select the first schema in anyOf
1356
- const selectedSchema = schema.anyOf[0];
1357
- result = this.generateMockData(selectedSchema);
1358
- }
1359
- else if (schema.oneOf) {
1360
- // Select the first schema in oneOf
1361
- const selectedSchema = schema.oneOf[0];
1362
- result = this.generateMockData(selectedSchema);
1363
- }
1364
- else if (schema.allOf) {
1365
- // merge all schemas in allOf
1366
- result = {};
1367
- for (const subschema of schema.allOf) {
1368
- const data = this.generateMockData(subschema);
1369
- result = Object.assign(Object.assign({}, result), data);
1370
- }
1371
- }
1372
- else {
1373
- switch (schema.type) {
1374
- case "string":
1375
- if (schema.example !== undefined) {
1376
- result = schema.example;
1377
- }
1378
- else if (schema.format) {
1379
- switch (schema.format) {
1380
- case "date-time":
1381
- result = "2024-11-01T05:25:43.593Z";
1382
- break;
1383
- case "email":
1384
- result = "example@example.com";
1385
- break;
1386
- case "uuid":
1387
- result = "123e4567-e89b-12d3-a456-426614174000";
1388
- break;
1389
- case "ipv4":
1390
- result = "192.168.0.1";
1391
- break;
1392
- case "ipv6":
1393
- result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1394
- break;
1395
- default:
1396
- result = "example string";
1397
- }
1398
- }
1399
- else {
1400
- result = "example string";
1401
- }
1402
- break;
1403
- case "number":
1404
- if (schema.example !== undefined) {
1405
- result = schema.example;
1406
- }
1407
- else if (schema.format) {
1408
- switch (schema.format) {
1409
- case "float":
1410
- result = 3.14;
1411
- break;
1412
- case "double":
1413
- result = 3.14159;
1414
- break;
1415
- default:
1416
- result = 123;
1417
- }
1418
- }
1419
- else {
1420
- result = 123;
1421
- }
1422
- break;
1423
- case "integer":
1424
- if (schema.example !== undefined) {
1425
- result = schema.example;
1426
- }
1427
- else if (schema.format) {
1428
- switch (schema.format) {
1429
- case "int32":
1430
- result = 123456;
1431
- break;
1432
- case "int64":
1433
- result = 123456789;
1434
- break;
1435
- default:
1436
- result = 123;
1437
- }
1438
- }
1439
- else {
1440
- result = 123;
1441
- }
1442
- break;
1443
- case "boolean":
1444
- result = schema.example !== undefined ? schema.example : true;
1445
- break;
1446
- case "array":
1447
- result = [this.generateMockData(schema.items)];
1448
- break;
1449
- case "object":
1450
- result = {};
1451
- if (schema.properties) {
1452
- for (const key in schema.properties) {
1453
- result[key] = this.generateMockData(schema.properties[key]);
1454
- }
1455
- }
1456
- break;
1457
- default:
1458
- result = schema.example || null;
1459
- }
1460
- }
1461
- this.visitedSchemas.delete(schema);
1462
- return result;
1463
- }
1464
- }
1465
- JsonDataGenerator.visitedSchemas = new Set();
1303
+ // Copyright (c) Microsoft Corporation.
1304
+ class JsonDataGenerator {
1305
+ static visitedSchemas = new Set();
1306
+ static generate(schema) {
1307
+ return this.generateMockData(schema);
1308
+ }
1309
+ static generateMockData(schema) {
1310
+ if (this.visitedSchemas.has(schema)) {
1311
+ return null; // Prevent circular reference
1312
+ }
1313
+ this.visitedSchemas.add(schema);
1314
+ let result;
1315
+ if (schema.anyOf) {
1316
+ // Select the first schema in anyOf
1317
+ const selectedSchema = schema.anyOf[0];
1318
+ result = this.generateMockData(selectedSchema);
1319
+ }
1320
+ else if (schema.oneOf) {
1321
+ // Select the first schema in oneOf
1322
+ const selectedSchema = schema.oneOf[0];
1323
+ result = this.generateMockData(selectedSchema);
1324
+ }
1325
+ else if (schema.allOf) {
1326
+ // merge all schemas in allOf
1327
+ result = {};
1328
+ for (const subschema of schema.allOf) {
1329
+ const data = this.generateMockData(subschema);
1330
+ result = { ...result, ...data };
1331
+ }
1332
+ }
1333
+ else {
1334
+ switch (schema.type) {
1335
+ case "string":
1336
+ if (schema.example !== undefined) {
1337
+ result = schema.example;
1338
+ }
1339
+ else if (schema.format) {
1340
+ switch (schema.format) {
1341
+ case "date-time":
1342
+ result = "2024-11-01T05:25:43.593Z";
1343
+ break;
1344
+ case "email":
1345
+ result = "example@example.com";
1346
+ break;
1347
+ case "uuid":
1348
+ result = "123e4567-e89b-12d3-a456-426614174000";
1349
+ break;
1350
+ case "ipv4":
1351
+ result = "192.168.0.1";
1352
+ break;
1353
+ case "ipv6":
1354
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1355
+ break;
1356
+ default:
1357
+ result = "example string";
1358
+ }
1359
+ }
1360
+ else {
1361
+ result = "example string";
1362
+ }
1363
+ break;
1364
+ case "number":
1365
+ if (schema.example !== undefined) {
1366
+ result = schema.example;
1367
+ }
1368
+ else if (schema.format) {
1369
+ switch (schema.format) {
1370
+ case "float":
1371
+ result = 3.14;
1372
+ break;
1373
+ case "double":
1374
+ result = 3.14159;
1375
+ break;
1376
+ default:
1377
+ result = 123;
1378
+ }
1379
+ }
1380
+ else {
1381
+ result = 123;
1382
+ }
1383
+ break;
1384
+ case "integer":
1385
+ if (schema.example !== undefined) {
1386
+ result = schema.example;
1387
+ }
1388
+ else if (schema.format) {
1389
+ switch (schema.format) {
1390
+ case "int32":
1391
+ result = 123456;
1392
+ break;
1393
+ case "int64":
1394
+ result = 123456789;
1395
+ break;
1396
+ default:
1397
+ result = 123;
1398
+ }
1399
+ }
1400
+ else {
1401
+ result = 123;
1402
+ }
1403
+ break;
1404
+ case "boolean":
1405
+ result = schema.example !== undefined ? schema.example : true;
1406
+ break;
1407
+ case "array":
1408
+ result = [this.generateMockData(schema.items)];
1409
+ break;
1410
+ case "object":
1411
+ result = {};
1412
+ if (schema.properties) {
1413
+ for (const key in schema.properties) {
1414
+ result[key] = this.generateMockData(schema.properties[key]);
1415
+ }
1416
+ }
1417
+ break;
1418
+ default:
1419
+ result = schema.example || null;
1420
+ }
1421
+ }
1422
+ this.visitedSchemas.delete(schema);
1423
+ return result;
1424
+ }
1425
+ }
1466
1426
 
1467
- // Copyright (c) Microsoft Corporation.
1468
- class AdaptiveCardGenerator {
1469
- static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1470
- try {
1471
- const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1472
- let cardBody = [];
1473
- let jsonData = {};
1474
- const warnings = [];
1475
- const operationId = operationItem.operationId;
1476
- let schema = json.schema;
1477
- let jsonPath = "$";
1478
- if (schema && Object.keys(schema).length > 0) {
1479
- try {
1480
- jsonData = JsonDataGenerator.generate(schema);
1481
- }
1482
- catch (err) {
1483
- warnings.push({
1484
- type: WarningType.GenerateJsonDataFailed,
1485
- content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1486
- data: operationId,
1487
- });
1488
- }
1489
- jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1490
- if (jsonPath !== "$") {
1491
- schema = schema.properties[jsonPath];
1492
- }
1493
- cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "", "", maxElementCount);
1494
- }
1495
- // if no schema, try to use example value
1496
- if (cardBody.length === 0 && (json.examples || json.example)) {
1497
- cardBody = [
1498
- {
1499
- type: ConstantString.TextBlockType,
1500
- text: "${jsonStringify($root)}",
1501
- wrap: true,
1502
- },
1503
- ];
1504
- }
1505
- // if no example value, use default success response
1506
- if (cardBody.length === 0) {
1507
- cardBody = [
1508
- {
1509
- type: ConstantString.TextBlockType,
1510
- text: "success",
1511
- wrap: true,
1512
- },
1513
- ];
1514
- }
1515
- const fullCard = {
1516
- type: ConstantString.AdaptiveCardType,
1517
- $schema: ConstantString.AdaptiveCardSchema,
1518
- version: ConstantString.AdaptiveCardVersion,
1519
- body: cardBody,
1520
- };
1521
- return [fullCard, jsonPath, jsonData, warnings];
1522
- }
1523
- catch (err) {
1524
- throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
1525
- }
1526
- }
1527
- static generateCardFromResponse(schema, name, parentArrayName = "", maxElementCount = Number.MAX_SAFE_INTEGER, counter = { count: 0 }) {
1528
- if (counter.count >= maxElementCount) {
1529
- return [];
1530
- }
1531
- if (schema.type === "array") {
1532
- // schema.items can be arbitrary object: schema { type: array, items: {} }
1533
- if (Object.keys(schema.items).length === 0) {
1534
- counter.count++;
1535
- return [
1536
- {
1537
- type: ConstantString.TextBlockType,
1538
- text: name ? `${name}: \${jsonStringify(${name})}` : "result: ${jsonStringify($root)}",
1539
- wrap: true,
1540
- },
1541
- ];
1542
- }
1543
- const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name, maxElementCount, counter);
1544
- const template = {
1545
- type: ConstantString.ContainerType,
1546
- $data: name ? `\${${name}}` : "${$root}",
1547
- items: Array(),
1548
- };
1549
- template.items.push(...obj);
1550
- return [template];
1551
- }
1552
- // some schema may not contain type but contain properties
1553
- if (Utils.isObjectSchema(schema)) {
1554
- const { properties } = schema;
1555
- const result = [];
1556
- for (const property in properties) {
1557
- const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName, maxElementCount, counter);
1558
- result.push(...obj);
1559
- }
1560
- if (schema.additionalProperties) {
1561
- // TODO: better ways to handler warnings.
1562
- console.warn(ConstantString.AdditionalPropertiesNotSupported);
1563
- }
1564
- return result;
1565
- }
1566
- if (schema.type === "string" ||
1567
- schema.type === "integer" ||
1568
- schema.type === "boolean" ||
1569
- schema.type === "number") {
1570
- counter.count++;
1571
- if (!AdaptiveCardGenerator.isImageUrlProperty(schema, name, parentArrayName)) {
1572
- // string in root: "ddd"
1573
- let text = "result: ${$root}";
1574
- if (name) {
1575
- // object { id: "1" }
1576
- text = `${name}: \${if(${name}, ${name}, 'N/A')}`;
1577
- if (parentArrayName) {
1578
- // object types inside array: { tags: ["id": 1, "name": "name"] }
1579
- text = `${parentArrayName}.${text}`;
1580
- }
1581
- }
1582
- else if (parentArrayName) {
1583
- // string array: photoUrls: ["1", "2"]
1584
- text = `${parentArrayName}: ` + "${$data}";
1585
- }
1586
- return [
1587
- {
1588
- type: ConstantString.TextBlockType,
1589
- text,
1590
- wrap: true,
1591
- },
1592
- ];
1593
- }
1594
- else {
1595
- const url = name ? `\${${name}}` : "${$data}";
1596
- const condition = name
1597
- ? `\${${name} != null && ${name} != ''}`
1598
- : "${$data != null && $data != ''}";
1599
- return [
1600
- {
1601
- type: "Image",
1602
- url,
1603
- $when: condition,
1604
- },
1605
- ];
1606
- }
1607
- }
1608
- if (schema.oneOf || schema.anyOf || schema.not || schema.allOf) {
1609
- throw new Error(Utils.format(ConstantString.SchemaNotSupported, JSON.stringify(schema)));
1610
- }
1611
- throw new Error(Utils.format(ConstantString.UnknownSchema, JSON.stringify(schema)));
1612
- }
1613
- // Find the first array property in the response schema object with the well-known name
1614
- static getResponseJsonPathFromSchema(schema) {
1615
- if (Utils.isObjectSchema(schema)) {
1616
- const { properties } = schema;
1617
- for (const property in properties) {
1618
- const schema = properties[property];
1619
- if (schema.type === "array" &&
1620
- Utils.isWellKnownName(property, ConstantString.WellknownResultNames)) {
1621
- return property;
1622
- }
1623
- }
1624
- }
1625
- return "$";
1626
- }
1627
- static isImageUrlProperty(schema, name, parentArrayName) {
1628
- const propertyName = name ? name : parentArrayName;
1629
- return (!!propertyName &&
1630
- schema.type === "string" &&
1631
- Utils.isWellKnownName(propertyName, ConstantString.WellknownImageName) &&
1632
- (propertyName.toLocaleLowerCase().indexOf("url") >= 0 || schema.format === "uri"));
1633
- }
1427
+ // Copyright (c) Microsoft Corporation.
1428
+ class AdaptiveCardGenerator {
1429
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1430
+ try {
1431
+ const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1432
+ let cardBody = [];
1433
+ let jsonData = {};
1434
+ const warnings = [];
1435
+ const operationId = operationItem.operationId;
1436
+ let schema = json.schema;
1437
+ let jsonPath = "$";
1438
+ if (schema && Object.keys(schema).length > 0) {
1439
+ try {
1440
+ jsonData = JsonDataGenerator.generate(schema);
1441
+ }
1442
+ catch (err) {
1443
+ warnings.push({
1444
+ type: WarningType.GenerateJsonDataFailed,
1445
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1446
+ data: operationId,
1447
+ });
1448
+ }
1449
+ jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1450
+ if (jsonPath !== "$") {
1451
+ schema = schema.properties[jsonPath];
1452
+ }
1453
+ cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "", "", maxElementCount);
1454
+ }
1455
+ // if no schema, try to use example value
1456
+ if (cardBody.length === 0 && (json.examples || json.example)) {
1457
+ cardBody = [
1458
+ {
1459
+ type: ConstantString.TextBlockType,
1460
+ text: "${jsonStringify($root)}",
1461
+ wrap: true,
1462
+ },
1463
+ ];
1464
+ }
1465
+ // if no example value, use default success response
1466
+ if (cardBody.length === 0) {
1467
+ cardBody = [
1468
+ {
1469
+ type: ConstantString.TextBlockType,
1470
+ text: "success",
1471
+ wrap: true,
1472
+ },
1473
+ ];
1474
+ }
1475
+ const fullCard = {
1476
+ type: ConstantString.AdaptiveCardType,
1477
+ $schema: ConstantString.AdaptiveCardSchema,
1478
+ version: ConstantString.AdaptiveCardVersion,
1479
+ body: cardBody,
1480
+ };
1481
+ return [fullCard, jsonPath, jsonData, warnings];
1482
+ }
1483
+ catch (err) {
1484
+ throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
1485
+ }
1486
+ }
1487
+ static generateCardFromResponse(schema, name, parentArrayName = "", maxElementCount = Number.MAX_SAFE_INTEGER, counter = { count: 0 }) {
1488
+ if (counter.count >= maxElementCount) {
1489
+ return [];
1490
+ }
1491
+ if (schema.type === "array") {
1492
+ // schema.items can be arbitrary object: schema { type: array, items: {} }
1493
+ if (Object.keys(schema.items).length === 0) {
1494
+ counter.count++;
1495
+ return [
1496
+ {
1497
+ type: ConstantString.TextBlockType,
1498
+ text: name ? `${name}: \${jsonStringify(${name})}` : "result: ${jsonStringify($root)}",
1499
+ wrap: true,
1500
+ },
1501
+ ];
1502
+ }
1503
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name, maxElementCount, counter);
1504
+ const template = {
1505
+ type: ConstantString.ContainerType,
1506
+ $data: name ? `\${${name}}` : "${$root}",
1507
+ items: Array(),
1508
+ };
1509
+ template.items.push(...obj);
1510
+ return [template];
1511
+ }
1512
+ // some schema may not contain type but contain properties
1513
+ if (Utils.isObjectSchema(schema)) {
1514
+ const { properties } = schema;
1515
+ const result = [];
1516
+ for (const property in properties) {
1517
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName, maxElementCount, counter);
1518
+ result.push(...obj);
1519
+ }
1520
+ if (schema.additionalProperties) {
1521
+ // TODO: better ways to handler warnings.
1522
+ console.warn(ConstantString.AdditionalPropertiesNotSupported);
1523
+ }
1524
+ return result;
1525
+ }
1526
+ if (schema.type === "string" ||
1527
+ schema.type === "integer" ||
1528
+ schema.type === "boolean" ||
1529
+ schema.type === "number") {
1530
+ counter.count++;
1531
+ if (!AdaptiveCardGenerator.isImageUrlProperty(schema, name, parentArrayName)) {
1532
+ // string in root: "ddd"
1533
+ let text = "result: ${$root}";
1534
+ if (name) {
1535
+ // object { id: "1" }
1536
+ text = `${name}: \${if(${name}, ${name}, 'N/A')}`;
1537
+ if (parentArrayName) {
1538
+ // object types inside array: { tags: ["id": 1, "name": "name"] }
1539
+ text = `${parentArrayName}.${text}`;
1540
+ }
1541
+ }
1542
+ else if (parentArrayName) {
1543
+ // string array: photoUrls: ["1", "2"]
1544
+ text = `${parentArrayName}: ` + "${$data}";
1545
+ }
1546
+ return [
1547
+ {
1548
+ type: ConstantString.TextBlockType,
1549
+ text,
1550
+ wrap: true,
1551
+ },
1552
+ ];
1553
+ }
1554
+ else {
1555
+ const url = name ? `\${${name}}` : "${$data}";
1556
+ const condition = name
1557
+ ? `\${${name} != null && ${name} != ''}`
1558
+ : "${$data != null && $data != ''}";
1559
+ return [
1560
+ {
1561
+ type: "Image",
1562
+ url,
1563
+ $when: condition,
1564
+ },
1565
+ ];
1566
+ }
1567
+ }
1568
+ if (schema.oneOf || schema.anyOf || schema.not || schema.allOf) {
1569
+ throw new Error(Utils.format(ConstantString.SchemaNotSupported, JSON.stringify(schema)));
1570
+ }
1571
+ throw new Error(Utils.format(ConstantString.UnknownSchema, JSON.stringify(schema)));
1572
+ }
1573
+ // Find the first array property in the response schema object with the well-known name
1574
+ static getResponseJsonPathFromSchema(schema) {
1575
+ if (Utils.isObjectSchema(schema)) {
1576
+ const { properties } = schema;
1577
+ for (const property in properties) {
1578
+ const schema = properties[property];
1579
+ if (schema.type === "array" &&
1580
+ Utils.isWellKnownName(property, ConstantString.WellknownResultNames)) {
1581
+ return property;
1582
+ }
1583
+ }
1584
+ }
1585
+ return "$";
1586
+ }
1587
+ static isImageUrlProperty(schema, name, parentArrayName) {
1588
+ const propertyName = name ? name : parentArrayName;
1589
+ return (!!propertyName &&
1590
+ schema.type === "string" &&
1591
+ Utils.isWellKnownName(propertyName, ConstantString.WellknownImageName) &&
1592
+ (propertyName.toLocaleLowerCase().indexOf("url") >= 0 || schema.format === "uri"));
1593
+ }
1634
1594
  }
1635
1595
 
1636
1596
  export { AdaptiveCardGenerator, ConstantString, ErrorType, SpecParser, SpecParserError, Utils, ValidationStatus, WarningType };