@microsoft/m365-spec-parser 0.2.1-rc-hotfix.0 → 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.
@@ -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,21 +285,28 @@ class Utils {
283
285
  static updateFirstLetter(str) {
284
286
  return str.charAt(0).toUpperCase() + str.slice(1);
285
287
  }
286
- static getResponseJson(operationObject) {
287
- var _a, _b;
288
+ static getResponseJson(operationObject, allowMultipleMediaType = false) {
289
+ var _a;
288
290
  let json = {};
289
291
  let multipleMediaType = false;
290
292
  for (const code of ConstantString.ResponseCodeFor20X) {
291
293
  const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
292
- if ((_b = responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) === null || _b === void 0 ? void 0 : _b["application/json"]) {
293
- multipleMediaType = false;
294
- json = responseObject.content["application/json"];
295
- if (Utils.containMultipleMediaTypes(responseObject)) {
296
- multipleMediaType = true;
297
- json = {};
298
- }
299
- else {
300
- break;
294
+ if (responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) {
295
+ for (const contentType of Object.keys(responseObject.content)) {
296
+ // json media type can also be "application/json; charset=utf-8"
297
+ if (contentType.indexOf("application/json") >= 0) {
298
+ multipleMediaType = false;
299
+ json = responseObject.content[contentType];
300
+ if (Utils.containMultipleMediaTypes(responseObject)) {
301
+ multipleMediaType = true;
302
+ if (!allowMultipleMediaType) {
303
+ json = {};
304
+ }
305
+ }
306
+ else {
307
+ return { json, multipleMediaType };
308
+ }
309
+ }
301
310
  }
302
311
  }
303
312
  }
@@ -558,10 +567,46 @@ class Utils {
558
567
  const serverUrl = operationServer || methodServer || rootServer;
559
568
  return serverUrl;
560
569
  }
570
+ static limitACBodyProperties(body, maxCount) {
571
+ const result = [];
572
+ let currentCount = 0;
573
+ for (const element of body) {
574
+ if (element.type === ConstantString.ContainerType) {
575
+ const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
576
+ result.push({
577
+ type: ConstantString.ContainerType,
578
+ $data: element.$data,
579
+ items: items,
580
+ });
581
+ currentCount += items.length;
582
+ }
583
+ else {
584
+ result.push(element);
585
+ currentCount++;
586
+ }
587
+ if (currentCount >= maxCount) {
588
+ break;
589
+ }
590
+ }
591
+ return result;
592
+ }
561
593
  }
562
594
 
563
595
  // Copyright (c) Microsoft Corporation.
564
596
  class Validator {
597
+ constructor() {
598
+ this.hasCircularReference = false;
599
+ }
600
+ checkCircularReference() {
601
+ try {
602
+ JSON.stringify(this.spec);
603
+ }
604
+ catch (e) {
605
+ if (e.message.includes("Converting circular structure to JSON")) {
606
+ this.hasCircularReference = true;
607
+ }
608
+ }
609
+ }
565
610
  listAPIs() {
566
611
  var _a;
567
612
  if (this.apiMap) {
@@ -657,6 +702,22 @@ class Validator {
657
702
  }
658
703
  return result;
659
704
  }
705
+ validateCircularReference(method, path) {
706
+ const result = { isValid: true, reason: [] };
707
+ if (this.hasCircularReference) {
708
+ const operationObject = this.spec.paths[path][method];
709
+ try {
710
+ JSON.stringify(operationObject);
711
+ }
712
+ catch (e) {
713
+ if (e.message.includes("Converting circular structure to JSON")) {
714
+ result.isValid = false;
715
+ result.reason.push(ErrorType.CircularReferenceNotSupported);
716
+ }
717
+ }
718
+ }
719
+ return result;
720
+ }
660
721
  validateResponse(method, path) {
661
722
  const result = { isValid: true, reason: [] };
662
723
  const operationObject = this.spec.paths[path][method];
@@ -845,6 +906,7 @@ class CopilotValidator extends Validator {
845
906
  this.projectType = ProjectType.Copilot;
846
907
  this.options = options;
847
908
  this.spec = spec;
909
+ this.checkCircularReference();
848
910
  }
849
911
  validateSpec() {
850
912
  const result = { errors: [], warnings: [] };
@@ -870,6 +932,10 @@ class CopilotValidator extends Validator {
870
932
  if (!methodAndPathResult.isValid) {
871
933
  return methodAndPathResult;
872
934
  }
935
+ const circularReferenceResult = this.validateCircularReference(method, path);
936
+ if (!circularReferenceResult.isValid) {
937
+ return circularReferenceResult;
938
+ }
873
939
  const operationObject = this.spec.paths[path][method];
874
940
  // validate auth
875
941
  const authCheckResult = this.validateAuth(method, path);
@@ -913,6 +979,7 @@ class SMEValidator extends Validator {
913
979
  this.projectType = ProjectType.SME;
914
980
  this.options = options;
915
981
  this.spec = spec;
982
+ this.checkCircularReference();
916
983
  }
917
984
  validateSpec() {
918
985
  const result = { errors: [], warnings: [] };
@@ -940,6 +1007,10 @@ class SMEValidator extends Validator {
940
1007
  if (!methodAndPathResult.isValid) {
941
1008
  return methodAndPathResult;
942
1009
  }
1010
+ const circularReferenceResult = this.validateCircularReference(method, path);
1011
+ if (!circularReferenceResult.isValid) {
1012
+ return circularReferenceResult;
1013
+ }
943
1014
  const operationObject = this.spec.paths[path][method];
944
1015
  // validate auth
945
1016
  const authCheckResult = this.validateAuth(method, path);
@@ -1010,6 +1081,7 @@ class TeamsAIValidator extends Validator {
1010
1081
  this.projectType = ProjectType.TeamsAi;
1011
1082
  this.options = options;
1012
1083
  this.spec = spec;
1084
+ this.checkCircularReference();
1013
1085
  }
1014
1086
  validateSpec() {
1015
1087
  const result = { errors: [], warnings: [] };
@@ -1029,6 +1101,10 @@ class TeamsAIValidator extends Validator {
1029
1101
  if (!methodAndPathResult.isValid) {
1030
1102
  return methodAndPathResult;
1031
1103
  }
1104
+ const circularReferenceResult = this.validateCircularReference(method, path);
1105
+ if (!circularReferenceResult.isValid) {
1106
+ return circularReferenceResult;
1107
+ }
1032
1108
  const operationObject = this.spec.paths[path][method];
1033
1109
  // validate operationId
1034
1110
  if (!this.options.allowMissingId && !operationObject.operationId) {
@@ -1271,9 +1347,9 @@ class SpecParser {
1271
1347
 
1272
1348
  // Copyright (c) Microsoft Corporation.
1273
1349
  class AdaptiveCardGenerator {
1274
- static generateAdaptiveCard(operationItem) {
1350
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1275
1351
  try {
1276
- const { json } = Utils.getResponseJson(operationItem);
1352
+ const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1277
1353
  let cardBody = [];
1278
1354
  let schema = json.schema;
1279
1355
  let jsonPath = "$";
@@ -1384,7 +1460,7 @@ class AdaptiveCardGenerator {
1384
1460
  {
1385
1461
  type: "Image",
1386
1462
  url: `\${${name}}`,
1387
- $when: `\${${name} != null}`,
1463
+ $when: `\${${name} != null && ${name} != ''}`,
1388
1464
  },
1389
1465
  ];
1390
1466
  }
@@ -1393,7 +1469,7 @@ class AdaptiveCardGenerator {
1393
1469
  {
1394
1470
  type: "Image",
1395
1471
  url: "${$data}",
1396
- $when: "${$data != null}",
1472
+ $when: "${$data != null && $data != ''}",
1397
1473
  },
1398
1474
  ];
1399
1475
  }