@microsoft/m365-spec-parser 0.2.0 → 0.2.1-alpha.20d61baa8.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.
@@ -17,10 +17,12 @@ var ErrorType;
17
17
  ErrorType["RelativeServerUrlNotSupported"] = "relative-server-url-not-supported";
18
18
  ErrorType["NoSupportedApi"] = "no-supported-api";
19
19
  ErrorType["NoExtraAPICanBeAdded"] = "no-extra-api-can-be-added";
20
+ ErrorType["AddedAPINotInOriginalSpec"] = "added-api-not-in-original-spec";
20
21
  ErrorType["ResolveServerUrlFailed"] = "resolve-server-url-failed";
21
22
  ErrorType["SwaggerNotSupported"] = "swagger-not-supported";
22
23
  ErrorType["MultipleAuthNotSupported"] = "multiple-auth-not-supported";
23
24
  ErrorType["SpecVersionNotSupported"] = "spec-version-not-supported";
25
+ ErrorType["CircularReferenceNotSupported"] = "circular-reference-not-supported";
24
26
  ErrorType["ListFailed"] = "list-failed";
25
27
  ErrorType["listSupportedAPIInfoFailed"] = "list-supported-api-info-failed";
26
28
  ErrorType["FilterSpecFailed"] = "filter-spec-failed";
@@ -536,6 +538,19 @@ class Utils {
536
538
 
537
539
  // Copyright (c) Microsoft Corporation.
538
540
  class Validator {
541
+ constructor() {
542
+ this.hasCircularReference = false;
543
+ }
544
+ checkCircularReference() {
545
+ try {
546
+ JSON.stringify(this.spec);
547
+ }
548
+ catch (e) {
549
+ if (e.message.includes("Converting circular structure to JSON")) {
550
+ this.hasCircularReference = true;
551
+ }
552
+ }
553
+ }
539
554
  listAPIs() {
540
555
  var _a;
541
556
  if (this.apiMap) {
@@ -631,6 +646,22 @@ class Validator {
631
646
  }
632
647
  return result;
633
648
  }
649
+ validateCircularReference(method, path) {
650
+ const result = { isValid: true, reason: [] };
651
+ if (this.hasCircularReference) {
652
+ const operationObject = this.spec.paths[path][method];
653
+ try {
654
+ JSON.stringify(operationObject);
655
+ }
656
+ catch (e) {
657
+ if (e.message.includes("Converting circular structure to JSON")) {
658
+ result.isValid = false;
659
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
660
+ }
661
+ }
662
+ }
663
+ return result;
664
+ }
634
665
  validateResponse(method, path) {
635
666
  const result = { isValid: true, reason: [] };
636
667
  const operationObject = this.spec.paths[path][method];
@@ -819,6 +850,7 @@ class CopilotValidator extends Validator {
819
850
  this.projectType = ProjectType.Copilot;
820
851
  this.options = options;
821
852
  this.spec = spec;
853
+ this.checkCircularReference();
822
854
  }
823
855
  validateSpec() {
824
856
  const result = { errors: [], warnings: [] };
@@ -844,6 +876,10 @@ class CopilotValidator extends Validator {
844
876
  if (!methodAndPathResult.isValid) {
845
877
  return methodAndPathResult;
846
878
  }
879
+ const circularReferenceResult = this.validateCircularReference(method, path);
880
+ if (!circularReferenceResult.isValid) {
881
+ return circularReferenceResult;
882
+ }
847
883
  const operationObject = this.spec.paths[path][method];
848
884
  // validate auth
849
885
  const authCheckResult = this.validateAuth(method, path);
@@ -887,6 +923,7 @@ class SMEValidator extends Validator {
887
923
  this.projectType = ProjectType.SME;
888
924
  this.options = options;
889
925
  this.spec = spec;
926
+ this.checkCircularReference();
890
927
  }
891
928
  validateSpec() {
892
929
  const result = { errors: [], warnings: [] };
@@ -914,6 +951,10 @@ class SMEValidator extends Validator {
914
951
  if (!methodAndPathResult.isValid) {
915
952
  return methodAndPathResult;
916
953
  }
954
+ const circularReferenceResult = this.validateCircularReference(method, path);
955
+ if (!circularReferenceResult.isValid) {
956
+ return circularReferenceResult;
957
+ }
917
958
  const operationObject = this.spec.paths[path][method];
918
959
  // validate auth
919
960
  const authCheckResult = this.validateAuth(method, path);
@@ -984,6 +1025,7 @@ class TeamsAIValidator extends Validator {
984
1025
  this.projectType = ProjectType.TeamsAi;
985
1026
  this.options = options;
986
1027
  this.spec = spec;
1028
+ this.checkCircularReference();
987
1029
  }
988
1030
  validateSpec() {
989
1031
  const result = { errors: [], warnings: [] };
@@ -1003,6 +1045,10 @@ class TeamsAIValidator extends Validator {
1003
1045
  if (!methodAndPathResult.isValid) {
1004
1046
  return methodAndPathResult;
1005
1047
  }
1048
+ const circularReferenceResult = this.validateCircularReference(method, path);
1049
+ if (!circularReferenceResult.isValid) {
1050
+ return circularReferenceResult;
1051
+ }
1006
1052
  const operationObject = this.spec.paths[path][method];
1007
1053
  // validate operationId
1008
1054
  if (!this.options.allowMissingId && !operationObject.operationId) {
@@ -1731,7 +1777,13 @@ class SpecParser {
1731
1777
  try {
1732
1778
  try {
1733
1779
  await this.loadSpec();
1734
- await this.parser.validate(this.spec);
1780
+ if (!this.parser.$refs.circular) {
1781
+ await this.parser.validate(this.spec);
1782
+ }
1783
+ else {
1784
+ const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
1785
+ await this.parser.validate(clonedUnResolveSpec);
1786
+ }
1735
1787
  }
1736
1788
  catch (e) {
1737
1789
  return {
@@ -1862,7 +1914,8 @@ class SpecParser {
1862
1914
  if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
1863
1915
  throw new SpecParserError(ConstantString.CancelledMessage, ErrorType.Cancelled);
1864
1916
  }
1865
- const newSpec = (await this.parser.dereference(newUnResolvedSpec));
1917
+ const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
1918
+ const newSpec = (await this.parser.dereference(clonedUnResolveSpec));
1866
1919
  return [newUnResolvedSpec, newSpec];
1867
1920
  }
1868
1921
  catch (err) {