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