Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/validation rules openapi #5691

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Validate openapi before generation
samwelkanda committed Oct 29, 2024
commit 747e5773acbe40b0e61fe580b8f9baa77484ea25
35 changes: 17 additions & 18 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
@@ -44,27 +44,11 @@ public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode ope
private const string DescriptionPathSuffix = "openapi.yml";
public async Task GenerateManifestAsync(CancellationToken cancellationToken = default)
{

//1. validate openapi file
var ruleSet = new Microsoft.OpenApi.Validations.ValidationRuleSet
{
OpenApiServerUrlRule.ServerUrlMustBeHttps,
OpenApiCombinedAuthFlowRule.PathsCanOnlyHaveOneSecuritySchemePerOperation(OAIDocument.SecurityRequirements),
OpenApiRequestBodySchemaRule.RequestBodySchemaObjectsMustNeverBeNested,
OpenApiApiKeyBearerRule.ApiKeyNotSupportedOnlyBearerPlusHttp(OAIDocument.SecurityRequirements)
};
var errors = OAIDocument.Validate(ruleSet)?.ToArray();
if (errors != null && errors.Length != 0)
{
var message = string.Join(Environment.NewLine, errors.Select(e => $"{e.Pointer}: {e.Message}"));
throw new InvalidOperationException($"OpenApi document validation failed with errors: {message}");
}

// 2. cleanup any namings to be used later on.
// 1. cleanup any namings to be used later on.
Configuration.ClientClassName =
PluginNameCleanupRegex().Replace(Configuration.ClientClassName, string.Empty); //drop any special characters

// 3. write the OpenApi description
// 2. write the OpenApi description
var descriptionRelativePath = $"{Configuration.ClientClassName.ToLowerInvariant()}-{DescriptionPathSuffix}";
var descriptionFullPath = Path.Combine(Configuration.OutputPath, descriptionRelativePath);
var directory = Path.GetDirectoryName(descriptionFullPath);
@@ -80,6 +64,21 @@ public async Task GenerateManifestAsync(CancellationToken cancellationToken = de
trimmedPluginDocument.SerializeAsV3(descriptionWriter);
descriptionWriter.Flush();

//3. validate openapi file
var ruleSet = new Microsoft.OpenApi.Validations.ValidationRuleSet
{
OpenApiServerUrlRule.ServerUrlMustBeHttps,
OpenApiCombinedAuthFlowRule.PathsCanOnlyHaveOneSecuritySchemePerOperation(OAIDocument.SecurityRequirements),
OpenApiRequestBodySchemaRule.RequestBodySchemaObjectsMustNeverBeNested,
OpenApiApiKeyBearerRule.ApiKeyNotSupportedOnlyBearerPlusHttp(OAIDocument.SecurityRequirements)
};
var errors = OAIDocument.Validate(ruleSet)?.ToArray();
if (errors != null && errors.Length != 0)
{
var message = string.Join(Environment.NewLine, errors.Select(e => $"{e.Pointer}: {e.Message}"));
throw new InvalidOperationException($"OpenApi document validation failed with errors: {message}");
}

// 4. write the plugins

foreach (var pluginType in Configuration.PluginTypes)