@microsoft/m365-spec-parser 0.2.3-alpha.dc2ed64c9.0 → 0.2.3-alpha.ea529ab0a.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.
@@ -55,6 +55,8 @@ var WarningType;
55
55
  WarningType["OperationOnlyContainsOptionalParam"] = "operation-only-contains-optional-param";
56
56
  WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
57
57
  WarningType["FuncDescriptionTooLong"] = "function-description-too-long";
58
+ WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
59
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
58
60
  WarningType["Unknown"] = "unknown";
59
61
  })(WarningType || (WarningType = {}));
60
62
  /**
@@ -100,8 +102,10 @@ ConstantString.ConvertSwaggerToOpenAPI = "The Swagger 2.0 file has been converte
100
102
  ConstantString.SwaggerNotSupported = "Swagger 2.0 is not supported. Please convert to OpenAPI 3.0 manually before proceeding.";
101
103
  ConstantString.SpecVersionNotSupported = "Unsupported OpenAPI version %s. Please use version 3.0.x.";
102
104
  ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are unsupported. Ensure all selected APIs use identical authentication.";
105
+ ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
103
106
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
104
107
  ConstantString.FuncDescriptionTooLong = "The description of the function '%s' is too long. The current length is %s characters, while the maximum allowed length is %s characters.";
108
+ ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
105
109
  ConstantString.WrappedCardVersion = "devPreview";
106
110
  ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
107
111
  ConstantString.WrappedCardResponseLayout = "list";
@@ -543,29 +547,6 @@ class Utils {
543
547
  const serverUrl = operationServer || methodServer || rootServer;
544
548
  return serverUrl;
545
549
  }
546
- static limitACBodyProperties(body, maxCount) {
547
- const result = [];
548
- let currentCount = 0;
549
- for (const element of body) {
550
- if (element.type === ConstantString.ContainerType) {
551
- const items = this.limitACBodyProperties(element.items, maxCount - currentCount);
552
- result.push({
553
- type: ConstantString.ContainerType,
554
- $data: element.$data,
555
- items: items,
556
- });
557
- currentCount += items.length;
558
- }
559
- else {
560
- result.push(element);
561
- currentCount++;
562
- }
563
- if (currentCount >= maxCount) {
564
- break;
565
- }
566
- }
567
- return result;
568
- }
569
550
  }
570
551
 
571
552
  // Copyright (c) Microsoft Corporation.
@@ -1296,20 +1277,157 @@ class SpecParser {
1296
1277
  }
1297
1278
  }
1298
1279
 
1280
+ // Copyright (c) Microsoft Corporation.
1281
+ class JsonDataGenerator {
1282
+ static generate(schema) {
1283
+ return this.generateMockData(schema);
1284
+ }
1285
+ static generateMockData(schema) {
1286
+ if (this.visitedSchemas.has(schema)) {
1287
+ return null; // Prevent circular reference
1288
+ }
1289
+ this.visitedSchemas.add(schema);
1290
+ let result;
1291
+ if (schema.anyOf) {
1292
+ // Select the first schema in anyOf
1293
+ const selectedSchema = schema.anyOf[0];
1294
+ result = this.generateMockData(selectedSchema);
1295
+ }
1296
+ else if (schema.oneOf) {
1297
+ // Select the first schema in oneOf
1298
+ const selectedSchema = schema.oneOf[0];
1299
+ result = this.generateMockData(selectedSchema);
1300
+ }
1301
+ else if (schema.allOf) {
1302
+ // merge all schemas in allOf
1303
+ result = {};
1304
+ for (const subschema of schema.allOf) {
1305
+ const data = this.generateMockData(subschema);
1306
+ result = Object.assign(Object.assign({}, result), data);
1307
+ }
1308
+ }
1309
+ else {
1310
+ switch (schema.type) {
1311
+ case "string":
1312
+ if (schema.example !== undefined) {
1313
+ result = schema.example;
1314
+ }
1315
+ else if (schema.format) {
1316
+ switch (schema.format) {
1317
+ case "date-time":
1318
+ result = "2024-11-01T05:25:43.593Z";
1319
+ break;
1320
+ case "email":
1321
+ result = "example@example.com";
1322
+ break;
1323
+ case "uuid":
1324
+ result = "123e4567-e89b-12d3-a456-426614174000";
1325
+ break;
1326
+ case "ipv4":
1327
+ result = "192.168.0.1";
1328
+ break;
1329
+ case "ipv6":
1330
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1331
+ break;
1332
+ default:
1333
+ result = "example string";
1334
+ }
1335
+ }
1336
+ else {
1337
+ result = "example string";
1338
+ }
1339
+ break;
1340
+ case "number":
1341
+ if (schema.example !== undefined) {
1342
+ result = schema.example;
1343
+ }
1344
+ else if (schema.format) {
1345
+ switch (schema.format) {
1346
+ case "float":
1347
+ result = 3.14;
1348
+ break;
1349
+ case "double":
1350
+ result = 3.14159;
1351
+ break;
1352
+ default:
1353
+ result = 123;
1354
+ }
1355
+ }
1356
+ else {
1357
+ result = 123;
1358
+ }
1359
+ break;
1360
+ case "integer":
1361
+ if (schema.example !== undefined) {
1362
+ result = schema.example;
1363
+ }
1364
+ else if (schema.format) {
1365
+ switch (schema.format) {
1366
+ case "int32":
1367
+ result = 123456;
1368
+ break;
1369
+ case "int64":
1370
+ result = 123456789;
1371
+ break;
1372
+ default:
1373
+ result = 123;
1374
+ }
1375
+ }
1376
+ else {
1377
+ result = 123;
1378
+ }
1379
+ break;
1380
+ case "boolean":
1381
+ result = schema.example !== undefined ? schema.example : true;
1382
+ break;
1383
+ case "array":
1384
+ result = [this.generateMockData(schema.items)];
1385
+ break;
1386
+ case "object":
1387
+ result = {};
1388
+ if (schema.properties) {
1389
+ for (const key in schema.properties) {
1390
+ result[key] = this.generateMockData(schema.properties[key]);
1391
+ }
1392
+ }
1393
+ break;
1394
+ default:
1395
+ result = schema.example || null;
1396
+ }
1397
+ }
1398
+ this.visitedSchemas.delete(schema);
1399
+ return result;
1400
+ }
1401
+ }
1402
+ JsonDataGenerator.visitedSchemas = new Set();
1403
+
1299
1404
  // Copyright (c) Microsoft Corporation.
1300
1405
  class AdaptiveCardGenerator {
1301
- static generateAdaptiveCard(operationItem, allowMultipleMediaType = false) {
1406
+ static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1302
1407
  try {
1303
1408
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1304
1409
  let cardBody = [];
1410
+ let jsonData = {};
1411
+ const warnings = [];
1412
+ const operationId = operationItem.operationId;
1305
1413
  let schema = json.schema;
1306
1414
  let jsonPath = "$";
1307
1415
  if (schema && Object.keys(schema).length > 0) {
1416
+ try {
1417
+ jsonData = JsonDataGenerator.generate(schema);
1418
+ }
1419
+ catch (err) {
1420
+ warnings.push({
1421
+ type: WarningType.GenerateJsonDataFailed,
1422
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1423
+ data: operationId,
1424
+ });
1425
+ }
1308
1426
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1309
1427
  if (jsonPath !== "$") {
1310
1428
  schema = schema.properties[jsonPath];
1311
1429
  }
1312
- cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "");
1430
+ cardBody = AdaptiveCardGenerator.generateCardFromResponse(schema, "", "", maxElementCount);
1313
1431
  }
1314
1432
  // if no schema, try to use example value
1315
1433
  if (cardBody.length === 0 && (json.examples || json.example)) {
@@ -1337,16 +1455,20 @@ class AdaptiveCardGenerator {
1337
1455
  version: ConstantString.AdaptiveCardVersion,
1338
1456
  body: cardBody,
1339
1457
  };
1340
- return [fullCard, jsonPath];
1458
+ return [fullCard, jsonPath, jsonData, warnings];
1341
1459
  }
1342
1460
  catch (err) {
1343
1461
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
1344
1462
  }
1345
1463
  }
1346
- static generateCardFromResponse(schema, name, parentArrayName = "") {
1464
+ static generateCardFromResponse(schema, name, parentArrayName = "", maxElementCount = Number.MAX_SAFE_INTEGER, counter = { count: 0 }) {
1465
+ if (counter.count >= maxElementCount) {
1466
+ return [];
1467
+ }
1347
1468
  if (schema.type === "array") {
1348
1469
  // schema.items can be arbitrary object: schema { type: array, items: {} }
1349
1470
  if (Object.keys(schema.items).length === 0) {
1471
+ counter.count++;
1350
1472
  return [
1351
1473
  {
1352
1474
  type: ConstantString.TextBlockType,
@@ -1355,7 +1477,7 @@ class AdaptiveCardGenerator {
1355
1477
  },
1356
1478
  ];
1357
1479
  }
1358
- const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name);
1480
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(schema.items, "", name, maxElementCount, counter);
1359
1481
  const template = {
1360
1482
  type: ConstantString.ContainerType,
1361
1483
  $data: name ? `\${${name}}` : "${$root}",
@@ -1369,7 +1491,7 @@ class AdaptiveCardGenerator {
1369
1491
  const { properties } = schema;
1370
1492
  const result = [];
1371
1493
  for (const property in properties) {
1372
- const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName);
1494
+ const obj = AdaptiveCardGenerator.generateCardFromResponse(properties[property], name ? `${name}.${property}` : property, parentArrayName, maxElementCount, counter);
1373
1495
  result.push(...obj);
1374
1496
  }
1375
1497
  if (schema.additionalProperties) {
@@ -1382,6 +1504,7 @@ class AdaptiveCardGenerator {
1382
1504
  schema.type === "integer" ||
1383
1505
  schema.type === "boolean" ||
1384
1506
  schema.type === "number") {
1507
+ counter.count++;
1385
1508
  if (!AdaptiveCardGenerator.isImageUrlProperty(schema, name, parentArrayName)) {
1386
1509
  // string in root: "ddd"
1387
1510
  let text = "result: ${$root}";
@@ -1406,24 +1529,17 @@ class AdaptiveCardGenerator {
1406
1529
  ];
1407
1530
  }
1408
1531
  else {
1409
- if (name) {
1410
- return [
1411
- {
1412
- type: "Image",
1413
- url: `\${${name}}`,
1414
- $when: `\${${name} != null && ${name} != ''}`,
1415
- },
1416
- ];
1417
- }
1418
- else {
1419
- return [
1420
- {
1421
- type: "Image",
1422
- url: "${$data}",
1423
- $when: "${$data != null && $data != ''}",
1424
- },
1425
- ];
1426
- }
1532
+ const url = name ? `\${${name}}` : "${$data}";
1533
+ const condition = name
1534
+ ? `\${${name} != null && ${name} != ''}`
1535
+ : "${$data != null && $data != ''}";
1536
+ return [
1537
+ {
1538
+ type: "Image",
1539
+ url,
1540
+ $when: condition,
1541
+ },
1542
+ ];
1427
1543
  }
1428
1544
  }
1429
1545
  if (schema.oneOf || schema.anyOf || schema.not || schema.allOf) {