@microsoft/m365-spec-parser 0.2.2-rc.0 → 0.2.2

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.
@@ -3,6 +3,7 @@ import converter from 'swagger2openapi';
3
3
  import jsyaml from 'js-yaml';
4
4
  import fs from 'fs-extra';
5
5
  import path from 'path';
6
+ import { ManifestUtil } from '@microsoft/teams-manifest';
6
7
  import { createHash } from 'crypto';
7
8
 
8
9
  // Copyright (c) Microsoft Corporation.
@@ -193,19 +194,16 @@ class SpecParserError extends Error {
193
194
  // Copyright (c) Microsoft Corporation.
194
195
  class Utils {
195
196
  static hasNestedObjectInSchema(schema) {
196
- if (this.isObjectSchema(schema)) {
197
+ if (schema.type === "object") {
197
198
  for (const property in schema.properties) {
198
199
  const nestedSchema = schema.properties[property];
199
- if (this.isObjectSchema(nestedSchema)) {
200
+ if (nestedSchema.type === "object") {
200
201
  return true;
201
202
  }
202
203
  }
203
204
  }
204
205
  return false;
205
206
  }
206
- static isObjectSchema(schema) {
207
- return schema.type === "object" || (!schema.type && !!schema.properties);
208
- }
209
207
  static containMultipleMediaTypes(bodyObject) {
210
208
  return Object.keys((bodyObject === null || bodyObject === void 0 ? void 0 : bodyObject.content) || {}).length > 1;
211
209
  }
@@ -434,7 +432,7 @@ class Utils {
434
432
  optionalParams.push(parameter);
435
433
  }
436
434
  }
437
- else if (Utils.isObjectSchema(schema)) {
435
+ else if (schema.type === "object") {
438
436
  const { properties } = schema;
439
437
  for (const property in properties) {
440
438
  let isRequired = false;
@@ -772,7 +770,7 @@ class Validator {
772
770
  }
773
771
  const isRequiredWithoutDefault = isRequired && schema.default === undefined;
774
772
  const isCopilot = this.projectType === ProjectType.Copilot;
775
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
773
+ if (isCopilot && this.hasNestedObjectInSchema(schema)) {
776
774
  paramResult.isValid = false;
777
775
  paramResult.reason = [ErrorType.RequestBodyContainsNestedObject];
778
776
  return paramResult;
@@ -788,7 +786,7 @@ class Validator {
788
786
  paramResult.optionalNum = paramResult.optionalNum + 1;
789
787
  }
790
788
  }
791
- else if (Utils.isObjectSchema(schema)) {
789
+ else if (schema.type === "object") {
792
790
  const { properties } = schema;
793
791
  for (const property in properties) {
794
792
  let isRequired = false;
@@ -824,7 +822,7 @@ class Validator {
824
822
  for (let i = 0; i < paramObject.length; i++) {
825
823
  const param = paramObject[i];
826
824
  const schema = param.schema;
827
- if (isCopilot && Utils.hasNestedObjectInSchema(schema)) {
825
+ if (isCopilot && this.hasNestedObjectInSchema(schema)) {
828
826
  paramResult.isValid = false;
829
827
  paramResult.reason.push(ErrorType.ParamsContainsNestedObject);
830
828
  continue;
@@ -867,6 +865,17 @@ class Validator {
867
865
  }
868
866
  return paramResult;
869
867
  }
868
+ hasNestedObjectInSchema(schema) {
869
+ if (schema.type === "object") {
870
+ for (const property in schema.properties) {
871
+ const nestedSchema = schema.properties[property];
872
+ if (nestedSchema.type === "object") {
873
+ return true;
874
+ }
875
+ }
876
+ }
877
+ return false;
878
+ }
870
879
  }
871
880
 
872
881
  // Copyright (c) Microsoft Corporation.
@@ -925,7 +934,7 @@ class CopilotValidator extends Validator {
925
934
  const requestJsonBody = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content["application/json"];
926
935
  if (requestJsonBody) {
927
936
  const requestBodySchema = requestJsonBody.schema;
928
- if (!Utils.isObjectSchema(requestBodySchema)) {
937
+ if (requestBodySchema.type !== "object") {
929
938
  result.reason.push(ErrorType.PostBodySchemaIsNotJson);
930
939
  }
931
940
  const requestBodyParamResult = this.checkPostBodySchema(requestBodySchema, requestBody.required);
@@ -1372,7 +1381,7 @@ class AdaptiveCardGenerator {
1372
1381
  return [template];
1373
1382
  }
1374
1383
  // some schema may not contain type but contain properties
1375
- if (Utils.isObjectSchema(schema)) {
1384
+ if (schema.type === "object" || (!schema.type && schema.properties)) {
1376
1385
  const { properties } = schema;
1377
1386
  const result = [];
1378
1387
  for (const property in properties) {
@@ -1440,7 +1449,7 @@ class AdaptiveCardGenerator {
1440
1449
  }
1441
1450
  // Find the first array property in the response schema object with the well-known name
1442
1451
  static getResponseJsonPathFromSchema(schema) {
1443
- if (Utils.isObjectSchema(schema)) {
1452
+ if (schema.type === "object" || (!schema.type && schema.properties)) {
1444
1453
  const { properties } = schema;
1445
1454
  for (const property in properties) {
1446
1455
  const schema = properties[property];
@@ -1595,16 +1604,30 @@ class ManifestUpdater {
1595
1604
  static async updateManifestWithAiPlugin(manifestPath, outputSpecPath, apiPluginFilePath, spec, options, authInfo, existingPluginManifestInfo) {
1596
1605
  const manifest = await fs.readJSON(manifestPath);
1597
1606
  const apiPluginRelativePath = ManifestUpdater.getRelativePath(manifestPath, apiPluginFilePath);
1598
- manifest.copilotExtensions = manifest.copilotExtensions || {};
1599
- // Insert plugins in manifest.json if it is plugin for Copilot.
1600
- if (!options.isGptPlugin) {
1601
- manifest.copilotExtensions.plugins = [
1602
- {
1603
- file: apiPluginRelativePath,
1604
- id: ConstantString.DefaultPluginId,
1605
- },
1606
- ];
1607
- ManifestUpdater.updateManifestDescription(manifest, spec);
1607
+ const useCopilotExtensionsInSchema = await ManifestUtil.useCopilotExtensionsInSchema(manifest);
1608
+ if (manifest.copilotExtensions || useCopilotExtensionsInSchema) {
1609
+ manifest.copilotExtensions = manifest.copilotExtensions || {};
1610
+ if (!options.isGptPlugin) {
1611
+ manifest.copilotExtensions.plugins = [
1612
+ {
1613
+ file: apiPluginRelativePath,
1614
+ id: ConstantString.DefaultPluginId,
1615
+ },
1616
+ ];
1617
+ ManifestUpdater.updateManifestDescription(manifest, spec);
1618
+ }
1619
+ }
1620
+ else {
1621
+ manifest.copilotAgents = manifest.copilotAgents || {};
1622
+ if (!options.isGptPlugin) {
1623
+ manifest.copilotAgents.plugins = [
1624
+ {
1625
+ file: apiPluginRelativePath,
1626
+ id: ConstantString.DefaultPluginId,
1627
+ },
1628
+ ];
1629
+ ManifestUpdater.updateManifestDescription(manifest, spec);
1630
+ }
1608
1631
  }
1609
1632
  const appName = this.removeEnvs(manifest.name.short);
1610
1633
  const specRelativePath = ManifestUpdater.getRelativePath(manifestPath, outputSpecPath);
@@ -1678,7 +1701,7 @@ class ManifestUpdater {
1678
1701
  if (requestBody) {
1679
1702
  const requestJsonBody = requestBody.content["application/json"];
1680
1703
  const requestBodySchema = requestJsonBody.schema;
1681
- if (Utils.isObjectSchema(requestBodySchema)) {
1704
+ if (requestBodySchema.type === "object") {
1682
1705
  for (const property in requestBodySchema.properties) {
1683
1706
  const schema = requestBodySchema.properties[property];
1684
1707
  ManifestUpdater.checkSchema(schema, method, pathUrl);