@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.
@@ -35,8 +35,6 @@ var ErrorType;
35
35
  ErrorType["PostBodySchemaIsNotJson"] = "post-body-schema-is-not-json";
36
36
  ErrorType["PostBodyContainsRequiredUnsupportedSchema"] = "post-body-contains-required-unsupported-schema";
37
37
  ErrorType["ParamsContainRequiredUnsupportedSchema"] = "params-contain-required-unsupported-schema";
38
- ErrorType["ParamsContainsNestedObject"] = "params-contains-nested-object";
39
- ErrorType["RequestBodyContainsNestedObject"] = "request-body-contains-nested-object";
40
38
  ErrorType["ExceededRequiredParamsLimit"] = "exceeded-required-params-limit";
41
39
  ErrorType["NoParameter"] = "no-parameter";
42
40
  ErrorType["NoAPIInfo"] = "no-api-info";
@@ -56,6 +54,7 @@ var WarningType;
56
54
  WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
57
55
  WarningType["FuncDescriptionTooLong"] = "function-description-too-long";
58
56
  WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
57
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
59
58
  WarningType["Unknown"] = "unknown";
60
59
  })(WarningType || (WarningType = {}));
61
60
  /**
@@ -104,6 +103,7 @@ ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are u
104
103
  ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
105
104
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
106
105
  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.";
106
+ ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
107
107
  ConstantString.WrappedCardVersion = "devPreview";
108
108
  ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
109
109
  ConstantString.WrappedCardResponseLayout = "list";
@@ -189,17 +189,6 @@ ConstantString.PluginManifestSchema = "https://developer.microsoft.com/json-sche
189
189
 
190
190
  // Copyright (c) Microsoft Corporation.
191
191
  class Utils {
192
- static hasNestedObjectInSchema(schema) {
193
- if (this.isObjectSchema(schema)) {
194
- for (const property in schema.properties) {
195
- const nestedSchema = schema.properties[property];
196
- if (this.isObjectSchema(nestedSchema)) {
197
- return true;
198
- }
199
- }
200
- }
201
- return false;
202
- }
203
192
  static isObjectSchema(schema) {
204
193
  return schema.type === "object" || (!schema.type && !!schema.properties);
205
194
  }
@@ -269,27 +258,33 @@ class Utils {
269
258
  let multipleMediaType = false;
270
259
  for (const code of ConstantString.ResponseCodeFor20X) {
271
260
  const responseObject = (_a = operationObject === null || operationObject === void 0 ? void 0 : operationObject.responses) === null || _a === void 0 ? void 0 : _a[code];
272
- if (responseObject === null || responseObject === void 0 ? void 0 : responseObject.content) {
273
- for (const contentType of Object.keys(responseObject.content)) {
274
- // json media type can also be "application/json; charset=utf-8"
275
- if (contentType.indexOf("application/json") >= 0) {
276
- multipleMediaType = false;
277
- json = responseObject.content[contentType];
278
- if (Utils.containMultipleMediaTypes(responseObject)) {
279
- multipleMediaType = true;
280
- if (!allowMultipleMediaType) {
281
- json = {};
282
- }
283
- }
284
- else {
285
- return { json, multipleMediaType };
286
- }
287
- }
288
- }
261
+ if (!responseObject) {
262
+ continue;
263
+ }
264
+ multipleMediaType = Utils.containMultipleMediaTypes(responseObject);
265
+ if (!allowMultipleMediaType && multipleMediaType) {
266
+ json = {};
267
+ continue;
268
+ }
269
+ const mediaObj = Utils.getJsonContentType(responseObject);
270
+ if (Object.keys(mediaObj).length > 0) {
271
+ json = mediaObj;
272
+ return { json, multipleMediaType };
289
273
  }
290
274
  }
291
275
  return { json, multipleMediaType };
292
276
  }
277
+ static getJsonContentType(responseObject) {
278
+ if (responseObject.content) {
279
+ for (const contentType of Object.keys(responseObject.content)) {
280
+ // json media type can also be "application/json; charset=utf-8"
281
+ if (contentType.indexOf("application/json") >= 0) {
282
+ return responseObject.content[contentType];
283
+ }
284
+ }
285
+ }
286
+ return {};
287
+ }
293
288
  static convertPathToCamelCase(path) {
294
289
  const pathSegments = path.split(/[./{]/);
295
290
  const camelCaseSegments = pathSegments.map((segment) => {
@@ -746,11 +741,6 @@ class Validator {
746
741
  }
747
742
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
748
743
  const isCopilot = this.projectType === ProjectType.Copilot;
749
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
750
- paramResult.isValid = false;
751
- paramResult.reason = [ErrorType.RequestBodyContainsNestedObject];
752
- return paramResult;
753
- }
754
744
  if (schema.type === "string" ||
755
745
  schema.type === "integer" ||
756
746
  schema.type === "boolean" ||
@@ -798,11 +788,6 @@ class Validator {
798
788
  for (let i = 0; i < paramObject.length; i++) {
799
789
  const param = paramObject[i];
800
790
  const schema = param.schema;
801
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
802
- paramResult.isValid = false;
803
- paramResult.reason.push(ErrorType.ParamsContainsNestedObject);
804
- continue;
805
- }
806
791
  const isRequiredWithoutDefault = param.required && schema.default === undefined;
807
792
  if (isCopilot) {
808
793
  if (isRequiredWithoutDefault) {
@@ -1275,15 +1260,152 @@ class SpecParser {
1275
1260
  }
1276
1261
  }
1277
1262
 
1263
+ // Copyright (c) Microsoft Corporation.
1264
+ class JsonDataGenerator {
1265
+ static generate(schema) {
1266
+ return this.generateMockData(schema);
1267
+ }
1268
+ static generateMockData(schema) {
1269
+ if (this.visitedSchemas.has(schema)) {
1270
+ return null; // Prevent circular reference
1271
+ }
1272
+ this.visitedSchemas.add(schema);
1273
+ let result;
1274
+ if (schema.anyOf) {
1275
+ // Select the first schema in anyOf
1276
+ const selectedSchema = schema.anyOf[0];
1277
+ result = this.generateMockData(selectedSchema);
1278
+ }
1279
+ else if (schema.oneOf) {
1280
+ // Select the first schema in oneOf
1281
+ const selectedSchema = schema.oneOf[0];
1282
+ result = this.generateMockData(selectedSchema);
1283
+ }
1284
+ else if (schema.allOf) {
1285
+ // merge all schemas in allOf
1286
+ result = {};
1287
+ for (const subschema of schema.allOf) {
1288
+ const data = this.generateMockData(subschema);
1289
+ result = Object.assign(Object.assign({}, result), data);
1290
+ }
1291
+ }
1292
+ else {
1293
+ switch (schema.type) {
1294
+ case "string":
1295
+ if (schema.example !== undefined) {
1296
+ result = schema.example;
1297
+ }
1298
+ else if (schema.format) {
1299
+ switch (schema.format) {
1300
+ case "date-time":
1301
+ result = "2024-11-01T05:25:43.593Z";
1302
+ break;
1303
+ case "email":
1304
+ result = "example@example.com";
1305
+ break;
1306
+ case "uuid":
1307
+ result = "123e4567-e89b-12d3-a456-426614174000";
1308
+ break;
1309
+ case "ipv4":
1310
+ result = "192.168.0.1";
1311
+ break;
1312
+ case "ipv6":
1313
+ result = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
1314
+ break;
1315
+ default:
1316
+ result = "example string";
1317
+ }
1318
+ }
1319
+ else {
1320
+ result = "example string";
1321
+ }
1322
+ break;
1323
+ case "number":
1324
+ if (schema.example !== undefined) {
1325
+ result = schema.example;
1326
+ }
1327
+ else if (schema.format) {
1328
+ switch (schema.format) {
1329
+ case "float":
1330
+ result = 3.14;
1331
+ break;
1332
+ case "double":
1333
+ result = 3.14159;
1334
+ break;
1335
+ default:
1336
+ result = 123;
1337
+ }
1338
+ }
1339
+ else {
1340
+ result = 123;
1341
+ }
1342
+ break;
1343
+ case "integer":
1344
+ if (schema.example !== undefined) {
1345
+ result = schema.example;
1346
+ }
1347
+ else if (schema.format) {
1348
+ switch (schema.format) {
1349
+ case "int32":
1350
+ result = 123456;
1351
+ break;
1352
+ case "int64":
1353
+ result = 123456789;
1354
+ break;
1355
+ default:
1356
+ result = 123;
1357
+ }
1358
+ }
1359
+ else {
1360
+ result = 123;
1361
+ }
1362
+ break;
1363
+ case "boolean":
1364
+ result = schema.example !== undefined ? schema.example : true;
1365
+ break;
1366
+ case "array":
1367
+ result = [this.generateMockData(schema.items)];
1368
+ break;
1369
+ case "object":
1370
+ result = {};
1371
+ if (schema.properties) {
1372
+ for (const key in schema.properties) {
1373
+ result[key] = this.generateMockData(schema.properties[key]);
1374
+ }
1375
+ }
1376
+ break;
1377
+ default:
1378
+ result = schema.example || null;
1379
+ }
1380
+ }
1381
+ this.visitedSchemas.delete(schema);
1382
+ return result;
1383
+ }
1384
+ }
1385
+ JsonDataGenerator.visitedSchemas = new Set();
1386
+
1278
1387
  // Copyright (c) Microsoft Corporation.
1279
1388
  class AdaptiveCardGenerator {
1280
1389
  static generateAdaptiveCard(operationItem, allowMultipleMediaType = false, maxElementCount = Number.MAX_SAFE_INTEGER) {
1281
1390
  try {
1282
1391
  const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);
1283
1392
  let cardBody = [];
1393
+ let jsonData = {};
1394
+ const warnings = [];
1395
+ const operationId = operationItem.operationId;
1284
1396
  let schema = json.schema;
1285
1397
  let jsonPath = "$";
1286
1398
  if (schema && Object.keys(schema).length > 0) {
1399
+ try {
1400
+ jsonData = JsonDataGenerator.generate(schema);
1401
+ }
1402
+ catch (err) {
1403
+ warnings.push({
1404
+ type: WarningType.GenerateJsonDataFailed,
1405
+ content: Utils.format(ConstantString.GenerateJsonDataFailed, operationId, err.toString()),
1406
+ data: operationId,
1407
+ });
1408
+ }
1287
1409
  jsonPath = AdaptiveCardGenerator.getResponseJsonPathFromSchema(schema);
1288
1410
  if (jsonPath !== "$") {
1289
1411
  schema = schema.properties[jsonPath];
@@ -1316,7 +1438,7 @@ class AdaptiveCardGenerator {
1316
1438
  version: ConstantString.AdaptiveCardVersion,
1317
1439
  body: cardBody,
1318
1440
  };
1319
- return [fullCard, jsonPath];
1441
+ return [fullCard, jsonPath, jsonData, warnings];
1320
1442
  }
1321
1443
  catch (err) {
1322
1444
  throw new SpecParserError(err.toString(), ErrorType.GenerateAdaptiveCardFailed);