Skip to content

Commit

Permalink
enable logging to warn users
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Wahome committed Nov 14, 2024
1 parent aebfdc5 commit 0d98e86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public async Task<bool> GeneratePluginAsync(CancellationToken cancellationToken)
throw new InvalidOperationException("The OpenAPI document and the URL tree must be loaded before generating the plugins");
// generate plugin
sw.Start();
var pluginsService = new PluginsGenerationService(openApiDocument, openApiTree, config, Directory.GetCurrentDirectory());
var pluginsService = new PluginsGenerationService(openApiDocument, openApiTree, config, Directory.GetCurrentDirectory(), logger);
await pluginsService.GenerateManifestAsync(cancellationToken).ConfigureAwait(false);
StopLogAndReset(sw, $"step {++stepId} - generate plugin - took");
return stepId;
Expand Down
13 changes: 8 additions & 5 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.OpenApiExtensions;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.ApiManifest;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;
Expand All @@ -23,9 +24,10 @@ public partial class PluginsGenerationService
private readonly OpenApiUrlTreeNode TreeNode;
private readonly GenerationConfiguration Configuration;
private readonly string WorkingDirectory;
private readonly ILogger<KiotaBuilder> Logger;

public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode openApiUrlTreeNode,
GenerationConfiguration configuration, string workingDirectory)
GenerationConfiguration configuration, string workingDirectory, ILogger<KiotaBuilder> logger)
{
ArgumentNullException.ThrowIfNull(document);
ArgumentNullException.ThrowIfNull(openApiUrlTreeNode);
Expand All @@ -35,6 +37,7 @@ public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode ope
TreeNode = openApiUrlTreeNode;
Configuration = configuration;
WorkingDirectory = workingDirectory;
Logger = logger;
}

private static readonly OpenAPIRuntimeComparer _openAPIRuntimeComparer = new();
Expand Down Expand Up @@ -258,7 +261,7 @@ private OpenApiDocument GetDocumentWithTrimmedComponentsAndResponses(OpenApiDocu

private PluginManifestDocument GetManifestDocument(string openApiDocumentPath)
{
var (runtimes, functions, conversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(OAIDocument, Configuration.PluginAuthInformation, TreeNode, openApiDocumentPath);
var (runtimes, functions, conversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(OAIDocument, Configuration.PluginAuthInformation, TreeNode, openApiDocumentPath, Logger);
var descriptionForHuman = OAIDocument.Info?.Description is string d && !string.IsNullOrEmpty(d) ? d : $"Description for {OAIDocument.Info?.Title}";
var manifestInfo = ExtractInfoFromDocument(OAIDocument.Info);
var pluginManifestDocument = new PluginManifestDocument
Expand Down Expand Up @@ -338,7 +341,7 @@ private sealed record OpenApiManifestInfo(
string ContactEmail = DefaultContactEmail);

private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimesFunctionsAndConversationStartersFromTree(OpenApiDocument document, PluginAuthConfiguration? authInformation, OpenApiUrlTreeNode currentNode,
string openApiDocumentPath)
string openApiDocumentPath, ILogger<KiotaBuilder> logger)
{
var runtimes = new List<OpenApiRuntime>();
var functions = new List<Function>();
Expand All @@ -355,7 +358,7 @@ private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimes
}
catch (UnsupportedSecuritySchemeException)
{
// log a warning here that the operation security is not supported
logger.LogWarning("Unsupported security scheme found in operation '{OperationId}'. Using anonymous auth.", operation.OperationId);
auth = new AnonymousAuth();
}

Expand Down Expand Up @@ -387,7 +390,7 @@ private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimes

foreach (var node in currentNode.Children)
{
var (childRuntimes, childFunctions, childConversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(document, authInformation, node.Value, openApiDocumentPath);
var (childRuntimes, childFunctions, childConversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(document, authInformation, node.Value, openApiDocumentPath, logger);
runtimes.AddRange(childRuntimes);
functions.AddRange(childFunctions);
conversationStarters.AddRange(childConversationStarters);
Expand Down
30 changes: 15 additions & 15 deletions tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ namespace Kiota.Builder.Tests.Plugins;
public sealed class PluginsGenerationServiceTests : IDisposable
{
private readonly HttpClient _httpClient = new();

private readonly ILogger<KiotaBuilder> _logger = new Mock<ILogger<KiotaBuilder>>().Object;
[Fact]
public void Defensive()
{
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(null, OpenApiUrlTreeNode.Create(), new(), "foo"));
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(new(), null, new(), "foo"));
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(new(), OpenApiUrlTreeNode.Create(), null, "foo"));
Assert.Throws<ArgumentException>(() => new PluginsGenerationService(new(), OpenApiUrlTreeNode.Create(), new(), string.Empty));
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(null, OpenApiUrlTreeNode.Create(), new(), "foo", _logger));
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(new(), null, new(), "foo", _logger));
Assert.Throws<ArgumentNullException>(() => new PluginsGenerationService(new(), OpenApiUrlTreeNode.Create(), null, "foo", _logger));
Assert.Throws<ArgumentException>(() => new PluginsGenerationService(new(), OpenApiUrlTreeNode.Create(), new(), string.Empty, _logger));
}

public void Dispose()
Expand Down Expand Up @@ -76,7 +76,7 @@ public async Task GeneratesManifestAsync(string inputPluginName, string expected
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, simpleDescriptionContent);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, _logger);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
Expand All @@ -91,7 +91,7 @@ public async Task GeneratesManifestAsync(string inputPluginName, string expected
KiotaBuilder.CleanupOperationIdForPlugins(openApiDocument);
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory, _logger);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, $"{expectedPluginName.ToLower()}-apiplugin.json")));
Expand Down Expand Up @@ -211,7 +211,7 @@ public async Task GeneratesManifestAndCleansUpInputDescriptionAsync()
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, simpleDescriptionContent);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, _logger);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
Expand All @@ -226,7 +226,7 @@ public async Task GeneratesManifestAndCleansUpInputDescriptionAsync()
KiotaBuilder.CleanupOperationIdForPlugins(openApiDocument);
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory, _logger);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Expand Down Expand Up @@ -463,7 +463,7 @@ public async Task GeneratesManifestWithAuthAsync(string securitySchemesComponent
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, apiDescription);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openApiDocumentDs = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var openApiDocumentDs = new OpenApiDocumentDownloadService(_httpClient, _logger);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
Expand All @@ -483,7 +483,7 @@ public async Task GeneratesManifestWithAuthAsync(string securitySchemesComponent
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService =
new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory, _logger);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Expand Down Expand Up @@ -542,7 +542,7 @@ public async Task GeneratesManifestWithMultipleSecuritySchemesAsync()
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, apiDescription);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openApiDocumentDs = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var openApiDocumentDs = new OpenApiDocumentDownloadService(_httpClient, _logger);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
Expand All @@ -561,7 +561,7 @@ public async Task GeneratesManifestWithMultipleSecuritySchemesAsync()
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService =
new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory, _logger);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Expand Down Expand Up @@ -748,7 +748,7 @@ public async Task MergesAllOfRequestBodyAsync(string content, Action<OpenApiDocu
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, apiDescription);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, _logger);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
Expand All @@ -763,7 +763,7 @@ public async Task MergesAllOfRequestBodyAsync(string content, Action<OpenApiDocu
KiotaBuilder.CleanupOperationIdForPlugins(openApiDocument);
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory, _logger);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Expand Down

0 comments on commit 0d98e86

Please sign in to comment.