From e869a1107b0c849c500946d2d46888566c2140a1 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 17 Jun 2024 09:34:16 -0400 Subject: [PATCH] fix: only loads OpenAPI plugins extensions when generating plugins --- CHANGELOG.md | 3 ++- .../Extensions/OpenApiSettingsExtensions.cs | 21 +++++++++++++++++++ .../OpenApiDocumentDownloadService.cs | 8 ++----- ...piAiReasoningInstructionsExtensionTests.cs | 2 +- ...iAiRespondingInstructionsExtensionTests.cs | 2 +- ...penApiDescriptionForModelExtensionTests.cs | 2 +- 6 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 src/Kiota.Builder/Extensions/OpenApiSettingsExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index e4966f58d1..b7f20b2c1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Plugins OpenAPI extensions are only added when generating plugins to reduce the risk of parsing errors. [#4834](https://github.com/microsoft/kiota/issues/4834) - TypeScript imports are now using ES6 imports with the .js extension. - Remove LINQ usage in generated code. -- Ensures descriptions are not empty in sliced OpenApi file when generating a plugin. +- Ensures descriptions are not empty in sliced OpenApi file when generating a plugin. ## [1.15.0] - 2024-06-06 diff --git a/src/Kiota.Builder/Extensions/OpenApiSettingsExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiSettingsExtensions.cs new file mode 100644 index 0000000000..eb097ce999 --- /dev/null +++ b/src/Kiota.Builder/Extensions/OpenApiSettingsExtensions.cs @@ -0,0 +1,21 @@ +using System; +using Kiota.Builder.OpenApiExtensions; +using Microsoft.OpenApi.Readers; + +namespace Kiota.Builder.Extensions; +public static class OpenApiSettingsExtensions +{ + /// + /// Adds the OpenAPI extensions used for plugins generation. + /// + public static void AddPluginsExtensions(this OpenApiReaderSettings settings) + { + ArgumentNullException.ThrowIfNull(settings); + settings.ExtensionParsers.TryAdd(OpenApiLogoExtension.Name, static (i, _) => OpenApiLogoExtension.Parse(i)); + settings.ExtensionParsers.TryAdd(OpenApiDescriptionForModelExtension.Name, static (i, _) => OpenApiDescriptionForModelExtension.Parse(i)); + settings.ExtensionParsers.TryAdd(OpenApiPrivacyPolicyUrlExtension.Name, static (i, _) => OpenApiPrivacyPolicyUrlExtension.Parse(i)); + settings.ExtensionParsers.TryAdd(OpenApiLegalInfoUrlExtension.Name, static (i, _) => OpenApiLegalInfoUrlExtension.Parse(i)); + settings.ExtensionParsers.TryAdd(OpenApiAiReasoningInstructionsExtension.Name, static (i, _) => OpenApiAiReasoningInstructionsExtension.Parse(i)); + settings.ExtensionParsers.TryAdd(OpenApiAiRespondingInstructionsExtension.Name, static (i, _) => OpenApiAiRespondingInstructionsExtension.Parse(i)); + } +} diff --git a/src/Kiota.Builder/OpenApiDocumentDownloadService.cs b/src/Kiota.Builder/OpenApiDocumentDownloadService.cs index b4dad81576..b62c438439 100644 --- a/src/Kiota.Builder/OpenApiDocumentDownloadService.cs +++ b/src/Kiota.Builder/OpenApiDocumentDownloadService.cs @@ -113,13 +113,9 @@ ex is SecurityException || RuleSet = ruleSet, }; settings.AddMicrosoftExtensionParsers(); + if (config.IsPluginConfiguration) + settings.AddPluginsExtensions(); settings.ExtensionParsers.TryAdd(OpenApiKiotaExtension.Name, static (i, _) => OpenApiKiotaExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiDescriptionForModelExtension.Name, static (i, _) => OpenApiDescriptionForModelExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiLogoExtension.Name, static (i, _) => OpenApiLogoExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiPrivacyPolicyUrlExtension.Name, static (i, _) => OpenApiPrivacyPolicyUrlExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiLegalInfoUrlExtension.Name, static (i, _) => OpenApiLegalInfoUrlExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiAiReasoningInstructionsExtension.Name, static (i, _) => OpenApiAiReasoningInstructionsExtension.Parse(i)); - settings.ExtensionParsers.TryAdd(OpenApiAiRespondingInstructionsExtension.Name, static (i, _) => OpenApiAiRespondingInstructionsExtension.Parse(i)); try { var rawUri = config.OpenAPIFilePath.TrimEnd(KiotaBuilder.ForwardSlash); diff --git a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiReasoningInstructionsExtensionTests.cs b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiReasoningInstructionsExtensionTests.cs index 1a4d716b51..8ee5bf5ad4 100644 --- a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiReasoningInstructionsExtensionTests.cs +++ b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiReasoningInstructionsExtensionTests.cs @@ -46,7 +46,7 @@ public async Task ParsesInDocument() await File.WriteAllTextAsync(documentPath, documentContent); var mockLogger = new Mock>(); var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object); - var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory }; + var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory, PluginTypes = [PluginType.APIPlugin] }; var (openApiDocumentStream, _) = await documentDownloadService.LoadStreamAsync(documentPath, generationConfig); var document = await documentDownloadService.GetDocumentFromStreamAsync(openApiDocumentStream, generationConfig); Assert.NotNull(document); diff --git a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiRespondingInstructionsExtensionTests.cs b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiRespondingInstructionsExtensionTests.cs index ba82ff57e7..591a9ab29d 100644 --- a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiRespondingInstructionsExtensionTests.cs +++ b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiAiRespondingInstructionsExtensionTests.cs @@ -46,7 +46,7 @@ public async Task ParsesInDocument() await File.WriteAllTextAsync(documentPath, documentContent); var mockLogger = new Mock>(); var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object); - var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory }; + var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory, PluginTypes = [PluginType.APIPlugin] }; var (openApiDocumentStream, _) = await documentDownloadService.LoadStreamAsync(documentPath, generationConfig); var document = await documentDownloadService.GetDocumentFromStreamAsync(openApiDocumentStream, generationConfig); Assert.NotNull(document); diff --git a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiDescriptionForModelExtensionTests.cs b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiDescriptionForModelExtensionTests.cs index f66cc5e28e..008052b648 100644 --- a/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiDescriptionForModelExtensionTests.cs +++ b/tests/Kiota.Builder.Tests/OpenApiExtensions/OpenApiDescriptionForModelExtensionTests.cs @@ -41,7 +41,7 @@ public async Task ParsesInDocument() await File.WriteAllTextAsync(documentPath, documentContent); var mockLogger = new Mock>(); var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object); - var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory }; + var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory, PluginTypes = [PluginType.APIPlugin] }; var (openApiDocumentStream, _) = await documentDownloadService.LoadStreamAsync(documentPath, generationConfig); var document = await documentDownloadService.GetDocumentFromStreamAsync(openApiDocumentStream, generationConfig); Assert.NotNull(document);