diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 3895975fc..44ae4d602 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -17,7 +17,7 @@ jobs: - name: Check out the repo uses: actions/checkout@v4 - name: Login to GitHub package feed - uses: docker/login-action@v3.1.0 + uses: docker/login-action@v3.2.0 with: username: ${{ secrets.ACR_USERNAME }} password: ${{ secrets.ACR_PASSWORD }} @@ -30,13 +30,13 @@ jobs: id: getversion - name: Push to GitHub Packages - Nightly if: ${{ github.ref == 'refs/heads/vnext' }} - uses: docker/build-push-action@v5.3.0 + uses: docker/build-push-action@v5.4.0 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly - name: Push to GitHub Packages - Release if: ${{ github.ref == 'refs/heads/master' }} - uses: docker/build-push-action@v5.3.0 + uses: docker/build-push-action@v5.4.0 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }} diff --git a/.vscode/launch.json b/.vscode/launch.json index c8714e62e..2fa4340b3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi/bin/Debug/net8.0/Microsoft.OpenApi.Hidi.dll", "args": ["plugin", - "-m","C:\\Users\\darrmi\\src\\github\\microsoft\\openapi.net\\test\\Microsoft.OpenApi.Hidi.Tests\\UtilityFiles\\exampleapimanifest.json", + "-m","${workspaceFolder}/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/exampleapimanifest.json", "--of","./output"], "cwd": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console diff --git a/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs b/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs index e0bfbf6b3..4c14cbef6 100644 --- a/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs +++ b/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs @@ -33,8 +33,8 @@ public async Task 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 diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index 3ef589659..01e5c1427 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -9,7 +9,7 @@ enable hidi ./../../artifacts - 1.4.4 + 1.4.5 OpenAPI.NET CLI tool for slicing OpenAPI documents true @@ -34,8 +34,8 @@ - - + + diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index f33fc61da..d3d3fdd8c 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -322,7 +322,8 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe /// /// Implementation of the validate command /// - public static async Task ValidateOpenApiDocument( + /// when valid, when invalid and when cancelled + public static async Task ValidateOpenApiDocument( string openApi, ILogger logger, CancellationToken cancellationToken = default) @@ -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")) { @@ -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 ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default) diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index d73767375..3bbbe339d 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -3,7 +3,7 @@ netstandard2.0 latest true - 1.6.14 + 1.6.15 OpenAPI.NET Readers for JSON and YAML documents true diff --git a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj index 41731665b..76c67aeeb 100644 --- a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj +++ b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index 45b707c96..ff795f27a 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -3,7 +3,7 @@ netstandard2.0 Latest true - 1.6.14 + 1.6.15 .NET models with JSON and YAML writers for OpenAPI specification true diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs index f7c5aab45..7314da8ab 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs @@ -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() diff --git a/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/InvalidSampleOpenApi.yml b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/InvalidSampleOpenApi.yml new file mode 100644 index 000000000..772214f5a --- /dev/null +++ b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/InvalidSampleOpenApi.yml @@ -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 \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index 54714c352..a516ec7e5 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -15,7 +15,7 @@ - +