From 4415c950b20e55ab1d7ec65de8ddf462dd4d0d4d Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 2 May 2024 16:43:17 +0300 Subject: [PATCH 1/4] Isolates RPC changes --- src/kiota/KiotaHost.cs | 3 +- src/kiota/Rpc/IServer.cs | 1 + src/kiota/Rpc/Server.cs | 68 +++++++++++++++++++++++++++++++++------- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/kiota/KiotaHost.cs b/src/kiota/KiotaHost.cs index 5923772de7..8dda187bf3 100644 --- a/src/kiota/KiotaHost.cs +++ b/src/kiota/KiotaHost.cs @@ -11,7 +11,8 @@ namespace kiota; public static partial class KiotaHost { - internal static readonly Lazy IsConfigPreviewEnabled = new(() => bool.TryParse(Environment.GetEnvironmentVariable("KIOTA_CONFIG_PREVIEW"), out var isPreviewEnabled) && isPreviewEnabled); + internal const string KiotaPreviewEnvironmentVariable = "KIOTA_CONFIG_PREVIEW"; + internal static readonly Lazy IsConfigPreviewEnabled = new(() => bool.TryParse(Environment.GetEnvironmentVariable(KiotaPreviewEnvironmentVariable), out var isPreviewEnabled) && isPreviewEnabled); public static RootCommand GetRootCommand() { var rootCommand = new RootCommand(); diff --git a/src/kiota/Rpc/IServer.cs b/src/kiota/Rpc/IServer.cs index 52f1409e41..5c467acdbe 100644 --- a/src/kiota/Rpc/IServer.cs +++ b/src/kiota/Rpc/IServer.cs @@ -12,4 +12,5 @@ internal interface IServer Task GetManifestDetailsAsync(string manifestPath, string apiIdentifier, bool clearCache, CancellationToken cancellationToken); Task> GenerateAsync(string openAPIFilePath, string outputPath, GenerationLanguage language, string[] includePatterns, string[] excludePatterns, string clientClassName, string clientNamespaceName, bool usesBackingStore, bool cleanOutput, bool clearCache, bool excludeBackwardCompatible, string[] disabledValidationRules, string[] serializers, string[] deserializers, string[] structuredMimeTypes, bool includeAdditionalData, CancellationToken cancellationToken); Task InfoForDescriptionAsync(string descriptionPath, bool clearCache, CancellationToken cancellationToken); + Task> GeneratePluginAsync(string openAPIFilePath, string outputPath, PluginType[] pluginTypes, string[] includePatterns, string[] excludePatterns, string clientClassName, bool cleanOutput, bool clearCache, string[] disabledValidationRules, CancellationToken cancellationToken); } diff --git a/src/kiota/Rpc/Server.cs b/src/kiota/Rpc/Server.cs index 878aa9b9d0..e717529237 100644 --- a/src/kiota/Rpc/Server.cs +++ b/src/kiota/Rpc/Server.cs @@ -30,6 +30,7 @@ protected KiotaConfiguration Configuration return configObject; }); private static readonly HttpClient httpClient = new(); + private static readonly Lazy IsConfigPreviewEnabled = new(() => bool.TryParse(Environment.GetEnvironmentVariable(KiotaHost.KiotaPreviewEnvironmentVariable), out var isPreviewEnabled) && isPreviewEnabled); public string GetVersion() { return KiotaVersion.Current(); @@ -80,7 +81,7 @@ private static async Task GenerateClientAsync(GenerationConfiguration conf { using var fileLogger = new FileLogLogger(config.OutputPath, LogLevel.Warning); var logger = new AggregateLogger(globalLogger, fileLogger); - return await new KiotaBuilder(logger, config, httpClient).GenerateClientAsync(cancellationToken); + return await new KiotaBuilder(logger, config, httpClient, IsConfigPreviewEnabled.Value).GenerateClientAsync(cancellationToken); } public async Task SearchAsync(string searchTerm, bool clearCache, CancellationToken cancellationToken) { @@ -97,7 +98,7 @@ public async Task GetManifestDetailsAsync(string manifestPath, s var configuration = Configuration.Generation; configuration.ClearCache = clearCache; configuration.ApiManifestPath = $"{manifestPath}#{apiIdentifier}"; - var builder = new KiotaBuilder(logger, configuration, httpClient); + var builder = new KiotaBuilder(logger, configuration, httpClient, IsConfigPreviewEnabled.Value); var manifestResult = await builder.GetApiManifestDetailsAsync(cancellationToken: cancellationToken); return new ManifestResult(logger.LogEntries, manifestResult?.Item1, @@ -109,12 +110,12 @@ public async Task ShowAsync(string descriptionPath, string[] include var configuration = Configuration.Generation; configuration.ClearCache = clearCache; configuration.OpenAPIFilePath = GetAbsolutePath(descriptionPath); - var builder = new KiotaBuilder(logger, configuration, httpClient); + var builder = new KiotaBuilder(logger, configuration, httpClient, IsConfigPreviewEnabled.Value); var fullUrlTreeNode = await builder.GetUrlTreeNodeAsync(cancellationToken); configuration.IncludePatterns = includeFilters.ToHashSet(StringComparer.Ordinal); configuration.ExcludePatterns = excludeFilters.ToHashSet(StringComparer.Ordinal); var filteredTreeNode = configuration.IncludePatterns.Count != 0 || configuration.ExcludePatterns.Count != 0 ? - await new KiotaBuilder(new NoopLogger(), configuration, httpClient).GetUrlTreeNodeAsync(cancellationToken) : // openapi.net seems to have side effects between tree node and the document, we need to drop all references + await new KiotaBuilder(new NoopLogger(), configuration, httpClient, IsConfigPreviewEnabled.Value).GetUrlTreeNodeAsync(cancellationToken) : // openapi.net seems to have side effects between tree node and the document, we need to drop all references default; var filteredPaths = filteredTreeNode is null ? new HashSet() : GetOperationsFromTreeNode(filteredTreeNode).ToHashSet(StringComparer.Ordinal); var rootNode = fullUrlTreeNode != null ? ConvertOpenApiUrlTreeNodeToPathItem(fullUrlTreeNode, filteredPaths) : null; @@ -150,14 +151,20 @@ public async Task> GenerateAsync(string openAPIFilePath, string o configuration.ClearCache = clearCache; configuration.ExcludeBackwardCompatible = excludeBackwardCompatible; configuration.IncludeAdditionalData = includeAdditionalData; - if (disabledValidationRules is not null && disabledValidationRules.Length != 0) + configuration.Operation = ConsumerOperation.Add; //TODO should be updated to edit in the edit scenario + if (disabledValidationRules is { Length: > 0 }) configuration.DisabledValidationRules = disabledValidationRules.ToHashSet(StringComparer.OrdinalIgnoreCase); - if (serializers is not null && serializers.Length != 0) + if (serializers is { Length: > 0 }) configuration.Serializers = serializers.ToHashSet(StringComparer.OrdinalIgnoreCase); - if (deserializers is not null && deserializers.Length != 0) + if (deserializers is { Length: > 0 }) configuration.Deserializers = deserializers.ToHashSet(StringComparer.OrdinalIgnoreCase); - if (structuredMimeTypes is not null && structuredMimeTypes.Length != 0) + if (structuredMimeTypes is { Length: > 0 }) configuration.StructuredMimeTypes = new(structuredMimeTypes); + if (IsConfigPreviewEnabled.Value) + { + configuration.Serializers.Clear(); + configuration.Deserializers.Clear(); + } if (!string.IsNullOrEmpty(clientClassName)) configuration.ClientClassName = clientClassName; if (!string.IsNullOrEmpty(clientNamespaceName)) @@ -176,6 +183,45 @@ public async Task> GenerateAsync(string openAPIFilePath, string o } return logger.LogEntries; } + public async Task> GeneratePluginAsync(string openAPIFilePath, string outputPath, PluginType[] pluginTypes, string[] includePatterns, string[] excludePatterns, string clientClassName, bool cleanOutput, bool clearCache, string[] disabledValidationRules, CancellationToken cancellationToken) + { + var globalLogger = new ForwardedLogger(); + var configuration = Configuration.Generation; + configuration.PluginTypes = pluginTypes.ToHashSet(); + configuration.OpenAPIFilePath = GetAbsolutePath(openAPIFilePath); + configuration.OutputPath = GetAbsolutePath(outputPath); + if (!string.IsNullOrEmpty(clientClassName)) + configuration.ClientClassName = clientClassName; + // configuration.SkipGeneration = skipGeneration; + configuration.CleanOutput = cleanOutput; + configuration.ClearCache = clearCache; + configuration.Operation = ConsumerOperation.Add; //TODO should be updated to edit in the edit scenario + if (disabledValidationRules is { Length: > 0 }) + configuration.DisabledValidationRules = disabledValidationRules.ToHashSet(StringComparer.OrdinalIgnoreCase); + if (pluginTypes is { Length: > 0 }) + configuration.PluginTypes = pluginTypes.ToHashSet(); + if (includePatterns is { Length: > 0 }) + configuration.IncludePatterns = includePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase); + if (excludePatterns is { Length: > 0 }) + configuration.ExcludePatterns = excludePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase); + configuration.OpenAPIFilePath = GetAbsolutePath(configuration.OpenAPIFilePath); + configuration.OutputPath = NormalizeSlashesInPath(GetAbsolutePath(configuration.OutputPath)); + try + { + using var fileLogger = new FileLogLogger(configuration.OutputPath, LogLevel.Warning); + var logger = new AggregateLogger(globalLogger, fileLogger); + var result = await new KiotaBuilder(logger, configuration, httpClient, IsConfigPreviewEnabled.Value).GeneratePluginAsync(cancellationToken); + if (result) + logger.LogInformation("Generation completed successfully"); + else + logger.LogInformation("Generation skipped as --skip-generation was passed"); + } + catch (Exception ex) + { + globalLogger.LogCritical("error adding the client: {exceptionMessage}", ex.Message); + } + return globalLogger.LogEntries; + } public LanguagesInformation Info() { return Configuration.Languages; @@ -191,7 +237,7 @@ private async Task InfoInternalAsync(string descriptionPat var configuration = Configuration.Generation; configuration.ClearCache = clearCache; configuration.OpenAPIFilePath = GetAbsolutePath(descriptionPath); - var builder = new KiotaBuilder(logger, configuration, httpClient); + var builder = new KiotaBuilder(logger, configuration, httpClient, IsConfigPreviewEnabled.Value); var result = await builder.GetLanguagesInformationAsync(cancellationToken); if (result is not null) return result; return Configuration.Languages; @@ -214,13 +260,13 @@ private static PathItem ConvertOpenApiUrlTreeNodeToPathItem(OpenApiUrlTreeNode n .ToArray(); return new PathItem(node.Path, node.DeduplicatedSegment(), children, filteredPaths.Count == 0 || Array.Exists(children, static x => x.isOperation) && children.Where(static x => x.isOperation).All(static x => x.selected)); } - protected static string GetAbsolutePath(string source) + private static string GetAbsolutePath(string source) { if (string.IsNullOrEmpty(source)) return string.Empty; return Path.IsPathRooted(source) || source.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? source : NormalizeSlashesInPath(Path.Combine(Directory.GetCurrentDirectory(), source)); } - protected static string NormalizeSlashesInPath(string path) + private static string NormalizeSlashesInPath(string path) { if (string.IsNullOrEmpty(path)) return path; From 23377f55b945e243de37b12234125319aea464d9 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 2 May 2024 17:24:55 +0300 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dadc093b77..227accb975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed excluding operation. [#4399](https://github.com/microsoft/kiota/issues/4399) - Fided a bug where absolute path would be used for workspace for local descriptions. [#4582](https://github.com/microsoft/kiota/issues/4582) [#4581](https://github.com/microsoft/kiota/issues/4581) - Fixed plugin generation of `ApiManifest` type to not include the `x-ms-kiota-hash` in the generated plugin. [#4561](https://github.com/microsoft/kiota/issues/4561) +- Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag. ## [1.13.0] - 2024-04-04 From fe9abba83ed3d057c4c9a02c79c8682e5af91955 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 3 May 2024 09:29:02 +0300 Subject: [PATCH 3/4] Update changelog and fixed typo --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83cb3fd19b..c766fd0c98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag. + ## [1.14.0] - 2024-05-02 ### Added @@ -30,9 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes request builder disambiguation when child nodes had different prefixes for different paths with same parameters.[#4448](https://github.com/microsoft/kiota/issues/4448) - Do not generate CS8603 warnings when enabling backing store in CSharp generation. - Fixed excluding operation. [#4399](https://github.com/microsoft/kiota/issues/4399) -- Fided a bug where absolute path would be used for workspace for local descriptions. [#4582](https://github.com/microsoft/kiota/issues/4582) [#4581](https://github.com/microsoft/kiota/issues/4581) +- Fixed a bug where absolute path would be used for workspace for local descriptions. [#4582](https://github.com/microsoft/kiota/issues/4582) [#4581](https://github.com/microsoft/kiota/issues/4581) - Fixed plugin generation of `ApiManifest` type to not include the `x-ms-kiota-hash` in the generated plugin. [#4561](https://github.com/microsoft/kiota/issues/4561) -- Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag. ## [1.13.0] - 2024-04-04 From 390bb92d3c24a7d82cbed1c6072a20b6719d445f Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 3 May 2024 09:33:58 +0300 Subject: [PATCH 4/4] Minor cleanup --- src/kiota/Rpc/Server.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/kiota/Rpc/Server.cs b/src/kiota/Rpc/Server.cs index e717529237..871f20bb54 100644 --- a/src/kiota/Rpc/Server.cs +++ b/src/kiota/Rpc/Server.cs @@ -192,7 +192,6 @@ public async Task> GeneratePluginAsync(string openAPIFilePath, st configuration.OutputPath = GetAbsolutePath(outputPath); if (!string.IsNullOrEmpty(clientClassName)) configuration.ClientClassName = clientClassName; - // configuration.SkipGeneration = skipGeneration; configuration.CleanOutput = cleanOutput; configuration.ClearCache = clearCache; configuration.Operation = ConsumerOperation.Add; //TODO should be updated to edit in the edit scenario @@ -218,7 +217,7 @@ public async Task> GeneratePluginAsync(string openAPIFilePath, st } catch (Exception ex) { - globalLogger.LogCritical("error adding the client: {exceptionMessage}", ex.Message); + globalLogger.LogCritical(ex, "error adding the client: {exceptionMessage}", ex.Message); } return globalLogger.LogEntries; }