@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.
@@ -65,8 +65,6 @@ var ErrorType;
65
65
  ErrorType["PostBodySchemaIsNotJson"] = "post-body-schema-is-not-json";
66
66
  ErrorType["PostBodyContainsRequiredUnsupportedSchema"] = "post-body-contains-required-unsupported-schema";
67
67
  ErrorType["ParamsContainRequiredUnsupportedSchema"] = "params-contain-required-unsupported-schema";
68
- ErrorType["ParamsContainsNestedObject"] = "params-contains-nested-object";
69
- ErrorType["RequestBodyContainsNestedObject"] = "request-body-contains-nested-object";
70
68
  ErrorType["ExceededRequiredParamsLimit"] = "exceeded-required-params-limit";
71
69
  ErrorType["NoParameter"] = "no-parameter";
72
70
  ErrorType["NoAPIInfo"] = "no-api-info";
@@ -86,6 +84,7 @@ var WarningType;
86
84
  WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
87
85
  WarningType["FuncDescriptionTooLong"] = "function-description-too-long";
88
86
  WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
87
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
89
88
  WarningType["Unknown"] = "unknown";
90
89
  })(WarningType || (WarningType = {}));
91
90
  /**
@@ -134,6 +133,7 @@ ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are u
134
133
  ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
135
134
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
136
135
  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.";
136
+ ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
137
137
  ConstantString.WrappedCardVersion = "devPreview";
138
138
  ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
139
139
  ConstantString.WrappedCardResponseLayout = "list";
@@ -219,17 +219,6 @@ ConstantString.PluginManifestSchema = "https://developer.microsoft.com/json-sche
219
219
 
220
220
  // Copyright (c) Microsoft Corporation.
221
221
  class Utils {
222
- static hasNestedObjectInSchema(schema) {
223
- if (this.isObjectSchema(schema)) {
224
- for (const property in schema.properties) {
225
- const nestedSchema = schema.properties[property];
226
- if (this.isObjectSchema(nestedSchema)) {
227
- return true;
228
- }
229
- }
230
- }
231
- return false;
232
- }
233
222
  static isObjectSchema(schema) {
234
223
  return schema.type === "object" || (!schema.type && !!schema.properties);
235
224
  }
@@ -299,27 +288,33 @@ class Utils {
299
288
  let multipleMediaType = false;
300
289
  for (const code of ConstantString.ResponseCodeFor20X) {
301
290
  const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
302
- if (responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) {
303
- for (const contentType of Object.keys(responseObject.content)) {
304
- // json media type can also be "application/json; charset=utf-8"
305
- if (contentType.indexOf("application/json") >= 0) {
306
- multipleMediaType = false;
307
- json = responseObject.content[contentType];
308
- if (Utils.containMultipleMediaTypes(responseObject)) {
309
- multipleMediaType = true;
310
- if (!allowMultipleMediaType) {
311
- json = {};
312
- }
313
- }
314
- else {
315
- return { json, multipleMediaType };
316
- }
317
- }
318
- }
291
+ if (!responseObject) {
292
+ continue;
293
+ }
294
+ multipleMediaType = Utils.containMultipleMediaTypes(responseObject);
295
+ if (!allowMultipleMediaType && multipleMediaType) {
296
+ json = {};
297
+ continue;
298
+ }
299
+ const mediaObj = Utils.getJsonContentType(responseObject);
300
+ if (Object.keys(mediaObj).length > 0) {
301
+ json = mediaObj;
302
+ return { json, multipleMediaType };
319
303
  }
320
304
  }
321
305
  return { json, multipleMediaType };
322
306
  }
307
+ static getJsonContentType(responseObject) {
308
+ if (responseObject.content) {
309
+ for (const contentType of Object.keys(responseObject.content)) {
310
+ // json media type can also be "application/json; charset=utf-8"
311
+ if (contentType.indexOf("application/json") >= 0) {
312
+ return responseObject.content[contentType];
313
+ }
314
+ }
315
+ }
316
+ return {};
317
+ }
323
318
  static convertPathToCamelCase(path) {
324
319
  const pathSegments = path.split(/[./{]/);
325
320
  const camelCaseSegments = pathSegments.map((segment) => {
@@ -776,11 +771,6 @@ class Validator {
776
771
  }
777
772
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
778
773
  const isCopilot = this.projectType === ProjectType.Copilot;
779
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
780
- paramResult.isValid = false;
781
- paramResult.reason = [ErrorType.RequestBodyContainsNestedObject];
782
- return paramResult;
783
- }
784
774
  if (schema.type === "string" ||
785
775
  schema.type === "integer" ||
786
776
  schema.type === "boolean" ||
@@ -828,11 +818,6 @@ class Validator {
828
818
  for (let i = 0; i < paramObject.length; i++) {
829
819
  const param = paramObject[i];
830
820
  const schema = param.schema;
831
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
832
- paramResult.isValid = false;
833
- paramResult.reason.push(ErrorType.ParamsContainsNestedObject);
834
- continue;
835
- }
836
821
  const isRequiredWithoutDefault = param.required && schema.default === undefined;
837
822
  if (isCopilot) {
838
823
  if (isRequiredWithoutDefault) {
@@ -1319,15 +1304,152 @@ class SpecParser {
1319
1304
  }
1320
1305
  }
1321
1306
 
1307
+ // Copyright (c) Microsoft Corporation.
1308
+ class JsonDataGenerator {
1309
+ static generate(schema) {
1310
+ return this.generateMockData(schema);
1311
+ }
1312
+ static generateMockData(schema) {
1313
+ if (this.visitedSchemas.has(schema)) {
1314
+ return null; // Prevent circular reference
1315
+ }
1316
+ this.visitedSchemas.add(schema);
1317
+ let result;
1318
+ if (schema.anyOf) {
1319
+ // Select the first schema in anyOf
1320
+ const selectedSchema = schema.anyOf[0];
1321
+ result = this.generateMockData(selectedSchema);
1322
+ }
1323
+ else if (schema.oneOf) {
1324
+ // Select the first schema in oneOf
1325
+ const selectedSchema = schema.oneOf[0];
1326
+ result = this.generateMockData(selectedSchema);
1327
+ }
1328
+ else if (schema.allOf) {
1329
+ // merge all schemas in allOf
1330
+ result = {};
1331
+ for (const subschema of schema.allOf) {
1332
+ const data = this.generateMockData(subschema);
1333
+ result = Object.assign(Object.assign({}, result), data);
1334
+ }
1335
+ }
1336
+ else {
1337
+ switch (schema.type) {
1338
+ case "string":
1339
+ if (schema.example !== undefined) {
1340
+ result = schema.example;
1341
+ }
1342
+ else if (schema.format) {
1343
+ switch (schema.format) {
1344
+ case "date-time":
1345
+ result = "2024-11-01T05:25:43.593Z";
1346
+ break;
1347
+ case "email":
1348
+ result = "example@example.com";
1349
+ break;
1350
+ case "uuid":
1351
+ result = "123e4567-e89b-12d3-a456-426614174000";
1352
+ break;
1353
+ case "ipv4":
1354
+ result = "192.168.0.1";
1355
+ break;
1356
+ case "ipv6":
1357
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1358
+ break;
1359
+ default:
1360
+ result = "example string";
1361
+ }
1362
+ }
1363
+ else {
1364
+ result = "example string";
1365
+ }
1366
+ break;
1367
+ case "number":
1368
+ if (schema.example !== undefined) {
1369
+ result = schema.example;
1370
+ }
1371
+ else if (schema.format) {
1372
+ switch (schema.format) {
1373
+ case "float":
1374
+ result = 3.14;
1375
+ break;
1376
+ case "double":
1377
+ result = 3.14159;
1378
+ break;
1379
+ default:
1380
+ result = 123;
1381
+ }
1382
+ }
1383
+ else {
1384
+ result = 123;
1385
+ }
1386
+ break;
1387
+ case "integer":
1388
+ if (schema.example !== undefined) {
1389
+ result = schema.example;
1390
+ }
1391
+ else if (schema.format) {
1392
+ switch (schema.format) {
1393
+ case "int32":
1394
+ result = 123456;
1395
+ break;
1396
+ case "int64":
1397
+ result = 123456789;
1398
+ break;
1399
+ default:
1400
+ result = 123;
1401
+ }
1402
+ }
1403
+ else {
1404
+ result = 123;
1405
+ }
1406
+ break;
1407
+ case "boolean":
1408
+ result = schema.example !== undefined ? schema.example : true;
1409
+ break;
1410
+ case "array":
1411
+ result = [this.generateMockData(schema.items)];
1412
+ break;
1413
+ case "object":
1414
+ result = {};
1415
+ if (schema.properties) {
1416
+ for (const key in schema.properties) {
1417
+ result[key] = this.generateMockData(schema.properties[key]);
1418
+ }
1419
+ }
1420
+ break;
1421
+ default:
1422
+ result = schema.example || null;
1423
+ }
1424
+ }
1425
+ this.visitedSchemas.delete(schema);
1426
+ return result;
1427
+ }
1428
+ }
1429
+ JsonDataGenerator.visitedSchemas = new Set();
1430
+
1322
1431
  // Copyright (c) Microsoft Corporation.
1323
1432
  class AdaptiveCardGenerator {
1324
1433
  static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1325
1434
  try {
1326
1435
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1327
1436
  let cardBody = [];
1437
+ let jsonData = {};
1438
+ const warnings = [];
1439
+ const operationId = operationItem.operationId;
1328
1440
  let schema = json.schema;
1329
1441
  let jsonPath = "$";
1330
1442
  if (schema && Object.keys(schema).length > 0) {
1443
+ try {
1444
+ jsonData = JsonDataGenerator.generate(schema);
1445
+ }
1446
+ catch (err) {
1447
+ warnings.push({
1448
+ type: WarningType.GenerateJsonDataFailed,
1449
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1450
+ data: operationId,
1451
+ });
1452
+ }
1331
1453
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1332
1454
  if (jsonPath !== "$") {
1333
1455
  schema = schema.properties[jsonPath];
@@ -1360,7 +1482,7 @@ class AdaptiveCardGenerator {
1360
1482
  version: ConstantString.AdaptiveCardVersion,
1361
1483
  body: cardBody,
1362
1484
  };
1363
- return [fullCard, jsonPath];
1485
+ return [fullCard, jsonPath, jsonData, warnings];
1364
1486
  }
1365
1487
  catch (err) {
1366
1488
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);