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

Return -1 exit code when the document is not valid #1686

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading