Skip to content

Commit 71beac4

Browse files
committed
Return -1 exit code when the document is not valid
1 parent 2049b73 commit 71beac4

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public async Task<int> InvokeAsync(InvocationContext context)
3333
try
3434
{
3535
if (hidiOptions.OpenApi is null) throw new InvalidOperationException("OpenApi file is required");
36-
await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
37-
return 0;
36+
var isValid = await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
37+
return isValid is not false ? 0 : -1;
3838
}
3939
#if RELEASE
4040
#pragma warning disable CA1031 // Do not catch general exception types

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
322322
/// <summary>
323323
/// Implementation of the validate command
324324
/// </summary>
325-
public static async Task ValidateOpenApiDocument(
325+
/// <returns><see langword="true"/> when valid, <see langword="false"/> when invalid and <see langword="null"/> when cancelled</returns>
326+
public static async Task<bool?> ValidateOpenApiDocument(
326327
string openApi,
327328
ILogger logger,
328329
CancellationToken cancellationToken = default)
@@ -332,11 +333,13 @@ public static async Task ValidateOpenApiDocument(
332333
throw new ArgumentNullException(nameof(openApi));
333334
}
334335

336+
ReadResult? result = null;
337+
335338
try
336339
{
337340
using var stream = await GetStream(openApi, logger, cancellationToken).ConfigureAwait(false);
338341

339-
var result = await ParseOpenApi(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);
342+
result = await ParseOpenApi(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);
340343

341344
using (logger.BeginScope("Calculating statistics"))
342345
{
@@ -358,6 +361,10 @@ public static async Task ValidateOpenApiDocument(
358361
{
359362
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
360363
}
364+
365+
if (result is null) return null;
366+
367+
return result.OpenApiDiagnostic.Errors.Count == 0;
361368
}
362369

363370
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ public async Task ValidateCommandProcessesOpenApi()
203203
Assert.True(true);
204204
}
205205

206+
[Fact]
207+
public async Task ValidFileReturnsTrue()
208+
{
209+
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger);
210+
211+
Assert.True(isValid);
212+
}
213+
214+
[Fact]
215+
public async Task InvalidFileReturnsFalse()
216+
{
217+
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "InvalidSampleOpenApi.yml"), _logger);
218+
219+
Assert.False(isValid);
220+
}
221+
222+
[Fact]
223+
public async Task CancellingValidationReturnsNull()
224+
{
225+
using var cts = new CancellationTokenSource();
226+
await cts.CancelAsync();
227+
var isValid = await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger, cts.Token);
228+
229+
Assert.Null(isValid);
230+
}
206231

207232
[Fact]
208233
public async Task TransformCommandConvertsOpenApi()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample OpenApi
4+
version: 1.0.0
5+
paths:
6+
/api/editresource:
7+
get:
8+
operationId: api.ListEditresource
9+
patch:
10+
operationId: api.UpdateEditresource
11+
responses:
12+
'200':
13+
description: OK
14+
/api/viewresource:
15+
get:
16+
operationId: api.ListViewresource
17+
responses:
18+
'200':
19+
description: OK

0 commit comments

Comments
 (0)