@microsoft/m365-spec-parser 0.2.3-alpha.c3772a0e8.0 → 0.2.3-alpha.e623f48bd.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.
@@ -1321,15 +1321,152 @@ class SpecParser {
1321
1321
  }
1322
1322
  }
1323
1323
 
1324
+ // Copyright (c) Microsoft Corporation.
1325
+ class JsonDataGenerator {
1326
+ static generate(schema) {
1327
+ return this.generateMockData(schema);
1328
+ }
1329
+ static generateMockData(schema) {
1330
+ if (this.visitedSchemas.has(schema)) {
1331
+ return null; // Prevent circular reference
1332
+ }
1333
+ this.visitedSchemas.add(schema);
1334
+ let result;
1335
+ if (schema.anyOf) {
1336
+ // Select the first schema in anyOf
1337
+ const selectedSchema = schema.anyOf[0];
1338
+ result = this.generateMockData(selectedSchema);
1339
+ }
1340
+ else if (schema.oneOf) {
1341
+ // Select the first schema in oneOf
1342
+ const selectedSchema = schema.oneOf[0];
1343
+ result = this.generateMockData(selectedSchema);
1344
+ }
1345
+ else if (schema.allOf) {
1346
+ // merge all schemas in allOf
1347
+ result = {};
1348
+ for (const subschema of schema.allOf) {
1349
+ const data = this.generateMockData(subschema);
1350
+ result = Object.assign(Object.assign({}, result), data);
1351
+ }
1352
+ }
1353
+ else {
1354
+ switch (schema.type) {
1355
+ case "string":
1356
+ if (schema.example !== undefined) {
1357
+ result = schema.example;
1358
+ }
1359
+ else if (schema.format) {
1360
+ switch (schema.format) {
1361
+ case "date-time":
1362
+ result = "2024-11-01T05:25:43.593Z";
1363
+ break;
1364
+ case "email":
1365
+ result = "example@example.com";
1366
+ break;
1367
+ case "uuid":
1368
+ result = "123e4567-e89b-12d3-a456-426614174000";
1369
+ break;
1370
+ case "ipv4":
1371
+ result = "192.168.0.1";
1372
+ break;
1373
+ case "ipv6":
1374
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1375
+ break;
1376
+ default:
1377
+ result = "example string";
1378
+ }
1379
+ }
1380
+ else {
1381
+ result = "example string";
1382
+ }
1383
+ break;
1384
+ case "number":
1385
+ if (schema.example !== undefined) {
1386
+ result = schema.example;
1387
+ }
1388
+ else if (schema.format) {
1389
+ switch (schema.format) {
1390
+ case "float":
1391
+ result = 3.14;
1392
+ break;
1393
+ case "double":
1394
+ result = 3.14159;
1395
+ break;
1396
+ default:
1397
+ result = 123;
1398
+ }
1399
+ }
1400
+ else {
1401
+ result = 123;
1402
+ }
1403
+ break;
1404
+ case "integer":
1405
+ if (schema.example !== undefined) {
1406
+ result = schema.example;
1407
+ }
1408
+ else if (schema.format) {
1409
+ switch (schema.format) {
1410
+ case "int32":
1411
+ result = 123456;
1412
+ break;
1413
+ case "int64":
1414
+ result = 123456789;
1415
+ break;
1416
+ default:
1417
+ result = 123;
1418
+ }
1419
+ }
1420
+ else {
1421
+ result = 123;
1422
+ }
1423
+ break;
1424
+ case "boolean":
1425
+ result = schema.example !== undefined ? schema.example : true;
1426
+ break;
1427
+ case "array":
1428
+ result = [this.generateMockData(schema.items)];
1429
+ break;
1430
+ case "object":
1431
+ result = {};
1432
+ if (schema.properties) {
1433
+ for (const key in schema.properties) {
1434
+ result[key] = this.generateMockData(schema.properties[key]);
1435
+ }
1436
+ }
1437
+ break;
1438
+ default:
1439
+ result = schema.example || null;
1440
+ }
1441
+ }
1442
+ this.visitedSchemas.delete(schema);
1443
+ return result;
1444
+ }
1445
+ }
1446
+ JsonDataGenerator.visitedSchemas = new Set();
1447
+
1324
1448
  // Copyright (c) Microsoft Corporation.
1325
1449
  class AdaptiveCardGenerator {
1326
1450
  static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1327
1451
  try {
1328
1452
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1329
1453
  let cardBody = [];
1454
+ let jsonData = {};
1455
+ const warnings = [];
1456
+ const operationId = operationItem.operationId;
1330
1457
  let schema = json.schema;
1331
1458
  let jsonPath = "$";
1332
1459
  if (schema && Object.keys(schema).length > 0) {
1460
+ try {
1461
+ jsonData = JsonDataGenerator.generate(schema);
1462
+ }
1463
+ catch (err) {
1464
+ warnings.push({
1465
+ type: WarningType.GenerateJsonDataFailed,
1466
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1467
+ data: operationId,
1468
+ });
1469
+ }
1333
1470
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1334
1471
  if (jsonPath !== "$") {
1335
1472
  schema = schema.properties[jsonPath];
@@ -1362,7 +1499,7 @@ class AdaptiveCardGenerator {
1362
1499
  version: ConstantString.AdaptiveCardVersion,
1363
1500
  body: cardBody,
1364
1501
  };
1365
- return [fullCard, jsonPath];
1502
+ return [fullCard, jsonPath, jsonData, warnings];
1366
1503
  }
1367
1504
  catch (err) {
1368
1505
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);