@microsoft/m365-spec-parser 0.2.1 → 0.2.2-alpha.02a122217.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.
@@ -13,10 +13,12 @@ var ErrorType;
13
13
  ErrorType["RelativeServerUrlNotSupported"] = "relative-server-url-not-supported";
14
14
  ErrorType["NoSupportedApi"] = "no-supported-api";
15
15
  ErrorType["NoExtraAPICanBeAdded"] = "no-extra-api-can-be-added";
16
+ ErrorType["AddedAPINotInOriginalSpec"] = "added-api-not-in-original-spec";
16
17
  ErrorType["ResolveServerUrlFailed"] = "resolve-server-url-failed";
17
18
  ErrorType["SwaggerNotSupported"] = "swagger-not-supported";
18
19
  ErrorType["MultipleAuthNotSupported"] = "multiple-auth-not-supported";
19
20
  ErrorType["SpecVersionNotSupported"] = "spec-version-not-supported";
21
+ ErrorType["CircularReferenceNotSupported"] = "circular-reference-not-supported";
20
22
  ErrorType["ListFailed"] = "list-failed";
21
23
  ErrorType["listSupportedAPIInfoFailed"] = "list-supported-api-info-failed";
22
24
  ErrorType["FilterSpecFailed"] = "filter-spec-failed";
@@ -253,21 +255,28 @@ class Utils {
253
255
  static updateFirstLetter(str) {
254
256
  return str.charAt(0).toUpperCase() + str.slice(1);
255
257
  }
256
- static getResponseJson(operationObject) {
257
- var _a, _b;
258
+ static getResponseJson(operationObject, allowMultipleMediaType = false) {
259
+ var _a;
258
260
  let json = {};
259
261
  let multipleMediaType = false;
260
262
  for (const code of ConstantString.ResponseCodeFor20X) {
261
263
  const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
262
- if ((_b = responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) === null || _b === void 0 ? void 0 : _b["application/json"]) {
263
- multipleMediaType = false;
264
- json = responseObject.content["application/json"];
265
- if (Utils.containMultipleMediaTypes(responseObject)) {
266
- multipleMediaType = true;
267
- json = {};
268
- }
269
- else {
270
- break;
264
+ if (responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) {
265
+ for (const contentType of Object.keys(responseObject.content)) {
266
+ // json media type can also be "application/json; charset=utf-8"
267
+ if (contentType.indexOf("application/json") >= 0) {
268
+ multipleMediaType = false;
269
+ json = responseObject.content[contentType];
270
+ if (Utils.containMultipleMediaTypes(responseObject)) {
271
+ multipleMediaType = true;
272
+ if (!allowMultipleMediaType) {
273
+ json = {};
274
+ }
275
+ }
276
+ else {
277
+ return { json, multipleMediaType };
278
+ }
279
+ }
271
280
  }
272
281
  }
273
282
  }
@@ -528,10 +537,46 @@ class Utils {
528
537
  const serverUrl = operationServer || methodServer || rootServer;
529
538
  return serverUrl;
530
539
  }
540
+ static limitACBodyProperties(body, maxCount) {
541
+ const result = [];
542
+ let currentCount = 0;
543
+ for (const element of body) {
544
+ if (element.type === ConstantString.ContainerType) {
545
+ const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
546
+ result.push({
547
+ type: ConstantString.ContainerType,
548
+ $data: element.$data,
549
+ items: items,
550
+ });
551
+ currentCount += items.length;
552
+ }
553
+ else {
554
+ result.push(element);
555
+ currentCount++;
556
+ }
557
+ if (currentCount >= maxCount) {
558
+ break;
559
+ }
560
+ }
561
+ return result;
562
+ }
531
563
  }
532
564
 
533
565
  // Copyright (c) Microsoft Corporation.
534
566
  class Validator {
567
+ constructor() {
568
+ this.hasCircularReference = false;
569
+ }
570
+ checkCircularReference() {
571
+ try {
572
+ JSON.stringify(this.spec);
573
+ }
574
+ catch (e) {
575
+ if (e.message.includes("Converting circular structure to JSON")) {
576
+ this.hasCircularReference = true;
577
+ }
578
+ }
579
+ }
535
580
  listAPIs() {
536
581
  var _a;
537
582
  if (this.apiMap) {
@@ -627,6 +672,22 @@ class Validator {
627
672
  }
628
673
  return result;
629
674
  }
675
+ validateCircularReference(method, path) {
676
+ const result = { isValid: true, reason: [] };
677
+ if (this.hasCircularReference) {
678
+ const operationObject = this.spec.paths[path][method];
679
+ try {
680
+ JSON.stringify(operationObject);
681
+ }
682
+ catch (e) {
683
+ if (e.message.includes("Converting circular structure to JSON")) {
684
+ result.isValid = false;
685
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
686
+ }
687
+ }
688
+ }
689
+ return result;
690
+ }
630
691
  validateResponse(method, path) {
631
692
  const result = { isValid: true, reason: [] };
632
693
  const operationObject = this.spec.paths[path][method];
@@ -815,6 +876,7 @@ class CopilotValidator extends Validator {
815
876
  this.projectType = ProjectType.Copilot;
816
877
  this.options = options;
817
878
  this.spec = spec;
879
+ this.checkCircularReference();
818
880
  }
819
881
  validateSpec() {
820
882
  const result = { errors: [], warnings: [] };
@@ -840,6 +902,10 @@ class CopilotValidator extends Validator {
840
902
  if (!methodAndPathResult.isValid) {
841
903
  return methodAndPathResult;
842
904
  }
905
+ const circularReferenceResult = this.validateCircularReference(method, path);
906
+ if (!circularReferenceResult.isValid) {
907
+ return circularReferenceResult;
908
+ }
843
909
  const operationObject = this.spec.paths[path][method];
844
910
  // validate auth
845
911
  const authCheckResult = this.validateAuth(method, path);
@@ -883,6 +949,7 @@ class SMEValidator extends Validator {
883
949
  this.projectType = ProjectType.SME;
884
950
  this.options = options;
885
951
  this.spec = spec;
952
+ this.checkCircularReference();
886
953
  }
887
954
  validateSpec() {
888
955
  const result = { errors: [], warnings: [] };
@@ -910,6 +977,10 @@ class SMEValidator extends Validator {
910
977
  if (!methodAndPathResult.isValid) {
911
978
  return methodAndPathResult;
912
979
  }
980
+ const circularReferenceResult = this.validateCircularReference(method, path);
981
+ if (!circularReferenceResult.isValid) {
982
+ return circularReferenceResult;
983
+ }
913
984
  const operationObject = this.spec.paths[path][method];
914
985
  // validate auth
915
986
  const authCheckResult = this.validateAuth(method, path);
@@ -980,6 +1051,7 @@ class TeamsAIValidator extends Validator {
980
1051
  this.projectType = ProjectType.TeamsAi;
981
1052
  this.options = options;
982
1053
  this.spec = spec;
1054
+ this.checkCircularReference();
983
1055
  }
984
1056
  validateSpec() {
985
1057
  const result = { errors: [], warnings: [] };
@@ -999,6 +1071,10 @@ class TeamsAIValidator extends Validator {
999
1071
  if (!methodAndPathResult.isValid) {
1000
1072
  return methodAndPathResult;
1001
1073
  }
1074
+ const circularReferenceResult = this.validateCircularReference(method, path);
1075
+ if (!circularReferenceResult.isValid) {
1076
+ return circularReferenceResult;
1077
+ }
1002
1078
  const operationObject = this.spec.paths[path][method];
1003
1079
  // validate operationId
1004
1080
  if (!this.options.allowMissingId && !operationObject.operationId) {
@@ -1227,9 +1303,9 @@ class SpecParser {
1227
1303
 
1228
1304
  // Copyright (c) Microsoft Corporation.
1229
1305
  class AdaptiveCardGenerator {
1230
- static generateAdaptiveCard(operationItem) {
1306
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1231
1307
  try {
1232
- const { json } = Utils.getResponseJson(operationItem);
1308
+ const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1233
1309
  let cardBody = [];
1234
1310
  let schema = json.schema;
1235
1311
  let jsonPath = "$";
@@ -1340,7 +1416,7 @@ class AdaptiveCardGenerator {
1340
1416
  {
1341
1417
  type: "Image",
1342
1418
  url: `\${${name}}`,
1343
- $when: `\${${name} != null}`,
1419
+ $when: `\${${name} != null && ${name} != ''}`,
1344
1420
  },
1345
1421
  ];
1346
1422
  }
@@ -1349,7 +1425,7 @@ class AdaptiveCardGenerator {
1349
1425
  {
1350
1426
  type: "Image",
1351
1427
  url: "${$data}",
1352
- $when: "${$data != null}",
1428
+ $when: "${$data != null && $data != ''}",
1353
1429
  },
1354
1430
  ];
1355
1431
  }