@microsoft/m365-spec-parser 0.2.1 → 0.2.2-alpha.0921562c9.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,7 +255,7 @@ class Utils {
253
255
  static updateFirstLetter(str) {
254
256
  return str.charAt(0).toUpperCase() + str.slice(1);
255
257
  }
256
- static getResponseJson(operationObject) {
258
+ static getResponseJson(operationObject, allowMultipleMediaType = false) {
257
259
  var _a, _b;
258
260
  let json = {};
259
261
  let multipleMediaType = false;
@@ -264,7 +266,9 @@ class Utils {
264
266
  json = responseObject.content["application/json"];
265
267
  if (Utils.containMultipleMediaTypes(responseObject)) {
266
268
  multipleMediaType = true;
267
- json = {};
269
+ if (!allowMultipleMediaType) {
270
+ json = {};
271
+ }
268
272
  }
269
273
  else {
270
274
  break;
@@ -528,10 +532,45 @@ class Utils {
528
532
  const serverUrl = operationServer || methodServer || rootServer;
529
533
  return serverUrl;
530
534
  }
535
+ static limitACBodyProperties(body, maxCount) {
536
+ const result = [];
537
+ let currentCount = 0;
538
+ for (const element of body) {
539
+ if (element.type === ConstantString.ContainerType) {
540
+ const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
541
+ result.push({
542
+ type: ConstantString.ContainerType,
543
+ $data: element.$data,
544
+ items: items,
545
+ });
546
+ currentCount += items.length;
547
+ }
548
+ else {
549
+ if (currentCount < maxCount) {
550
+ result.push(element);
551
+ currentCount++;
552
+ }
553
+ }
554
+ }
555
+ return result;
556
+ }
531
557
  }
532
558
 
533
559
  // Copyright (c) Microsoft Corporation.
534
560
  class Validator {
561
+ constructor() {
562
+ this.hasCircularReference = false;
563
+ }
564
+ checkCircularReference() {
565
+ try {
566
+ JSON.stringify(this.spec);
567
+ }
568
+ catch (e) {
569
+ if (e.message.includes("Converting circular structure to JSON")) {
570
+ this.hasCircularReference = true;
571
+ }
572
+ }
573
+ }
535
574
  listAPIs() {
536
575
  var _a;
537
576
  if (this.apiMap) {
@@ -627,6 +666,22 @@ class Validator {
627
666
  }
628
667
  return result;
629
668
  }
669
+ validateCircularReference(method, path) {
670
+ const result = { isValid: true, reason: [] };
671
+ if (this.hasCircularReference) {
672
+ const operationObject = this.spec.paths[path][method];
673
+ try {
674
+ JSON.stringify(operationObject);
675
+ }
676
+ catch (e) {
677
+ if (e.message.includes("Converting circular structure to JSON")) {
678
+ result.isValid = false;
679
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
680
+ }
681
+ }
682
+ }
683
+ return result;
684
+ }
630
685
  validateResponse(method, path) {
631
686
  const result = { isValid: true, reason: [] };
632
687
  const operationObject = this.spec.paths[path][method];
@@ -815,6 +870,7 @@ class CopilotValidator extends Validator {
815
870
  this.projectType = ProjectType.Copilot;
816
871
  this.options = options;
817
872
  this.spec = spec;
873
+ this.checkCircularReference();
818
874
  }
819
875
  validateSpec() {
820
876
  const result = { errors: [], warnings: [] };
@@ -840,6 +896,10 @@ class CopilotValidator extends Validator {
840
896
  if (!methodAndPathResult.isValid) {
841
897
  return methodAndPathResult;
842
898
  }
899
+ const circularReferenceResult = this.validateCircularReference(method, path);
900
+ if (!circularReferenceResult.isValid) {
901
+ return circularReferenceResult;
902
+ }
843
903
  const operationObject = this.spec.paths[path][method];
844
904
  // validate auth
845
905
  const authCheckResult = this.validateAuth(method, path);
@@ -883,6 +943,7 @@ class SMEValidator extends Validator {
883
943
  this.projectType = ProjectType.SME;
884
944
  this.options = options;
885
945
  this.spec = spec;
946
+ this.checkCircularReference();
886
947
  }
887
948
  validateSpec() {
888
949
  const result = { errors: [], warnings: [] };
@@ -910,6 +971,10 @@ class SMEValidator extends Validator {
910
971
  if (!methodAndPathResult.isValid) {
911
972
  return methodAndPathResult;
912
973
  }
974
+ const circularReferenceResult = this.validateCircularReference(method, path);
975
+ if (!circularReferenceResult.isValid) {
976
+ return circularReferenceResult;
977
+ }
913
978
  const operationObject = this.spec.paths[path][method];
914
979
  // validate auth
915
980
  const authCheckResult = this.validateAuth(method, path);
@@ -980,6 +1045,7 @@ class TeamsAIValidator extends Validator {
980
1045
  this.projectType = ProjectType.TeamsAi;
981
1046
  this.options = options;
982
1047
  this.spec = spec;
1048
+ this.checkCircularReference();
983
1049
  }
984
1050
  validateSpec() {
985
1051
  const result = { errors: [], warnings: [] };
@@ -999,6 +1065,10 @@ class TeamsAIValidator extends Validator {
999
1065
  if (!methodAndPathResult.isValid) {
1000
1066
  return methodAndPathResult;
1001
1067
  }
1068
+ const circularReferenceResult = this.validateCircularReference(method, path);
1069
+ if (!circularReferenceResult.isValid) {
1070
+ return circularReferenceResult;
1071
+ }
1002
1072
  const operationObject = this.spec.paths[path][method];
1003
1073
  // validate operationId
1004
1074
  if (!this.options.allowMissingId && !operationObject.operationId) {
@@ -1227,9 +1297,9 @@ class SpecParser {
1227
1297
 
1228
1298
  // Copyright (c) Microsoft Corporation.
1229
1299
  class AdaptiveCardGenerator {
1230
- static generateAdaptiveCard(operationItem) {
1300
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1231
1301
  try {
1232
- const { json } = Utils.getResponseJson(operationItem);
1302
+ const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1233
1303
  let cardBody = [];
1234
1304
  let schema = json.schema;
1235
1305
  let jsonPath = "$";