@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.
@@ -43,10 +43,12 @@ var ErrorType;
43
43
  ErrorType["RelativeServerUrlNotSupported"] = "relative-server-url-not-supported";
44
44
  ErrorType["NoSupportedApi"] = "no-supported-api";
45
45
  ErrorType["NoExtraAPICanBeAdded"] = "no-extra-api-can-be-added";
46
+ ErrorType["AddedAPINotInOriginalSpec"] = "added-api-not-in-original-spec";
46
47
  ErrorType["ResolveServerUrlFailed"] = "resolve-server-url-failed";
47
48
  ErrorType["SwaggerNotSupported"] = "swagger-not-supported";
48
49
  ErrorType["MultipleAuthNotSupported"] = "multiple-auth-not-supported";
49
50
  ErrorType["SpecVersionNotSupported"] = "spec-version-not-supported";
51
+ ErrorType["CircularReferenceNotSupported"] = "circular-reference-not-supported";
50
52
  ErrorType["ListFailed"] = "list-failed";
51
53
  ErrorType["listSupportedAPIInfoFailed"] = "list-supported-api-info-failed";
52
54
  ErrorType["FilterSpecFailed"] = "filter-spec-failed";
@@ -283,7 +285,7 @@ class Utils {
283
285
  static updateFirstLetter(str) {
284
286
  return str.charAt(0).toUpperCase() + str.slice(1);
285
287
  }
286
- static getResponseJson(operationObject) {
288
+ static getResponseJson(operationObject, allowMultipleMediaType = false) {
287
289
  var _a, _b;
288
290
  let json = {};
289
291
  let multipleMediaType = false;
@@ -294,7 +296,9 @@ class Utils {
294
296
  json = responseObject.content["application/json"];
295
297
  if (Utils.containMultipleMediaTypes(responseObject)) {
296
298
  multipleMediaType = true;
297
- json = {};
299
+ if (!allowMultipleMediaType) {
300
+ json = {};
301
+ }
298
302
  }
299
303
  else {
300
304
  break;
@@ -558,10 +562,45 @@ class Utils {
558
562
  const serverUrl = operationServer || methodServer || rootServer;
559
563
  return serverUrl;
560
564
  }
565
+ static limitACBodyProperties(body, maxCount) {
566
+ const result = [];
567
+ let currentCount = 0;
568
+ for (const element of body) {
569
+ if (element.type === ConstantString.ContainerType) {
570
+ const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
571
+ result.push({
572
+ type: ConstantString.ContainerType,
573
+ $data: element.$data,
574
+ items: items,
575
+ });
576
+ currentCount += items.length;
577
+ }
578
+ else {
579
+ if (currentCount < maxCount) {
580
+ result.push(element);
581
+ currentCount++;
582
+ }
583
+ }
584
+ }
585
+ return result;
586
+ }
561
587
  }
562
588
 
563
589
  // Copyright (c) Microsoft Corporation.
564
590
  class Validator {
591
+ constructor() {
592
+ this.hasCircularReference = false;
593
+ }
594
+ checkCircularReference() {
595
+ try {
596
+ JSON.stringify(this.spec);
597
+ }
598
+ catch (e) {
599
+ if (e.message.includes("Converting circular structure to JSON")) {
600
+ this.hasCircularReference = true;
601
+ }
602
+ }
603
+ }
565
604
  listAPIs() {
566
605
  var _a;
567
606
  if (this.apiMap) {
@@ -657,6 +696,22 @@ class Validator {
657
696
  }
658
697
  return result;
659
698
  }
699
+ validateCircularReference(method, path) {
700
+ const result = { isValid: true, reason: [] };
701
+ if (this.hasCircularReference) {
702
+ const operationObject = this.spec.paths[path][method];
703
+ try {
704
+ JSON.stringify(operationObject);
705
+ }
706
+ catch (e) {
707
+ if (e.message.includes("Converting circular structure to JSON")) {
708
+ result.isValid = false;
709
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
710
+ }
711
+ }
712
+ }
713
+ return result;
714
+ }
660
715
  validateResponse(method, path) {
661
716
  const result = { isValid: true, reason: [] };
662
717
  const operationObject = this.spec.paths[path][method];
@@ -845,6 +900,7 @@ class CopilotValidator extends Validator {
845
900
  this.projectType = ProjectType.Copilot;
846
901
  this.options = options;
847
902
  this.spec = spec;
903
+ this.checkCircularReference();
848
904
  }
849
905
  validateSpec() {
850
906
  const result = { errors: [], warnings: [] };
@@ -870,6 +926,10 @@ class CopilotValidator extends Validator {
870
926
  if (!methodAndPathResult.isValid) {
871
927
  return methodAndPathResult;
872
928
  }
929
+ const circularReferenceResult = this.validateCircularReference(method, path);
930
+ if (!circularReferenceResult.isValid) {
931
+ return circularReferenceResult;
932
+ }
873
933
  const operationObject = this.spec.paths[path][method];
874
934
  // validate auth
875
935
  const authCheckResult = this.validateAuth(method, path);
@@ -913,6 +973,7 @@ class SMEValidator extends Validator {
913
973
  this.projectType = ProjectType.SME;
914
974
  this.options = options;
915
975
  this.spec = spec;
976
+ this.checkCircularReference();
916
977
  }
917
978
  validateSpec() {
918
979
  const result = { errors: [], warnings: [] };
@@ -940,6 +1001,10 @@ class SMEValidator extends Validator {
940
1001
  if (!methodAndPathResult.isValid) {
941
1002
  return methodAndPathResult;
942
1003
  }
1004
+ const circularReferenceResult = this.validateCircularReference(method, path);
1005
+ if (!circularReferenceResult.isValid) {
1006
+ return circularReferenceResult;
1007
+ }
943
1008
  const operationObject = this.spec.paths[path][method];
944
1009
  // validate auth
945
1010
  const authCheckResult = this.validateAuth(method, path);
@@ -1010,6 +1075,7 @@ class TeamsAIValidator extends Validator {
1010
1075
  this.projectType = ProjectType.TeamsAi;
1011
1076
  this.options = options;
1012
1077
  this.spec = spec;
1078
+ this.checkCircularReference();
1013
1079
  }
1014
1080
  validateSpec() {
1015
1081
  const result = { errors: [], warnings: [] };
@@ -1029,6 +1095,10 @@ class TeamsAIValidator extends Validator {
1029
1095
  if (!methodAndPathResult.isValid) {
1030
1096
  return methodAndPathResult;
1031
1097
  }
1098
+ const circularReferenceResult = this.validateCircularReference(method, path);
1099
+ if (!circularReferenceResult.isValid) {
1100
+ return circularReferenceResult;
1101
+ }
1032
1102
  const operationObject = this.spec.paths[path][method];
1033
1103
  // validate operationId
1034
1104
  if (!this.options.allowMissingId && !operationObject.operationId) {
@@ -1271,9 +1341,9 @@ class SpecParser {
1271
1341
 
1272
1342
  // Copyright (c) Microsoft Corporation.
1273
1343
  class AdaptiveCardGenerator {
1274
- static generateAdaptiveCard(operationItem) {
1344
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1275
1345
  try {
1276
- const { json } = Utils.getResponseJson(operationItem);
1346
+ const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1277
1347
  let cardBody = [];
1278
1348
  let schema = json.schema;
1279
1349
  let jsonPath = "$";