@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.
@@ -1285,15 +1285,152 @@ class SpecFilter {
1285
1285
  }
1286
1286
  }
1287
1287
 
1288
+ // Copyright (c) Microsoft Corporation.
1289
+ class JsonDataGenerator {
1290
+ static generate(schema) {
1291
+ return this.generateMockData(schema);
1292
+ }
1293
+ static generateMockData(schema) {
1294
+ if (this.visitedSchemas.has(schema)) {
1295
+ return null; // Prevent circular reference
1296
+ }
1297
+ this.visitedSchemas.add(schema);
1298
+ let result;
1299
+ if (schema.anyOf) {
1300
+ // Select the first schema in anyOf
1301
+ const selectedSchema = schema.anyOf[0];
1302
+ result = this.generateMockData(selectedSchema);
1303
+ }
1304
+ else if (schema.oneOf) {
1305
+ // Select the first schema in oneOf
1306
+ const selectedSchema = schema.oneOf[0];
1307
+ result = this.generateMockData(selectedSchema);
1308
+ }
1309
+ else if (schema.allOf) {
1310
+ // merge all schemas in allOf
1311
+ result = {};
1312
+ for (const subschema of schema.allOf) {
1313
+ const data = this.generateMockData(subschema);
1314
+ result = Object.assign(Object.assign({}, result), data);
1315
+ }
1316
+ }
1317
+ else {
1318
+ switch (schema.type) {
1319
+ case "string":
1320
+ if (schema.example !== undefined) {
1321
+ result = schema.example;
1322
+ }
1323
+ else if (schema.format) {
1324
+ switch (schema.format) {
1325
+ case "date-time":
1326
+ result = "2024-11-01T05:25:43.593Z";
1327
+ break;
1328
+ case "email":
1329
+ result = "example@example.com";
1330
+ break;
1331
+ case "uuid":
1332
+ result = "123e4567-e89b-12d3-a456-426614174000";
1333
+ break;
1334
+ case "ipv4":
1335
+ result = "192.168.0.1";
1336
+ break;
1337
+ case "ipv6":
1338
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1339
+ break;
1340
+ default:
1341
+ result = "example string";
1342
+ }
1343
+ }
1344
+ else {
1345
+ result = "example string";
1346
+ }
1347
+ break;
1348
+ case "number":
1349
+ if (schema.example !== undefined) {
1350
+ result = schema.example;
1351
+ }
1352
+ else if (schema.format) {
1353
+ switch (schema.format) {
1354
+ case "float":
1355
+ result = 3.14;
1356
+ break;
1357
+ case "double":
1358
+ result = 3.14159;
1359
+ break;
1360
+ default:
1361
+ result = 123;
1362
+ }
1363
+ }
1364
+ else {
1365
+ result = 123;
1366
+ }
1367
+ break;
1368
+ case "integer":
1369
+ if (schema.example !== undefined) {
1370
+ result = schema.example;
1371
+ }
1372
+ else if (schema.format) {
1373
+ switch (schema.format) {
1374
+ case "int32":
1375
+ result = 123456;
1376
+ break;
1377
+ case "int64":
1378
+ result = 123456789;
1379
+ break;
1380
+ default:
1381
+ result = 123;
1382
+ }
1383
+ }
1384
+ else {
1385
+ result = 123;
1386
+ }
1387
+ break;
1388
+ case "boolean":
1389
+ result = schema.example !== undefined ? schema.example : true;
1390
+ break;
1391
+ case "array":
1392
+ result = [this.generateMockData(schema.items)];
1393
+ break;
1394
+ case "object":
1395
+ result = {};
1396
+ if (schema.properties) {
1397
+ for (const key in schema.properties) {
1398
+ result[key] = this.generateMockData(schema.properties[key]);
1399
+ }
1400
+ }
1401
+ break;
1402
+ default:
1403
+ result = schema.example || null;
1404
+ }
1405
+ }
1406
+ this.visitedSchemas.delete(schema);
1407
+ return result;
1408
+ }
1409
+ }
1410
+ JsonDataGenerator.visitedSchemas = new Set();
1411
+
1288
1412
  // Copyright (c) Microsoft Corporation.
1289
1413
  class AdaptiveCardGenerator {
1290
1414
  static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1291
1415
  try {
1292
1416
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1293
1417
  let cardBody = [];
1418
+ let jsonData = {};
1419
+ const warnings = [];
1420
+ const operationId = operationItem.operationId;
1294
1421
  let schema = json.schema;
1295
1422
  let jsonPath = "$";
1296
1423
  if (schema && Object.keys(schema).length > 0) {
1424
+ try {
1425
+ jsonData = JsonDataGenerator.generate(schema);
1426
+ }
1427
+ catch (err) {
1428
+ warnings.push({
1429
+ type: WarningType.GenerateJsonDataFailed,
1430
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1431
+ data: operationId,
1432
+ });
1433
+ }
1297
1434
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1298
1435
  if (jsonPath !== "$") {
1299
1436
  schema = schema.properties[jsonPath];
@@ -1326,7 +1463,7 @@ class AdaptiveCardGenerator {
1326
1463
  version: ConstantString.AdaptiveCardVersion,
1327
1464
  body: cardBody,
1328
1465
  };
1329
- return [fullCard, jsonPath];
1466
+ return [fullCard, jsonPath, jsonData, warnings];
1330
1467
  }
1331
1468
  catch (err) {
1332
1469
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);
@@ -1933,130 +2070,6 @@ class ManifestUpdater {
1933
2070
  }
1934
2071
  }
1935
2072
 
1936
- // Copyright (c) Microsoft Corporation.
1937
- class JsonDataGenerator {
1938
- static generate(schema) {
1939
- return this.generateMockData(schema);
1940
- }
1941
- static generateMockData(schema) {
1942
- if (this.visitedSchemas.has(schema)) {
1943
- return null; // Prevent circular reference
1944
- }
1945
- this.visitedSchemas.add(schema);
1946
- let result;
1947
- if (schema.anyOf) {
1948
- // Select the first schema in anyOf
1949
- const selectedSchema = schema.anyOf[0];
1950
- result = this.generateMockData(selectedSchema);
1951
- }
1952
- else if (schema.oneOf) {
1953
- // Select the first schema in oneOf
1954
- const selectedSchema = schema.oneOf[0];
1955
- result = this.generateMockData(selectedSchema);
1956
- }
1957
- else if (schema.allOf) {
1958
- // merge all schemas in allOf
1959
- result = {};
1960
- for (const subschema of schema.allOf) {
1961
- const data = this.generateMockData(subschema);
1962
- result = Object.assign(Object.assign({}, result), data);
1963
- }
1964
- }
1965
- else {
1966
- switch (schema.type) {
1967
- case "string":
1968
- if (schema.example !== undefined) {
1969
- result = schema.example;
1970
- }
1971
- else if (schema.format) {
1972
- switch (schema.format) {
1973
- case "date-time":
1974
- result = "2024-11-01T05:25:43.593Z";
1975
- break;
1976
- case "email":
1977
- result = "example@example.com";
1978
- break;
1979
- case "uuid":
1980
- result = "123e4567-e89b-12d3-a456-426614174000";
1981
- break;
1982
- case "ipv4":
1983
- result = "192.168.0.1";
1984
- break;
1985
- case "ipv6":
1986
- result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1987
- break;
1988
- default:
1989
- result = "example string";
1990
- }
1991
- }
1992
- else {
1993
- result = "example string";
1994
- }
1995
- break;
1996
- case "number":
1997
- if (schema.example !== undefined) {
1998
- result = schema.example;
1999
- }
2000
- else if (schema.format) {
2001
- switch (schema.format) {
2002
- case "float":
2003
- result = 3.14;
2004
- break;
2005
- case "double":
2006
- result = 3.14159;
2007
- break;
2008
- default:
2009
- result = 123;
2010
- }
2011
- }
2012
- else {
2013
- result = 123;
2014
- }
2015
- break;
2016
- case "integer":
2017
- if (schema.example !== undefined) {
2018
- result = schema.example;
2019
- }
2020
- else if (schema.format) {
2021
- switch (schema.format) {
2022
- case "int32":
2023
- result = 123456;
2024
- break;
2025
- case "int64":
2026
- result = 123456789;
2027
- break;
2028
- default:
2029
- result = 123;
2030
- }
2031
- }
2032
- else {
2033
- result = 123;
2034
- }
2035
- break;
2036
- case "boolean":
2037
- result = schema.example !== undefined ? schema.example : true;
2038
- break;
2039
- case "array":
2040
- result = [this.generateMockData(schema.items)];
2041
- break;
2042
- case "object":
2043
- result = {};
2044
- if (schema.properties) {
2045
- for (const key in schema.properties) {
2046
- result[key] = this.generateMockData(schema.properties[key]);
2047
- }
2048
- }
2049
- break;
2050
- default:
2051
- result = schema.example || null;
2052
- }
2053
- }
2054
- this.visitedSchemas.delete(schema);
2055
- return result;
2056
- }
2057
- }
2058
- JsonDataGenerator.visitedSchemas = new Set();
2059
-
2060
2073
  // Copyright (c) Microsoft Corporation.
2061
2074
  /**
2062
2075
  * A class that parses an OpenAPI specification file and provides methods to validate, list, and generate artifacts.
@@ -2354,25 +2367,11 @@ class SpecParser {
2354
2367
  if (this.options.allowMethods.includes(method)) {
2355
2368
  const operation = newSpec.paths[url][method];
2356
2369
  try {
2357
- const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operation);
2370
+ const [card, jsonPath, jsonData, warnings] = AdaptiveCardGenerator.generateAdaptiveCard(operation);
2371
+ result.warnings.push(...warnings);
2358
2372
  const safeAdaptiveCardName = operation.operationId.replace(/[^a-zA-Z0-9]/g, "_");
2359
2373
  const fileName = path.join(adaptiveCardFolder, `${safeAdaptiveCardName}.json`);
2360
2374
  const wrappedCard = wrapAdaptiveCard(card, jsonPath);
2361
- const { json } = Utils.getResponseJson(operation, false);
2362
- const schema = json.schema;
2363
- let jsonData = {};
2364
- if (schema && Object.keys(schema).length > 0) {
2365
- try {
2366
- jsonData = JsonDataGenerator.generate(schema);
2367
- }
2368
- catch (err) {
2369
- result.warnings.push({
2370
- type: WarningType.GenerateJsonDataFailed,
2371
- content: Utils.format(ConstantString.GenerateJsonDataFailed, operation.operationId, err.toString()),
2372
- data: operation.operationId,
2373
- });
2374
- }
2375
- }
2376
2375
  await fs.outputJSON(fileName, wrappedCard, { spaces: 2 });
2377
2376
  const dataFileName = path.join(adaptiveCardFolder, `${safeAdaptiveCardName}.data.json`);
2378
2377
  await fs.outputJSON(dataFileName, jsonData, { spaces: 2 });