Skip to content

Commit

Permalink
Merge pull request #1686 from KuraiAndras/return-non-0-exit-code-on-v…
Browse files Browse the repository at this point in the history
…alidate

Return -1 exit code when the document is not valid
  • Loading branch information
MaggieKimani1 authored Jun 10, 2024
2 parents d9fc82b + 71beac4 commit 09bef6c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public async Task<int> InvokeAsync(InvocationContext context)
try
{
if (hidiOptions.OpenApi is null) throw new InvalidOperationException("OpenApi file is required");
await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
return 0;
var isValid = await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
return isValid is not false ? 0 : -1;
}
#if RELEASE
#pragma warning disable CA1031 // Do not catch general exception types
Expand Down
11 changes: 9 additions & 2 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
/// <summary>
/// Implementation of the validate command
/// </summary>
public static async Task ValidateOpenApiDocument(
/// <returns><see langword="true"/> when valid, <see langword="false"/> when invalid and <see langword="null"/> when cancelled</returns>
public static async Task<bool?> ValidateOpenApiDocument(
string openApi,
ILogger logger,
CancellationToken cancellationToken = default)
Expand All @@ -332,11 +333,13 @@ public static async Task ValidateOpenApiDocument(
throw new ArgumentNullException(nameof(openApi));
}

ReadResult? result = null;

try
{
using var stream = await GetStream(openApi, logger, cancellationToken).ConfigureAwait(false);

var result = await ParseOpenApi(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);
result = await ParseOpenApi(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);

using (logger.BeginScope("Calculating statistics"))
{
Expand All @@ -358,6 +361,10 @@ public static async Task ValidateOpenApiDocument(
{
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
}

if (result is null) return null;

return result.OpenApiDiagnostic.Errors.Count == 0;
}

private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
Expand Down
25 changes: 25 additions & 0 deletions test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ public async Task ValidateCommandProcessesOpenApi()
Assert.True(true);
}

[Fact]
public async Task ValidFileReturnsTrue()
{
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger);

Assert.True(isValid);
}

[Fact]
public async Task InvalidFileReturnsFalse()
{
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "InvalidSampleOpenApi.yml"), _logger);

Assert.False(isValid);
}

[Fact]
public async Task CancellingValidationReturnsNull()
{
using var cts = new CancellationTokenSource();
await cts.CancelAsync();
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger, cts.Token);

Assert.Null(isValid);
}

[Fact]
public async Task TransformCommandConvertsOpenApi()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: 3.0.0
info:
title: Sample OpenApi
version: 1.0.0
paths:
/api/editresource:
get:
operationId: api.ListEditresource
patch:
operationId: api.UpdateEditresource
responses:
'200':
description: OK
/api/viewresource:
get:
operationId: api.ListViewresource
responses:
'200':
description: OK

0 comments on commit 09bef6c

Please sign in to comment.