@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.
@@ -188,16 +188,19 @@ ConstantString.PluginManifestSchema = "https://developer.microsoft.com/json-sche
188
188
  // Copyright (c) Microsoft Corporation.
189
189
  class Utils {
190
190
  static hasNestedObjectInSchema(schema) {
191
- if (schema.type === "object") {
191
+ if (this.isObjectSchema(schema)) {
192
192
  for (const property in schema.properties) {
193
193
  const nestedSchema = schema.properties[property];
194
- if (nestedSchema.type === "object") {
194
+ if (this.isObjectSchema(nestedSchema)) {
195
195
  return true;
196
196
  }
197
197
  }
198
198
  }
199
199
  return false;
200
200
  }
201
+ static isObjectSchema(schema) {
202
+ return schema.type === "object" || (!schema.type && !!schema.properties);
203
+ }
201
204
  static containMultipleMediaTypes(bodyObject) {
202
205
  return Object.keys((bodyObject === null || bodyObject === void 0 ? void 0 : bodyObject.content) || {}).length > 1;
203
206
  }
@@ -426,7 +429,7 @@ class Utils {
426
429
  optionalParams.push(parameter);
427
430
  }
428
431
  }
429
- else if (schema.type === "object") {
432
+ else if (Utils.isObjectSchema(schema)) {
430
433
  const { properties } = schema;
431
434
  for (const property in properties) {
432
435
  let isRequired = false;
@@ -540,29 +543,6 @@ class Utils {
540
543
  const serverUrl = operationServer || methodServer || rootServer;
541
544
  return serverUrl;
542
545
  }
543
- static limitACBodyProperties(body, maxCount) {
544
- const result = [];
545
- let currentCount = 0;
546
- for (const element of body) {
547
- if (element.type === ConstantString.ContainerType) {
548
- const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
549
- result.push({
550
- type: ConstantString.ContainerType,
551
- $data: element.$data,
552
- items: items,
553
- });
554
- currentCount += items.length;
555
- }
556
- else {
557
- result.push(element);
558
- currentCount++;
559
- }
560
- if (currentCount >= maxCount) {
561
- break;
562
- }
563
- }
564
- return result;
565
- }
566
546
  }
567
547
 
568
548
  // Copyright (c) Microsoft Corporation.
@@ -764,7 +744,7 @@ class Validator {
764
744
  }
765
745
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
766
746
  const isCopilot = this.projectType === ProjectType.Copilot;
767
- if (isCopilot && this.hasNestedObjectInSchema(schema)) {
747
+ if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
768
748
  paramResult.isValid = false;
769
749
  paramResult.reason = [ErrorType.RequestBodyContainsNestedObject];
770
750
  return paramResult;
@@ -780,7 +760,7 @@ class Validator {
780
760
  paramResult.optionalNum = paramResult.optionalNum + 1;
781
761
  }
782
762
  }
783
- else if (schema.type === "object") {
763
+ else if (Utils.isObjectSchema(schema)) {
784
764
  const { properties } = schema;
785
765
  for (const property in properties) {
786
766
  let isRequired = false;
@@ -816,7 +796,7 @@ class Validator {
816
796
  for (let i = 0; i < paramObject.length; i++) {
817
797
  const param = paramObject[i];
818
798
  const schema = param.schema;
819
- if (isCopilot && this.hasNestedObjectInSchema(schema)) {
799
+ if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
820
800
  paramResult.isValid = false;
821
801
  paramResult.reason.push(ErrorType.ParamsContainsNestedObject);
822
802
  continue;
@@ -859,17 +839,6 @@ class Validator {
859
839
  }
860
840
  return paramResult;
861
841
  }
862
- hasNestedObjectInSchema(schema) {
863
- if (schema.type === "object") {
864
- for (const property in schema.properties) {
865
- const nestedSchema = schema.properties[property];
866
- if (nestedSchema.type === "object") {
867
- return true;
868
- }
869
- }
870
- }
871
- return false;
872
- }
873
842
  }
874
843
 
875
844
  // Copyright (c) Microsoft Corporation.
@@ -928,7 +897,7 @@ class CopilotValidator extends Validator {
928
897
  const requestJsonBody = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content["application/json"];
929
898
  if (requestJsonBody) {
930
899
  const requestBodySchema = requestJsonBody.schema;
931
- if (requestBodySchema.type !== "object") {
900
+ if (!Utils.isObjectSchema(requestBodySchema)) {
932
901
  result.reason.push(ErrorType.PostBodySchemaIsNotJson);
933
902
  }
934
903
  const requestBodyParamResult = this.checkPostBodySchema(requestBodySchema, requestBody.required);
@@ -1306,7 +1275,7 @@ class SpecParser {
1306
1275
 
1307
1276
  // Copyright (c) Microsoft Corporation.
1308
1277
  class AdaptiveCardGenerator {
1309
- static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1278
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1310
1279
  try {
1311
1280
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1312
1281
  let cardBody = [];
@@ -1317,7 +1286,7 @@ class AdaptiveCardGenerator {
1317
1286
  if (jsonPath !== "$") {
1318
1287
  schema = schema.properties[jsonPath];
1319
1288
  }
1320
- cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "");
1289
+ cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "", "", maxElementCount);
1321
1290
  }
1322
1291
  // if no schema, try to use example value
1323
1292
  if (cardBody.length === 0 && (json.examples || json.example)) {
@@ -1351,10 +1320,14 @@ class AdaptiveCardGenerator {
1351
1320
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
1352
1321
  }
1353
1322
  }
1354
- static generateCardFromResponse(schema, name, parentArrayName = "") {
1323
+ static generateCardFromResponse(schema, name, parentArrayName = "", maxElementCount = Number.MAX_SAFE_INTEGER, counter = { count: 0 }) {
1324
+ if (counter.count >= maxElementCount) {
1325
+ return [];
1326
+ }
1355
1327
  if (schema.type === "array") {
1356
1328
  // schema.items can be arbitrary object: schema { type: array, items: {} }
1357
1329
  if (Object.keys(schema.items).length === 0) {
1330
+ counter.count++;
1358
1331
  return [
1359
1332
  {
1360
1333
  type: ConstantString.TextBlockType,
@@ -1363,7 +1336,7 @@ class AdaptiveCardGenerator {
1363
1336
  },
1364
1337
  ];
1365
1338
  }
1366
- const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name);
1339
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name, maxElementCount, counter);
1367
1340
  const template = {
1368
1341
  type: ConstantString.ContainerType,
1369
1342
  $data: name ? `\${${name}}` : "${$root}",
@@ -1373,11 +1346,11 @@ class AdaptiveCardGenerator {
1373
1346
  return [template];
1374
1347
  }
1375
1348
  // some schema may not contain type but contain properties
1376
- if (schema.type === "object" || (!schema.type && schema.properties)) {
1349
+ if (Utils.isObjectSchema(schema)) {
1377
1350
  const { properties } = schema;
1378
1351
  const result = [];
1379
1352
  for (const property in properties) {
1380
- const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName);
1353
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName, maxElementCount, counter);
1381
1354
  result.push(...obj);
1382
1355
  }
1383
1356
  if (schema.additionalProperties) {
@@ -1390,6 +1363,7 @@ class AdaptiveCardGenerator {
1390
1363
  schema.type === "integer" ||
1391
1364
  schema.type === "boolean" ||
1392
1365
  schema.type === "number") {
1366
+ counter.count++;
1393
1367
  if (!AdaptiveCardGenerator.isImageUrlProperty(schema, name, parentArrayName)) {
1394
1368
  // string in root: "ddd"
1395
1369
  let text = "result: ${$root}";
@@ -1414,24 +1388,17 @@ class AdaptiveCardGenerator {
1414
1388
  ];
1415
1389
  }
1416
1390
  else {
1417
- if (name) {
1418
- return [
1419
- {
1420
- type: "Image",
1421
- url: `\${${name}}`,
1422
- $when: `\${${name} != null && ${name} != ''}`,
1423
- },
1424
- ];
1425
- }
1426
- else {
1427
- return [
1428
- {
1429
- type: "Image",
1430
- url: "${$data}",
1431
- $when: "${$data != null && $data != ''}",
1432
- },
1433
- ];
1434
- }
1391
+ const url = name ? `\${${name}}` : "${$data}";
1392
+ const condition = name
1393
+ ? `\${${name} != null && ${name} != ''}`
1394
+ : "${$data != null && $data != ''}";
1395
+ return [
1396
+ {
1397
+ type: "Image",
1398
+ url,
1399
+ $when: condition,
1400
+ },
1401
+ ];
1435
1402
  }
1436
1403
  }
1437
1404
  if (schema.oneOf || schema.anyOf || schema.not || schema.allOf) {
@@ -1441,7 +1408,7 @@ class AdaptiveCardGenerator {
1441
1408
  }
1442
1409
  // Find the first array property in the response schema object with the well-known name
1443
1410
  static getResponseJsonPathFromSchema(schema) {
1444
- if (schema.type === "object" || (!schema.type && schema.properties)) {
1411
+ if (Utils.isObjectSchema(schema)) {
1445
1412
  const { properties } = schema;
1446
1413
  for (const property in properties) {
1447
1414
  const schema = properties[property];