@microsoft/m365-spec-parser 0.2.2 → 0.2.3-alpha.572d2b73b.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.
@@ -194,16 +194,19 @@ class SpecParserError extends Error {
194
194
  // Copyright (c) Microsoft Corporation.
195
195
  class Utils {
196
196
  static hasNestedObjectInSchema(schema) {
197
- if (schema.type === "object") {
197
+ if (this.isObjectSchema(schema)) {
198
198
  for (const property in schema.properties) {
199
199
  const nestedSchema = schema.properties[property];
200
- if (nestedSchema.type === "object") {
200
+ if (this.isObjectSchema(nestedSchema)) {
201
201
  return true;
202
202
  }
203
203
  }
204
204
  }
205
205
  return false;
206
206
  }
207
+ static isObjectSchema(schema) {
208
+ return schema.type === "object" || (!schema.type && !!schema.properties);
209
+ }
207
210
  static containMultipleMediaTypes(bodyObject) {
208
211
  return Object.keys((bodyObject === null || bodyObject === void 0 ? void 0 : bodyObject.content) || {}).length > 1;
209
212
  }
@@ -432,7 +435,7 @@ class Utils {
432
435
  optionalParams.push(parameter);
433
436
  }
434
437
  }
435
- else if (schema.type === "object") {
438
+ else if (Utils.isObjectSchema(schema)) {
436
439
  const { properties } = schema;
437
440
  for (const property in properties) {
438
441
  let isRequired = false;
@@ -546,29 +549,6 @@ class Utils {
546
549
  const serverUrl = operationServer || methodServer || rootServer;
547
550
  return serverUrl;
548
551
  }
549
- static limitACBodyProperties(body, maxCount) {
550
- const result = [];
551
- let currentCount = 0;
552
- for (const element of body) {
553
- if (element.type === ConstantString.ContainerType) {
554
- const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
555
- result.push({
556
- type: ConstantString.ContainerType,
557
- $data: element.$data,
558
- items: items,
559
- });
560
- currentCount += items.length;
561
- }
562
- else {
563
- result.push(element);
564
- currentCount++;
565
- }
566
- if (currentCount >= maxCount) {
567
- break;
568
- }
569
- }
570
- return result;
571
- }
572
552
  }
573
553
 
574
554
  // Copyright (c) Microsoft Corporation.
@@ -770,7 +750,7 @@ class Validator {
770
750
  }
771
751
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
772
752
  const isCopilot = this.projectType === ProjectType.Copilot;
773
- if (isCopilot && this.hasNestedObjectInSchema(schema)) {
753
+ if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
774
754
  paramResult.isValid = false;
775
755
  paramResult.reason = [ErrorType.RequestBodyContainsNestedObject];
776
756
  return paramResult;
@@ -786,7 +766,7 @@ class Validator {
786
766
  paramResult.optionalNum = paramResult.optionalNum + 1;
787
767
  }
788
768
  }
789
- else if (schema.type === "object") {
769
+ else if (Utils.isObjectSchema(schema)) {
790
770
  const { properties } = schema;
791
771
  for (const property in properties) {
792
772
  let isRequired = false;
@@ -822,7 +802,7 @@ class Validator {
822
802
  for (let i = 0; i < paramObject.length; i++) {
823
803
  const param = paramObject[i];
824
804
  const schema = param.schema;
825
- if (isCopilot && this.hasNestedObjectInSchema(schema)) {
805
+ if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
826
806
  paramResult.isValid = false;
827
807
  paramResult.reason.push(ErrorType.ParamsContainsNestedObject);
828
808
  continue;
@@ -865,17 +845,6 @@ class Validator {
865
845
  }
866
846
  return paramResult;
867
847
  }
868
- hasNestedObjectInSchema(schema) {
869
- if (schema.type === "object") {
870
- for (const property in schema.properties) {
871
- const nestedSchema = schema.properties[property];
872
- if (nestedSchema.type === "object") {
873
- return true;
874
- }
875
- }
876
- }
877
- return false;
878
- }
879
848
  }
880
849
 
881
850
  // Copyright (c) Microsoft Corporation.
@@ -934,7 +903,7 @@ class CopilotValidator extends Validator {
934
903
  const requestJsonBody = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content["application/json"];
935
904
  if (requestJsonBody) {
936
905
  const requestBodySchema = requestJsonBody.schema;
937
- if (requestBodySchema.type !== "object") {
906
+ if (!Utils.isObjectSchema(requestBodySchema)) {
938
907
  result.reason.push(ErrorType.PostBodySchemaIsNotJson);
939
908
  }
940
909
  const requestBodyParamResult = this.checkPostBodySchema(requestBodySchema, requestBody.required);
@@ -1314,7 +1283,7 @@ class SpecFilter {
1314
1283
 
1315
1284
  // Copyright (c) Microsoft Corporation.
1316
1285
  class AdaptiveCardGenerator {
1317
- static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1286
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1318
1287
  try {
1319
1288
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1320
1289
  let cardBody = [];
@@ -1325,7 +1294,7 @@ class AdaptiveCardGenerator {
1325
1294
  if (jsonPath !== "$") {
1326
1295
  schema = schema.properties[jsonPath];
1327
1296
  }
1328
- cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "");
1297
+ cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "", "", maxElementCount);
1329
1298
  }
1330
1299
  // if no schema, try to use example value
1331
1300
  if (cardBody.length === 0 && (json.examples || json.example)) {
@@ -1359,10 +1328,14 @@ class AdaptiveCardGenerator {
1359
1328
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
1360
1329
  }
1361
1330
  }
1362
- static generateCardFromResponse(schema, name, parentArrayName = "") {
1331
+ static generateCardFromResponse(schema, name, parentArrayName = "", maxElementCount = Number.MAX_SAFE_INTEGER, counter = { count: 0 }) {
1332
+ if (counter.count >= maxElementCount) {
1333
+ return [];
1334
+ }
1363
1335
  if (schema.type === "array") {
1364
1336
  // schema.items can be arbitrary object: schema { type: array, items: {} }
1365
1337
  if (Object.keys(schema.items).length === 0) {
1338
+ counter.count++;
1366
1339
  return [
1367
1340
  {
1368
1341
  type: ConstantString.TextBlockType,
@@ -1371,7 +1344,7 @@ class AdaptiveCardGenerator {
1371
1344
  },
1372
1345
  ];
1373
1346
  }
1374
- const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name);
1347
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name, maxElementCount, counter);
1375
1348
  const template = {
1376
1349
  type: ConstantString.ContainerType,
1377
1350
  $data: name ? `\${${name}}` : "${$root}",
@@ -1381,11 +1354,11 @@ class AdaptiveCardGenerator {
1381
1354
  return [template];
1382
1355
  }
1383
1356
  // some schema may not contain type but contain properties
1384
- if (schema.type === "object" || (!schema.type && schema.properties)) {
1357
+ if (Utils.isObjectSchema(schema)) {
1385
1358
  const { properties } = schema;
1386
1359
  const result = [];
1387
1360
  for (const property in properties) {
1388
- const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName);
1361
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName, maxElementCount, counter);
1389
1362
  result.push(...obj);
1390
1363
  }
1391
1364
  if (schema.additionalProperties) {
@@ -1398,6 +1371,7 @@ class AdaptiveCardGenerator {
1398
1371
  schema.type === "integer" ||
1399
1372
  schema.type === "boolean" ||
1400
1373
  schema.type === "number") {
1374
+ counter.count++;
1401
1375
  if (!AdaptiveCardGenerator.isImageUrlProperty(schema, name, parentArrayName)) {
1402
1376
  // string in root: "ddd"
1403
1377
  let text = "result: ${$root}";
@@ -1422,24 +1396,17 @@ class AdaptiveCardGenerator {
1422
1396
  ];
1423
1397
  }
1424
1398
  else {
1425
- if (name) {
1426
- return [
1427
- {
1428
- type: "Image",
1429
- url: `\${${name}}`,
1430
- $when: `\${${name} != null && ${name} != ''}`,
1431
- },
1432
- ];
1433
- }
1434
- else {
1435
- return [
1436
- {
1437
- type: "Image",
1438
- url: "${$data}",
1439
- $when: "${$data != null && $data != ''}",
1440
- },
1441
- ];
1442
- }
1399
+ const url = name ? `\${${name}}` : "${$data}";
1400
+ const condition = name
1401
+ ? `\${${name} != null && ${name} != ''}`
1402
+ : "${$data != null && $data != ''}";
1403
+ return [
1404
+ {
1405
+ type: "Image",
1406
+ url,
1407
+ $when: condition,
1408
+ },
1409
+ ];
1443
1410
  }
1444
1411
  }
1445
1412
  if (schema.oneOf || schema.anyOf || schema.not || schema.allOf) {
@@ -1449,7 +1416,7 @@ class AdaptiveCardGenerator {
1449
1416
  }
1450
1417
  // Find the first array property in the response schema object with the well-known name
1451
1418
  static getResponseJsonPathFromSchema(schema) {
1452
- if (schema.type === "object" || (!schema.type && schema.properties)) {
1419
+ if (Utils.isObjectSchema(schema)) {
1453
1420
  const { properties } = schema;
1454
1421
  for (const property in properties) {
1455
1422
  const schema = properties[property];
@@ -1701,7 +1668,7 @@ class ManifestUpdater {
1701
1668
  if (requestBody) {
1702
1669
  const requestJsonBody = requestBody.content["application/json"];
1703
1670
  const requestBodySchema = requestJsonBody.schema;
1704
- if (requestBodySchema.type === "object") {
1671
+ if (Utils.isObjectSchema(requestBodySchema)) {
1705
1672
  for (const property in requestBodySchema.properties) {
1706
1673
  const schema = requestBodySchema.properties[property];
1707
1674
  ManifestUpdater.checkSchema(schema, method, pathUrl);
@@ -1729,8 +1696,7 @@ class ManifestUpdater {
1729
1696
  try {
1730
1697
  const { json } = Utils.getResponseJson(operationItem);
1731
1698
  if (json.schema) {
1732
- const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem);
1733
- card.body = Utils.limitACBodyProperties(card.body, 5);
1699
+ const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, false, 5);
1734
1700
  const responseSemantic = wrapResponseSemantics(card, jsonPath);
1735
1701
  funcObj.capabilities = {
1736
1702
  response_semantics: responseSemantic,