@microsoft/m365-spec-parser 0.2.3-alpha.09af212fe.0 → 0.2.3-alpha.1e0763330.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.
@@ -83,8 +83,6 @@ exports.ErrorType = void 0;
83
83
  ErrorType["PostBodySchemaIsNotJson"] = "post-body-schema-is-not-json";
84
84
  ErrorType["PostBodyContainsRequiredUnsupportedSchema"] = "post-body-contains-required-unsupported-schema";
85
85
  ErrorType["ParamsContainRequiredUnsupportedSchema"] = "params-contain-required-unsupported-schema";
86
- ErrorType["ParamsContainsNestedObject"] = "params-contains-nested-object";
87
- ErrorType["RequestBodyContainsNestedObject"] = "request-body-contains-nested-object";
88
86
  ErrorType["ExceededRequiredParamsLimit"] = "exceeded-required-params-limit";
89
87
  ErrorType["NoParameter"] = "no-parameter";
90
88
  ErrorType["NoAPIInfo"] = "no-api-info";
@@ -104,6 +102,7 @@ exports.WarningType = void 0;
104
102
  WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
105
103
  WarningType["FuncDescriptionTooLong"] = "function-description-too-long";
106
104
  WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
105
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
107
106
  WarningType["Unknown"] = "unknown";
108
107
  })(exports.WarningType || (exports.WarningType = {}));
109
108
  /**
@@ -144,6 +143,7 @@ ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are u
144
143
  ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
145
144
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
146
145
  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.";
146
+ ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
147
147
  ConstantString.WrappedCardVersion = "devPreview";
148
148
  ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
149
149
  ConstantString.WrappedCardResponseLayout = "list";
@@ -237,17 +237,6 @@ class SpecParserError extends Error {
237
237
 
238
238
  // Copyright (c) Microsoft Corporation.
239
239
  class Utils {
240
- static hasNestedObjectInSchema(schema) {
241
- if (this.isObjectSchema(schema)) {
242
- for (const property in schema.properties) {
243
- const nestedSchema = schema.properties[property];
244
- if (this.isObjectSchema(nestedSchema)) {
245
- return true;
246
- }
247
- }
248
- }
249
- return false;
250
- }
251
240
  static isObjectSchema(schema) {
252
241
  return schema.type === "object" || (!schema.type && !!schema.properties);
253
242
  }
@@ -317,27 +306,33 @@ class Utils {
317
306
  let multipleMediaType = false;
318
307
  for (const code of ConstantString.ResponseCodeFor20X) {
319
308
  const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
320
- if (responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) {
321
- for (const contentType of Object.keys(responseObject.content)) {
322
- // json media type can also be "application/json; charset=utf-8"
323
- if (contentType.indexOf("application/json") >= 0) {
324
- multipleMediaType = false;
325
- json = responseObject.content[contentType];
326
- if (Utils.containMultipleMediaTypes(responseObject)) {
327
- multipleMediaType = true;
328
- if (!allowMultipleMediaType) {
329
- json = {};
330
- }
331
- }
332
- else {
333
- return { json, multipleMediaType };
334
- }
335
- }
336
- }
309
+ if (!responseObject) {
310
+ continue;
311
+ }
312
+ multipleMediaType = Utils.containMultipleMediaTypes(responseObject);
313
+ if (!allowMultipleMediaType && multipleMediaType) {
314
+ json = {};
315
+ continue;
316
+ }
317
+ const mediaObj = Utils.getJsonContentType(responseObject);
318
+ if (Object.keys(mediaObj).length > 0) {
319
+ json = mediaObj;
320
+ return { json, multipleMediaType };
337
321
  }
338
322
  }
339
323
  return { json, multipleMediaType };
340
324
  }
325
+ static getJsonContentType(responseObject) {
326
+ if (responseObject.content) {
327
+ for (const contentType of Object.keys(responseObject.content)) {
328
+ // json media type can also be "application/json; charset=utf-8"
329
+ if (contentType.indexOf("application/json") >= 0) {
330
+ return responseObject.content[contentType];
331
+ }
332
+ }
333
+ }
334
+ return {};
335
+ }
341
336
  static convertPathToCamelCase(path) {
342
337
  const pathSegments = path.split(/[./{]/);
343
338
  const camelCaseSegments = pathSegments.map((segment) => {
@@ -794,11 +789,6 @@ class Validator {
794
789
  }
795
790
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
796
791
  const isCopilot = this.projectType === exports.ProjectType.Copilot;
797
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
798
- paramResult.isValid = false;
799
- paramResult.reason = [exports.ErrorType.RequestBodyContainsNestedObject];
800
- return paramResult;
801
- }
802
792
  if (schema.type === "string" ||
803
793
  schema.type === "integer" ||
804
794
  schema.type === "boolean" ||
@@ -846,11 +836,6 @@ class Validator {
846
836
  for (let i = 0; i < paramObject.length; i++) {
847
837
  const param = paramObject[i];
848
838
  const schema = param.schema;
849
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
850
- paramResult.isValid = false;
851
- paramResult.reason.push(exports.ErrorType.ParamsContainsNestedObject);
852
- continue;
853
- }
854
839
  const isRequiredWithoutDefault = param.required && schema.default === undefined;
855
840
  if (isCopilot) {
856
841
  if (isRequiredWithoutDefault) {
@@ -1325,15 +1310,152 @@ class SpecFilter {
1325
1310
  }
1326
1311
  }
1327
1312
 
1313
+ // Copyright (c) Microsoft Corporation.
1314
+ class JsonDataGenerator {
1315
+ static generate(schema) {
1316
+ return this.generateMockData(schema);
1317
+ }
1318
+ static generateMockData(schema) {
1319
+ if (this.visitedSchemas.has(schema)) {
1320
+ return null; // Prevent circular reference
1321
+ }
1322
+ this.visitedSchemas.add(schema);
1323
+ let result;
1324
+ if (schema.anyOf) {
1325
+ // Select the first schema in anyOf
1326
+ const selectedSchema = schema.anyOf[0];
1327
+ result = this.generateMockData(selectedSchema);
1328
+ }
1329
+ else if (schema.oneOf) {
1330
+ // Select the first schema in oneOf
1331
+ const selectedSchema = schema.oneOf[0];
1332
+ result = this.generateMockData(selectedSchema);
1333
+ }
1334
+ else if (schema.allOf) {
1335
+ // merge all schemas in allOf
1336
+ result = {};
1337
+ for (const subschema of schema.allOf) {
1338
+ const data = this.generateMockData(subschema);
1339
+ result = Object.assign(Object.assign({}, result), data);
1340
+ }
1341
+ }
1342
+ else {
1343
+ switch (schema.type) {
1344
+ case "string":
1345
+ if (schema.example !== undefined) {
1346
+ result = schema.example;
1347
+ }
1348
+ else if (schema.format) {
1349
+ switch (schema.format) {
1350
+ case "date-time":
1351
+ result = "2024-11-01T05:25:43.593Z";
1352
+ break;
1353
+ case "email":
1354
+ result = "example@example.com";
1355
+ break;
1356
+ case "uuid":
1357
+ result = "123e4567-e89b-12d3-a456-426614174000";
1358
+ break;
1359
+ case "ipv4":
1360
+ result = "192.168.0.1";
1361
+ break;
1362
+ case "ipv6":
1363
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1364
+ break;
1365
+ default:
1366
+ result = "example string";
1367
+ }
1368
+ }
1369
+ else {
1370
+ result = "example string";
1371
+ }
1372
+ break;
1373
+ case "number":
1374
+ if (schema.example !== undefined) {
1375
+ result = schema.example;
1376
+ }
1377
+ else if (schema.format) {
1378
+ switch (schema.format) {
1379
+ case "float":
1380
+ result = 3.14;
1381
+ break;
1382
+ case "double":
1383
+ result = 3.14159;
1384
+ break;
1385
+ default:
1386
+ result = 123;
1387
+ }
1388
+ }
1389
+ else {
1390
+ result = 123;
1391
+ }
1392
+ break;
1393
+ case "integer":
1394
+ if (schema.example !== undefined) {
1395
+ result = schema.example;
1396
+ }
1397
+ else if (schema.format) {
1398
+ switch (schema.format) {
1399
+ case "int32":
1400
+ result = 123456;
1401
+ break;
1402
+ case "int64":
1403
+ result = 123456789;
1404
+ break;
1405
+ default:
1406
+ result = 123;
1407
+ }
1408
+ }
1409
+ else {
1410
+ result = 123;
1411
+ }
1412
+ break;
1413
+ case "boolean":
1414
+ result = schema.example !== undefined ? schema.example : true;
1415
+ break;
1416
+ case "array":
1417
+ result = [this.generateMockData(schema.items)];
1418
+ break;
1419
+ case "object":
1420
+ result = {};
1421
+ if (schema.properties) {
1422
+ for (const key in schema.properties) {
1423
+ result[key] = this.generateMockData(schema.properties[key]);
1424
+ }
1425
+ }
1426
+ break;
1427
+ default:
1428
+ result = schema.example || null;
1429
+ }
1430
+ }
1431
+ this.visitedSchemas.delete(schema);
1432
+ return result;
1433
+ }
1434
+ }
1435
+ JsonDataGenerator.visitedSchemas = new Set();
1436
+
1328
1437
  // Copyright (c) Microsoft Corporation.
1329
1438
  class AdaptiveCardGenerator {
1330
1439
  static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1331
1440
  try {
1332
1441
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1333
1442
  let cardBody = [];
1443
+ let jsonData = {};
1444
+ const warnings = [];
1445
+ const operationId = operationItem.operationId;
1334
1446
  let schema = json.schema;
1335
1447
  let jsonPath = "$";
1336
1448
  if (schema && Object.keys(schema).length > 0) {
1449
+ try {
1450
+ jsonData = JsonDataGenerator.generate(schema);
1451
+ }
1452
+ catch (err) {
1453
+ warnings.push({
1454
+ type: exports.WarningType.GenerateJsonDataFailed,
1455
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1456
+ data: operationId,
1457
+ });
1458
+ }
1337
1459
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1338
1460
  if (jsonPath !== "$") {
1339
1461
  schema = schema.properties[jsonPath];
@@ -1366,7 +1488,7 @@ class AdaptiveCardGenerator {
1366
1488
  version: ConstantString.AdaptiveCardVersion,
1367
1489
  body: cardBody,
1368
1490
  };
1369
- return [fullCard, jsonPath];
1491
+ return [fullCard, jsonPath, jsonData, warnings];
1370
1492
  }
1371
1493
  catch (err) {
1372
1494
  throw new SpecParserError(err.toString(), exports.ErrorType.GenerateAdaptiveCardFailed);
@@ -1696,36 +1818,11 @@ class ManifestUpdater {
1696
1818
  for (const method in operations) {
1697
1819
  if (options.allowMethods.includes(method)) {
1698
1820
  const operationItem = operations[method];
1699
- const confirmationBodies = [];
1700
1821
  if (operationItem) {
1701
1822
  const operationId = operationItem.operationId;
1702
1823
  const safeFunctionName = operationId.replace(/[^a-zA-Z0-9]/g, "_");
1703
1824
  const description = (_a = operationItem.description) !== null && _a !== void 0 ? _a : "";
1704
1825
  const summary = operationItem.summary;
1705
- const paramObject = operationItem.parameters;
1706
- const requestBody = operationItem.requestBody;
1707
- if (paramObject) {
1708
- for (let i = 0; i < paramObject.length; i++) {
1709
- const param = paramObject[i];
1710
- const schema = param.schema;
1711
- ManifestUpdater.checkSchema(schema, method, pathUrl);
1712
- confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(param.name));
1713
- }
1714
- }
1715
- if (requestBody) {
1716
- const requestJsonBody = requestBody.content["application/json"];
1717
- const requestBodySchema = requestJsonBody.schema;
1718
- if (Utils.isObjectSchema(requestBodySchema)) {
1719
- for (const property in requestBodySchema.properties) {
1720
- const schema = requestBodySchema.properties[property];
1721
- ManifestUpdater.checkSchema(schema, method, pathUrl);
1722
- confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(property));
1723
- }
1724
- }
1725
- else {
1726
- throw new SpecParserError(Utils.format(ConstantString.UnsupportedSchema, method, pathUrl, JSON.stringify(requestBodySchema)), exports.ErrorType.UpdateManifestFailed);
1727
- }
1728
- }
1729
1826
  let funcDescription = operationItem.description || operationItem.summary || "";
1730
1827
  if (funcDescription.length > ConstantString.FunctionDescriptionMaxLens) {
1731
1828
  warnings.push({
@@ -1759,14 +1856,39 @@ class ManifestUpdater {
1759
1856
  }
1760
1857
  }
1761
1858
  if (options.allowConfirmation && method !== ConstantString.GetMethod) {
1762
- if (!funcObj.capabilities) {
1763
- funcObj.capabilities = {};
1859
+ const paramObject = operationItem.parameters;
1860
+ const requestBody = operationItem.requestBody;
1861
+ const confirmationBodies = [];
1862
+ if (paramObject) {
1863
+ for (let i = 0; i < paramObject.length; i++) {
1864
+ const param = paramObject[i];
1865
+ const schema = param.schema;
1866
+ ManifestUpdater.checkSchema(schema, method, pathUrl);
1867
+ confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(param.name));
1868
+ }
1869
+ }
1870
+ if (requestBody) {
1871
+ const requestJsonBody = Utils.getJsonContentType(requestBody);
1872
+ const requestBodySchema = requestJsonBody.schema;
1873
+ if (Utils.isObjectSchema(requestBodySchema)) {
1874
+ for (const property in requestBodySchema.properties) {
1875
+ const schema = requestBodySchema.properties[property];
1876
+ ManifestUpdater.checkSchema(schema, method, pathUrl);
1877
+ confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(property));
1878
+ }
1879
+ }
1880
+ else {
1881
+ throw new SpecParserError(Utils.format(ConstantString.UnsupportedSchema, method, pathUrl, JSON.stringify(requestBodySchema)), exports.ErrorType.UpdateManifestFailed);
1882
+ }
1764
1883
  }
1765
- funcObj.capabilities.confirmation = {
1766
- type: "AdaptiveCard",
1767
- title: (_b = operationItem.summary) !== null && _b !== void 0 ? _b : description,
1768
- };
1769
1884
  if (confirmationBodies.length > 0) {
1885
+ if (!funcObj.capabilities) {
1886
+ funcObj.capabilities = {};
1887
+ }
1888
+ funcObj.capabilities.confirmation = {
1889
+ type: "AdaptiveCard",
1890
+ title: (_b = operationItem.summary) !== null && _b !== void 0 ? _b : description,
1891
+ };
1770
1892
  funcObj.capabilities.confirmation.body = confirmationBodies.join("\n");
1771
1893
  }
1772
1894
  }
@@ -2289,13 +2411,14 @@ class SpecParser {
2289
2411
  if (this.options.allowMethods.includes(method)) {
2290
2412
  const operation = newSpec.paths[url][method];
2291
2413
  try {
2292
- const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operation);
2414
+ const [card, jsonPath, jsonData, warnings] = AdaptiveCardGenerator.generateAdaptiveCard(operation);
2415
+ result.warnings.push(...warnings);
2293
2416
  const safeAdaptiveCardName = operation.operationId.replace(/[^a-zA-Z0-9]/g, "_");
2294
2417
  const fileName = path__default['default'].join(adaptiveCardFolder, `${safeAdaptiveCardName}.json`);
2295
2418
  const wrappedCard = wrapAdaptiveCard(card, jsonPath);
2296
2419
  yield fs__default['default'].outputJSON(fileName, wrappedCard, { spaces: 2 });
2297
2420
  const dataFileName = path__default['default'].join(adaptiveCardFolder, `${safeAdaptiveCardName}.data.json`);
2298
- yield fs__default['default'].outputJSON(dataFileName, {}, { spaces: 2 });
2421
+ yield fs__default['default'].outputJSON(dataFileName, jsonData, { spaces: 2 });
2299
2422
  }
2300
2423
  catch (err) {
2301
2424
  result.allSuccess = false;