@microsoft/m365-spec-parser 0.2.3-alpha.6f30def09.0 → 0.2.3-alpha.86ff45725.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.
@@ -62,6 +62,7 @@ var WarningType;
62
62
  WarningType["ConvertSwaggerToOpenAPI"] = "convert-swagger-to-openapi";
63
63
  WarningType["FuncDescriptionTooLong"] = "function-description-too-long";
64
64
  WarningType["OperationIdContainsSpecialCharacters"] = "operationid-contains-special-characters";
65
+ WarningType["GenerateJsonDataFailed"] = "generate-json-data-failed";
65
66
  WarningType["Unknown"] = "unknown";
66
67
  })(WarningType || (WarningType = {}));
67
68
  /**
@@ -102,6 +103,7 @@ ConstantString.MultipleAuthNotSupported = "Multiple authentication methods are u
102
103
  ConstantString.OperationIdContainsSpecialCharacters = "Operation id '%s' in OpenAPI description document contained special characters and was renamed to '%s'.";
103
104
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
104
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.";
105
107
  ConstantString.WrappedCardVersion = "devPreview";
106
108
  ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
107
109
  ConstantString.WrappedCardResponseLayout = "list";
@@ -1931,6 +1933,130 @@ class ManifestUpdater {
1931
1933
  }
1932
1934
  }
1933
1935
 
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
+
1934
2060
  // Copyright (c) Microsoft Corporation.
1935
2061
  /**
1936
2062
  * A class that parses an OpenAPI specification file and provides methods to validate, list, and generate artifacts.
@@ -2232,9 +2358,24 @@ class SpecParser {
2232
2358
  const safeAdaptiveCardName = operation.operationId.replace(/[^a-zA-Z0-9]/g, "_");
2233
2359
  const fileName = path.join(adaptiveCardFolder, `${safeAdaptiveCardName}.json`);
2234
2360
  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
+ }
2235
2376
  await fs.outputJSON(fileName, wrappedCard, { spaces: 2 });
2236
2377
  const dataFileName = path.join(adaptiveCardFolder, `${safeAdaptiveCardName}.data.json`);
2237
- await fs.outputJSON(dataFileName, {}, { spaces: 2 });
2378
+ await fs.outputJSON(dataFileName, jsonData, { spaces: 2 });
2238
2379
  }
2239
2380
  catch (err) {
2240
2381
  result.allSuccess = false;