| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Tests the OpenAPI document for the specified Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function tests the OpenAPI document for the specified Kestrun server using the discovered components. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance for which the OpenAPI document will be tested. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER DocId |
| | | 10 | | The ID of the OpenAPI document to test. Default is 'default'. |
| | | 11 | | .PARAMETER Version |
| | | 12 | | The OpenAPI specification version to use for testing. Default is OpenApi3_1. |
| | | 13 | | .PARAMETER ThrowOnError |
| | | 14 | | If specified, the function will throw an error if the OpenAPI document contains any validation errors |
| | | 15 | | .PARAMETER DiagnosticVariable |
| | | 16 | | If specified, the function will store the diagnostic results in a variable with this name in the script scope. |
| | | 17 | | .EXAMPLE |
| | | 18 | | # Test the OpenAPI document for the default document ID |
| | | 19 | | Test-KrOpenApiDocument -Server $myServer -DocId 'default' |
| | | 20 | | .OUTPUTS |
| | | 21 | | Microsoft.OpenApi.Reader.OpenApiDiagnostic |
| | | 22 | | #> |
| | | 23 | | function Test-KrOpenApiDocument { |
| | | 24 | | [KestrunRuntimeApi('Everywhere')] |
| | | 25 | | [OutputType([bool])] |
| | | 26 | | param( |
| | | 27 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 28 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 29 | | [Parameter()] |
| | | 30 | | [string]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultSchemeName, |
| | | 31 | | [Parameter(Mandatory = $false)] |
| | | 32 | | [Microsoft.OpenApi.OpenApiSpecVersion]$Version = [Microsoft.OpenApi.OpenApiSpecVersion]::OpenApi3_1, |
| | | 33 | | [switch]$ThrowOnError, |
| | | 34 | | [string]$DiagnosticVariable |
| | | 35 | | ) |
| | | 36 | | begin { |
| | | 37 | | # Ensure the server instance is resolved |
| | 0 | 38 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 39 | | } |
| | | 40 | | process { |
| | 0 | 41 | | if ( -not $Server.OpenApiDocumentDescriptor.ContainsKey($DocId)) { |
| | 0 | 42 | | throw "OpenAPI document with ID '$DocId' does not exist on the server." |
| | | 43 | | } |
| | | 44 | | # Log the start of the validation process |
| | 0 | 45 | | Write-KrLog -Level Information -Message "Starting OpenAPI document validation for DocId: '{DocId}' Version: '{Ve |
| | | 46 | | # Retrieve the document descriptor |
| | 0 | 47 | | $doc = $Server.OpenApiDocumentDescriptor[$DocId] |
| | | 48 | | # Read and diagnose the OpenAPI document |
| | 0 | 49 | | $result = $doc.ReadAndDiagnose($Version) |
| | 0 | 50 | | if ($null -eq $result) { |
| | 0 | 51 | | throw "Failed to read and diagnose the OpenAPI document with ID '$DocId'." |
| | | 52 | | } |
| | | 53 | | # Log the diagnostic results |
| | 0 | 54 | | foreach ($diagnosticWarning in $result.Diagnostic.Warnings) { |
| | 0 | 55 | | Write-KrLog -Level Warning -Message $diagnosticWarning.ToString() |
| | | 56 | | } |
| | 0 | 57 | | foreach ($diagnosticError in $result.Diagnostic.Errors) { |
| | 0 | 58 | | Write-KrLog -Level Error -Message $diagnosticError.ToString() |
| | | 59 | | } |
| | 0 | 60 | | if ($result.Diagnostic.Errors.Count -eq 0 -and $result.Diagnostic.Warnings.Count -eq 0) { |
| | 0 | 61 | | Write-KrLog -Level Information -Message 'OpenAPI document validation completed successfully.' |
| | 0 | 62 | | } elseif ($result.Diagnostic.Errors.Count -eq 0) { |
| | 0 | 63 | | Write-KrLog -Level Warning -Message 'OpenAPI document validation completed with {WarningsCount} warnings.' - |
| | | 64 | | } else { |
| | 0 | 65 | | Write-KrLog -Level Error -Message 'OpenAPI document validation completed with {ErrorsCount} errors.' -Value |
| | | 66 | | } |
| | | 67 | | # Throw an error if validation failed and the switch is set |
| | 0 | 68 | | if ($ThrowOnError.IsPresent -and $result.Diagnostic.Errors.Count -gt 0) { |
| | 0 | 69 | | $errorMessages = $result.Diagnostic.Errors | ForEach-Object { $_.Message } |
| | 0 | 70 | | $combinedMessage = "OpenAPI document validation failed with the following errors:`n" + ($errorMessages -join |
| | 0 | 71 | | throw $combinedMessage |
| | | 72 | | } |
| | 0 | 73 | | if ($DiagnosticVariable) { |
| | 0 | 74 | | Set-Variable -Name $DiagnosticVariable -Value $result.Diagnostic -Scope Script -Force |
| | | 75 | | } |
| | 0 | 76 | | return ($result.Diagnostic.Errors.Count -eq 0 -and $result.Diagnostic.Warnings.Count -eq 0) |
| | | 77 | | } |
| | | 78 | | } |