< Summary - Kestrun — Combined Coverage

Information
Class: Public.OpenAPI.Test-KrOpenApiDocument
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/OpenAPI/Test-KrOpenApiDocument.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 78
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 12/12/2025 - 17:27:19 Line coverage: 0% (0/24) Total lines: 78 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/OpenAPI/Test-KrOpenApiDocument.ps1

#LineLine coverage
 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#>
 23function 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
 038        $Server = Resolve-KestrunServer -Server $Server
 39    }
 40    process {
 041        if ( -not $Server.OpenApiDocumentDescriptor.ContainsKey($DocId)) {
 042            throw "OpenAPI document with ID '$DocId' does not exist on the server."
 43        }
 44        # Log the start of the validation process
 045        Write-KrLog -Level Information -Message "Starting OpenAPI document validation for DocId: '{DocId}' Version: '{Ve
 46        # Retrieve the document descriptor
 047        $doc = $Server.OpenApiDocumentDescriptor[$DocId]
 48        # Read and diagnose the OpenAPI document
 049        $result = $doc.ReadAndDiagnose($Version)
 050        if ($null -eq $result) {
 051            throw "Failed to read and diagnose the OpenAPI document with ID '$DocId'."
 52        }
 53        # Log the diagnostic results
 054        foreach ($diagnosticWarning in $result.Diagnostic.Warnings) {
 055            Write-KrLog -Level Warning -Message $diagnosticWarning.ToString()
 56        }
 057        foreach ($diagnosticError in $result.Diagnostic.Errors) {
 058            Write-KrLog -Level Error -Message $diagnosticError.ToString()
 59        }
 060        if ($result.Diagnostic.Errors.Count -eq 0 -and $result.Diagnostic.Warnings.Count -eq 0) {
 061            Write-KrLog -Level Information -Message 'OpenAPI document validation completed successfully.'
 062        } elseif ($result.Diagnostic.Errors.Count -eq 0) {
 063            Write-KrLog -Level Warning -Message 'OpenAPI document validation completed with {WarningsCount} warnings.' -
 64        } else {
 065            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
 068        if ($ThrowOnError.IsPresent -and $result.Diagnostic.Errors.Count -gt 0) {
 069            $errorMessages = $result.Diagnostic.Errors | ForEach-Object { $_.Message }
 070            $combinedMessage = "OpenAPI document validation failed with the following errors:`n" + ($errorMessages -join
 071            throw $combinedMessage
 72        }
 073        if ($DiagnosticVariable) {
 074            Set-Variable -Name $DiagnosticVariable -Value $result.Diagnostic -Scope Script -Force
 75        }
 076        return ($result.Diagnostic.Errors.Count -eq 0 -and $result.Diagnostic.Warnings.Count -eq 0)
 77    }
 78}

Methods/Properties

Test-KrOpenApiDocument()